CREATE TABLE statement in SQL is used to create a table in SQL. When a user executes a CREATE TABLE statement, he becomes the default owner of the table. The new table name should be unique. A column is defined in parentheses in a CREATE TABLE statement.
CREATE TABLE COMPANIES (COMPANY INTEGER NOT NULL, CITY VARCHAR(20) NOT NULL, REGION VARCHAR(20) NOT NULL, CEO VARCHAR(20), PROFITS MONEY NOT NULL)
Rules for creating a column in a CREATE TABLE statement:
1) Cloumns are defined from left to right.
2) A column name must be unique in a table but can be a duplicate in other tbales.
3) A data type must be dined for a column.
4) Required data is used to specify that a column values cannot be NULL.
5) Default value is used to specify a default value for a column if there’s no value specified.
PRIMARY KEY clause: It is used to specify a column or columns as a primary key of the table.
FOREIGN KEY clause: It is used to specify a column or columns as a foreign key. It also establishes a relationship and identifies the parent table. A name can also be defined for a relationship for use in error messages and can also be used for dropping the foreign key. Optional delete rule is also specified by this clause to delete a parent row. It also defines an optional update rule for relationship to update part of primary key in a parent row. An optional check constraint can also be defined by this clause.