SQL CREATE TABLE Keyword
The SQL CREATE TABLE keyword is used to create a new table. Creating a table involves providing a name to the table and defining name and data type (e.g. varchar, integer, date, etc.) of each column.
Syntax
The syntax of the CREATE TABLE keyword is given below:
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ..... PRIMARY KEY(one or more columns) );
Example: Create a table
The below mentioned SQL code creates a table called Employee which contains five columns: EmpID, Name, City, Age and Salary.
CREATE TABLE Employee ( EmpID INT NOT NULL, Name VARCHAR(255) NOT NULL, City VARCHAR(100), Age INT, Salary DECIMAL(18,2), PRIMARY KEY(EmpID) );
This will create a empty table named Employee containing five columns as shown below:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
When the table is created successfully it displays a message. Along with this, it can also be checked using DESC command as follows:
DESC Employee;
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
EmpID | int(11) | No | PRI | ||
Name | varchar(255) | No | |||
City | varchar(100) | Yes | NULL | ||
Age | int(11) | Yes | NULL | ||
Salary | decimal(18,2) | Yes | NULL |
This indicates that the Employee is now available in the database which can be used to store information related to employee.
❮ SQL Keywords