CREATE TABLE
statementCREATE TABLE [IF NOT EXISTS] tableName ( columnName1 dataType [columnAttributes] [, columnName2 dataType [columnAttributes]] [, columnName3 dataType [columnAttributes]]... )
Attribute | Description |
---|---|
UNIQUE | Specifies that each value stored in the column must be unique, but allows the column to store NULL values. |
NOT NULLL | Indicates that the column doesn’t accept NULL values. If omitted, the column can accept NULL values. |
DEFAULT default_value | Specifies a default value for the column. |
CREATE TABLE customers ( customerID INT, firstName VARCHAR(60), lastName VARCHAR(60) );
CREATE TABLE customers ( customerID INT NOT NULL UNIQUE, firstName VARCHAR(60) NOT NULL, lastName VARCHAR(60) NOT NULL );
CREATE TABLE orders ( orderID INT NOT NULL UNIQUE, customerID INT NOT NULL UNIQUE, orderNumber VARCHAR(50) NOT NULL, orderDate DATE NOT NULL, orderTotal DECIMAL(9,2) NOT NULL, paymentTotal DECIMAL(9,2) DEFAULT 0 );
CREATE TABLE
statement creates a table based on the column definitions and column attributes you specify.NOT NULL
and UNIQUE
attributes limit the type of data that a column can store, they are known as constraints.CREATE TABLE
statement, you can search the MySQL Reference Manual, which is available online.