SQL Create Table Statement

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

SQL Create Table using Query

In sql, by using “Create TABLE” statement we can create a table in sql database. Generally to create 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 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 above syntax, we defined a different type of parameters, those are

 

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

SQL Create Table using Query Example

Following is the example of creating a table called “EmployeeDetails” with columns empid, empname, designation, salary, location and joineddate in 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 joineddate column is a datetime datatype, it will expect datetime format values.

 

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

 

SELECT * FROM EmployeeDetails

When we execute above query, we will get the result like as shown 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 like 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 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 new window in that create 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 save option available on top or press Ctrl + S to save the table details, whenever we press save or Ctrl + F5 option it will ask you for table name, enter required table name and click OK to create a new table in database.

 

To check 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