SQL Server - CREATE TABLE Keyword
The SQL Server (Transact-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 using CREATE TABLE keyword in SQL Server (Transact-SQL) is given below:
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ..... ..... );
Example: Create a table
The mentioned below statement creates a table called Employee which contains five columns: EmpID, Name, City, Age and Salary.
CREATE TABLE Employee ( EmpID INT NOT NULL PRIMARY KEY, Name VARCHAR(255) NOT NULL, City VARCHAR(100), Age INT, Salary DECIMAL(18,2) );
In the above statement, the data type specifies what type of data the column can hold. After the data type, optional attributes can be provided for each column, like NOT NULL is used to ensure that a column cannot have a NULL value. See SQL Server Constraints for more information.
The above statement will create a empty table named Employee containing five columns. When the table is created successfully it displays a message. Along with this, it can also be checked using EXEC sp_columns command as shown below:
EXEC sp_columns Employee;
This result of the above code will be similar to:
TABLE_OWNER | TABLE_NAME | COLUMN_NAME | DATA_TYPE | TYPE_NAME | PRECISION | LENGTH |
---|---|---|---|---|---|---|
dbo | Employee | EmpID | 4 | int identity | 10 | 4 |
dbo | Employee | Name | 12 | varchar | 255 | 255 |
dbo | Employee | City | 12 | varchar | 100 | 100 |
dbo | Employee | Age | 4 | int | 10 | 4 |
dbo | Employee | Salary | 3 | decimal | 18 | 20 |
This indicates that the Employee is now available in the database which can be used to store information related to employee.
Create a table using Another table
The SQL Server (Transact-SQL) SELECT INTO keyword is used to create a table from an existing table by copying the existing table's columns. When creating a table in this way, the new table will be populated with the records from the existing table (based on the SELECT statement).
Syntax
The syntax for using SELECT INTO keyword in SQL Server (Transact-SQL) is given below:
/* copy all columns into new_table */ SELECT * INTO new_table FROM old_table WHERE condition; /* copy selected columns into new_table */ SELECT column1, column2, column3, ... INTO new_table FROM old_table WHERE condition;
Example:
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
-
Copy all columns of a table: The below statement is used to copy all columns of Employee table into a new table called Employee_PT. The new table will contain all records of Employee table where the age of the employee is less than 28.
SELECT * INTO Employee_PT FROM Employee WHERE Age < 28; -- see the result SELECT * FROM Employee_PT;
This will produce the result as shown below:
EmpID Name City Age Salary 1 John London 25 3000 2 Marry New York 24 2750 3 Jo Paris 27 2800 -
Copy selected columns of a table: It is also possible to copy selected columns when using SELECT INTO statement. In the example below, the columns which are copied are: EmpID, Name and City.
SELECT EmpID, Name, City INTO Employee_PT FROM Employee WHERE Age < 28; -- see the result SELECT * FROM Employee_PT;
This will produce the result as shown below:
EmpID Name City 1 John London 2 Marry New York 3 Jo Paris -
Using AS clause: The AS clause can be used to rename the column name. See the example below:
SELECT EmpID, Name AS EmployeeName, City INTO Employee_PT FROM Employee WHERE Age < 28; -- see the result SELECT * FROM Employee_PT;
This will produce the result as shown below:
EmpID EmployeeName City 1 John London 2 Marry New York 3 Jo Paris
❮ SQL Server Keywords