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 ...

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.