Saturday 5 August 2017

How to write Query for creating a table and Inserting data into table in Sql Server ?


Here we are Creating the table Rolemaster_tbl , which will be used  on login page(in next post) so script for creating the table is given below.

create table Rolemaster_tbl
(RoleID int not null identity(1,1),
UserRole nvarchar(max) not null,
primary key(RoleID)
)

Create table is the Sql Command used for creating a table.
 Rolemaster_tbl is the name of table.



On the above query  Rolemaster_tbl is the name of table... RoleID is first column which is primary key and auto incremented by 1.
Once the table created than we have to add data into it , so here I  have written a script for inserting data into table.
Insert into Rolemaster_tbl(UserRole) values('Student');
Insert into Rolemaster_tbl(UserRole) values('Parent');
Insert into Rolemaster_tbl(UserRole) values('Teacher');
Insert into Rolemaster_tbl(UserRole) values('Admin');
Insert into Rolemaster_tbl(UserRole) values('Principal');
Insert into Rolemaster_tbl(UserRole) values('Transport');
Insert into Rolemaster_tbl(UserRole) values('Exam Control');

I only specify one column name Rolemaster_tbl(UserRole) rather than two columns Rolemaster_tbl(RoleID,UserRole) because RoleID is the Primary key and we do not need to specify primary key when we insert data into table.

Now run the Query  Select * from Rolemaster_tbl  on query window and show the result of table.
Output of Query is given below.

RoleID UserRole
1         Student
2         Parent
3         Teacher
4         Admin
5        Principal
6       Transport
7       Exam Control

No comments:

Post a Comment

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...