SQL CREATE TABLE Statement
The SQL CREATE TABLE statement is used to create a new table in a database.
Tables are organized into rows and columns; and each table must have a name.
Related Links
SQL CREATE TABLE Syntax
The below syntax is used to create sql database table:
CREATE TABLE your_table_name
(
table_column_name1 datatype(size),
table_column_name2 datatype(size),
table_column_name3 datatype(size),
....
table_column_nameN datatype(size),
);
Syntax Description
CREATE TABLE is the keyword telling the database system to create a new table. The table_name must be unique name for the table follows the CREATE TABLE statement.
Then in brackets comes the list defining each column_name in the table and each column must have a data type. The datatype parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.).
The size parameter specifies the maximum length of the column of the table.
SQL CREATE TABLE Example
Following is an example, which creates a new "Books" table that contains four columns: BookID, BookName, AuthorName, and BookPrice.
We use the following CREATE TABLE statement:
CREATE TABLE Books
(
BookID INT,
BookName VARCHAR(255),
AuthorName VARCHAR(255),
BookPrice DECIMAL (7, 2)
);
The BookID field is of type INT and will hold an integer.
The BookName and AuthorName fields are of type VARCHAR and will hold characters, and the maximum length for these columns is 255 characters.
The BookPrice field is of type DECIMAL and will hold an floating point value.
You can check if your table has been created successfully by looking at the message displayed by the database systems, otherwise you can use "DESC" command to view your table structure.
Column Name | Datatype | NULL |
---|---|---|
BookID | INT | YES |
BookName | VARCHAR | YES |
AuthorName | VARCHAR | YES |
BookPrice | DECIMAL | YES |
The empty "Books" table will now look like this:
BookID | BookName | AuthorName | BookPrice |
---|---|---|---|
Related Links