Creating tables
In its basic structure, the MySQL statement for creating tables is very similar to that for databases. Recall that database creation is involved in simply naming the database:
CREATE DATABASE foo;;
Creating a table requires the definition of at least one column. The basic syntax looks like this (note that the backticks are optional):
CREATE TABLE <table name> (`<column name>` <column specifications>);
In practice, definition of a table bar
in database foo
would be as follows:
CREATE TABLE bar (snafu varchar(30));
While this is basically the statement, it is also a very flawed way of defining the table when using the MySQLdb
module. Before we go into what is wrong, however, let's cover what is right.
Note
A comprehensive discussion of the options available when creating a table can be found in the MySQL manual: http://dev.mysql.com/doc/refman/5.4/en/create-table.html
Obviously, we declare a table named bar
. MySQL requires at least one column to be defined. A column...