SQL CREATE TABLE statement


SQL CREATE TABLE statement

Create TABLE statements are used to create tables in a database.

Tables consist of rows and columns, each of which must have a table name.

SQL CREATE TABLE syntax

CREATE TABLE table_name                
(                
column_name1 data_type(size),                
column_name2 data_type(size),                
column_name3 data_type(size),                
....                
);       

column_name parameters specify the name of the column in the table.

data_type parameters specify the data types of columns (e.g. varchar, integer, decimal, date, and so on).

The size parameter specifies the maximum length of the columns in the table.

Tip: To learn about the types of data available in MS Access, MySQL, and SQL Server, visit our complete Data Type Reference Manual.


SQL CREATE TABLE instance

Now we want to create a table called "Peoples" with five columns: PersonID, LastName, FirstName, Address, and City.

Let's use the following CREATE TABLE statement:

CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

The PersonID column data type is int and contains an integer.

LastName, FirstName, Address, and City columns have varchar data types that contain characters, and the maximum length of these fields is 255 characters.

The empty "Persons" table is like this:

PersonID LastName FirstName Address City

Tip: Use the INSERT INTO statement to write data to an empty table.