This example shows how to set default values for each column — as well as how to allow (or not allow) NULL values in the column. By default, any column can contain NULL values.
(Note that we’re also using “CREATE TABLE IF NOT EXISTS” so an error doesn’t get thrown if the table already exists when we try to create it.)
We’ve added comments as well, though we’ll explain those after.
/* Create the table foo.example_02 if it doesn't exist. */ CREATE TABLE IF NOT EXISTS `foo`.`example_02` ( `field_1` VARCHAR(255) NOT NULL DEFAULT '', `field_2` CHAR(20) NOT NULL default 'default_text', `field_3` INT(10) DEFAULT NULL, `field_4` BIGINT(20), `field_5` DATETIME -- embed a comment in the definition like this... );
Note that if you don’t specify NULL or NOT NULL, columns by default can contain NULL values. And not specifying a default is the same as specifying ‘DEFAULT NULL’ since MySQL uses NULL as the default value in both cases.
Note also that I’ve added comments to the declaration. Comments come in two types — multi-line comments, and single-line comments.
/* This is a multi-line comment */ -- This is single-line comment select foo from bar; -- This means the rest of this line is a comment