SQL Create Table Statement

In SQL, we can create tables either by using the "CREATE TABLE TABLENAME" statement or directly from the SQL server management studio.

SQL Create Table using Query

In SQL, by using the “Create TABLE” statement, we can create a table in the SQL database. Generally, to create a table in SQL our syntax will be like as shown below.

SQL Create Table using Query Syntax

Following is the syntax of creating a table using the CREATE TABLE statement in SQL Server.

 

CREATE TABLE tablename(
columnname1 datatype NOT NULL,
columnname2 datatype(size) NULL,
columname3 datatype(size) NULL,
.......
.......
columnnameN datatype(size) NULL
)

If you observe the above syntax, we defined different types of parameters, those are

 

ParameterDescription
tablename It's the name of the table we will create in the database.
columnname It is the name of the column that we are going to create.
datatype It is useful to specify a type of data we will insert in columns (Ex: int, varchar, datetime, float, etc.).
size It means the maximum size of data that we can insert in columns.
NULL It will allow users to insert null values in columns.
NOT NULL It will not allow the user to insert null values in the column.

SQL Create Table using Query Example

The following is the example of creating a "EmployeeDetails" table with columns empid, empname, designation, salary, location, and joineddate in the database.

 

CREATE TABLE EmployeeDetails
(
empid int NOT NULL,
empname varchar(50) NULL,
designation varchar(50) NULL,
salary int NOT NULL,
location varchar(50) NULL,
joineddate datetime NULL
)

Here, empidsalary columns are int datatypes, and it will expect only integer values. The empname, designation, location columns are varchar datatypes, and these will accept a maximum of 50 characters, and the joineddate column is a datetime datatype, it will expect datetime format values.

 

Once we create a table, try to run the following SQL select query to get the details from the “EmployeeDetails” table.

 

SELECT * FROM EmployeeDetails

When we execute the above query, we will get the result below.

 

Crete New table in SQL Server Database using Query Result

 

You can check this “EmployeeDetails” table in SQL Server management studio under your database section, as shown below.

 

SQL Server Newly Created Table in Database Output

SQL Create Table using SQL Server Management Studio

To create a new table in the database, open SQL Server Management Studio à Open Databases section à Select Required Database to create a table à Right click on Tables section and Select New Table like as shown below.

 

Create new table using sql server management studio

 

Whenever we click on “New Table” it will open a new window in that create the table by entering column name, datatype and allow null options like as shown below.

 

SQL Create new table by entering column name, datatype and null values

Once we enter all the fields, click the save option available on top or press Ctrl + S to save the table details, whenever we press the save or Ctrl + F5 option, it will ask you for the table name, enter the required table name, and click OK to create a new table in the database.

 

To check the newly created table, open databases à select your Database à Right-click and select Refresh option to refresh database à Now check it in Tables section that would be like as shown below.

 

SQL Server Newly Created Table in Database using SQL Management Studio Output