Create Tables and Insert Data in SQL Server

Hi programmers , welcome to another Microsoft SQL server post. this article i’ll write different query to create table and insert records in SQL server. The Create statement is used to create tables and insert statement is used to inserting data into database table. let’s see the queries.

1.Creating basic tables without using any constraints. the columns has null means it accepts null into rows.we can insert duplicate data because table doesn’t have any primary or unique keys. see below image.

2.Creating table with primary key and unique keys.the primary and unique keys accepts unique and not null values. see below image.

3.we can insert multiple records using single insert statement. see below image.

4.Creating table with identity column or auto generated column, using identity column you don’t need to pass any values. it generate rows values each times query run. see below image.

Directly Test below Queries into Editor.

--Basic table
Create Table Employee
( 
Emp_ID int null ,
Emp_Name varchar(55) null,
Emp_Salary int null
);

-- Inserting Records
Insert into Employee values(10,'James',7000);

--For Inserting Multiple Records
Insert into Employee values(10,'James',7000);
Insert into Employee values(20,'Chris',6000);

*************************************************
-- Creating Table using Primary Key
Create Table Emp
( 
Emp_ID int ,
Emp_Name varchar(55) null,
Emp_Salary int null,
Primary key (Emp_ID),
unique(Emp_Name)
);
-- Inserting Records
Insert into Emp values(10,'James',7000);

--For Inserting Multiple Records
Insert into Emp values(20,'Jones',7000);
Insert into Emp values(30,'Chris',6000);

--Insert multiple data using one Insert Statement
Insert into Emp(Emp_ID,Emp_Name, Emp_Salary) values(40,'Mathew',8000),
(50,'Sally',9000),(60,'Marks',7600);
******************************************

-- Creating Table using Idenntity
Create Table Empl
( 
Emp_ID int identity(1,1) primary key ,
Emp_Name varchar(55) null,
Emp_Salary int null,
Age int
);
-- Inserting Records
Insert into Empl values('James',7000,67);

--For Inserting Multiple Records
Insert into Empl values('Jones',7000,25);
Insert into Empl values('Chris',6000,26);

--Select Table 
Select * from Empl;

Happy Coding…Thanks.

Post Author: adama