SQL CREATE TABLE
SQL CREATE TABLE statement
The CREATE TABLE statement is used to create a table in the database.
Tables are composed of rows and columns, and each table must have a table name.
SQL CREATE TABLE syntax
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
.. ..
);
column_name parameter specifies the name of the column in the table.
The data_type parameter specifies the data type of the column (such as varchar, integer, decimal, date, etc.).
The size parameter specifies the maximum length of the columns in the table.
Tip: To learn about the data types available in MS Access, MySQL and SQL Server, visit our complete Data Types Reference Manual.
SQL CREATE TABLE Example
Now we want to create a table named "Persons" with five columns: PersonID, LastName, FirstName, Address and City.
We use the following CREATE TABLE statement:
Example
(
PersonID int,
LastName varchar(255 ),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The data type of the PersonID column is int, Contains integers.
The data type of the LastName, FirstName, Address, and City columns is varchar, including characters, and the maximum length of these fields is 255 characters.
The empty "Persons" table looks like this:
LastName | FirstName | Address | City | |
---|---|---|---|---|
You can use the INSERT INTO statement to write data to an empty table.