Here we will learn how to create tables in sql server databases. Generally we can create tables in sql server using queries and sql server management studio.
To create SQL Table we have two different methods one is using Query like “CREATE TABLE TABLENAME etc…” and another one is directly using SQL Server Management Studio
By using “Create TABLE” statement we can create SQL TABLE. Generally to create table in SQL our syntax will be like as shown below
Following sql statement is the syntax to create table 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 have different type of parameters.
tablename: It means name of the table which we are going to create in database
columnname: it means name of the column which we are going to create
datatype: it is used to specify 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 means allow users to insert null values in column
NOT NULL: It means it will not allow user to insert null values in column
The following SQL statement will create new table “EmployeeDetails” with columns empid, empname, designation, salary, location and joineddate
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 empid and salary columns are int datatype and it will expect only integer values and empname, designation, location columns are varchar datatype it will expect maximum of 50 characters and joineddate column is datetime datatype it will expect datetime format values.
Once we create table try to run following SQL select query to get details from “EmployeeDetails” table
SELECT * FROM EmployeeDetails
Now our sql table will be like as shown below
You can check this “EmployeeDetails” table in SQL Server management studio under your database section like as shown below
To create new table in database open SQL Server Management Studio --> Open Databases section --> Select Required Database to create table --> Right click on Tables section and Select New Table like as shown below
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
Once we enter all the fields click save option available on top or press Ctrl + S to save table details whenever we press save or Ctrl + S option it will ask you for table name give your table name and click OK to create 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