A complete step by step reference for SQL Server :-
SQL Syntax—select student name class from students where class<> ’science’
What is SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
What Can SQL do?
• Execute queries in opposition to a database
• Retrieve data from a database
• Insert records in a database
• Update records in a database
• Delete records from a database
• Create new databases
• Create new tables in a database
• Create stored procedures in a database
• Create views in a database
RDBMS(Relational Database Management System):-
RDBMS stands for Relational Database Management System.RDBMS is the basis for SQL, and for all contemporary database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.The data in RDBMS is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
SQL Syntax
Database Tables
A database most frequently contains one or more tables. Each table is recognized by a name (e.g. "Students_Info" or "Section"). Tables contain records (rows) with data.
Below is an example of a table called "Student":
The table above three records and five columns.
How to Create Database?
Create Database-
How to create Table?
Create table
CREATE TABLE Table_name
(
Column _name1 data type,
Column _name2 data type,
Column_ name 3 data type,
Column _name4 data type,
…………………………………
)
Example:-
CREATE TABLE student
(
Stu_Id int,
L_Name varchar(50),
F_Name varchar(50),
Add varchar(50),
City varchar(50)
)
Now create table student:
| Stu_Id | L_Name | F_Name | Add | City |
How to insert record in table?
Insert record in table:we can use insert into command .
As
insert into student values(1,'Singh','Pankaj','B-10','Lucknow')
insert into student values(2,'Singh','rani','c-23','Dehradun')
insert into student values(3,'Ranjan','Rajesh','d-11','Delhi')
select *from student
After having this command your table will look like this.
| Stu_Id | L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | rani | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
The table above three records and five columns.
SQL Statements
If you will select all record in the “student” table:
See the SQL command written below
Select *from student |
Show all record in a student table
SQL (structure Query Language)
SQL divide in a two part
We use the following SQL statement:
DML (Data manipulation Language
The query and update commands form the Data Manipulation Language part of SQL:
- SELECT - extracts data from a database
The SQL select statement:--The select statement is used to select data from a database.
See the SQL command written below
| Select *from table_name |
Example-table name “student”
| Select *from student |
| Stu_Id | L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | rani | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
And if you want choose only content of the column as F_Name and Add..
Use select command-
Example-
Select F_name,Add from student |
Output is-
F_Name |
Add |
| Pankaj | B-10 |
| rani | C-23 |
| Rajesh | d-11 |
- UPDATE - updates data in a database
The update command used to update record in a table.
See the SQL command written below
Update table_name set column1=value…..where column=value |
Now we want to update the student "rani" in the "student" table.
Example-
Update student set F_name=’Ranchiki’ where Add=’c-23’ |
Output:-
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
SQL DELETE Command:
- DELETE - deletes data from a database
See the Sql command written below
Delete from table_name where column=value.. |
Now we want to delete Row , the student "Rajesh" in the "student" table.
Example-
Delete from student where F_name=’Rajesh’ |
Output is:-
| Stu_Id | L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
Note **If you want to delete all rows in a table then we want to use
| Delete *from student |
SQL Insert into Command:
- INSERT INTO - inserts new data into a database
See the Sql command written below
INSERT INTO "table_name" ("column1", "column2", ...) VALUES ("value1", "value2", ...) |
Output
INSERT INTO student (Stu_Id, F_Name, L_Name,Add,City) VALUES (4,'Vijay', ‘Singh’, 'a-21',’Lucknow’) |
Now create one new row student table .
| Stu_Id | L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
| 4 | Singh | Vijay | a-21 | Lucknow |
Some other command in Sql server when we can use
The SELECT DISTINCT keyword allows us to grab all information from a column (or columns) on a table that there will be redundancies
See the Sql command written below
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
See the Sql command written below
SELECT DISTINCT "column_name" FROM "table_name" |
Output:
SELECT DISTINCT F_Name FROM student |
| F_Name | |
| Pankaj | |
| Ranchiki | |
| Rajesh |
And & Or Operator:
The AND operator displays a record if both the first condition and the second condition is true.
And Operator:
| Stu_Id | L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
Syntax-
Select *from student where F_Name=’Ranchiki’ and L_Name=’Singh’ |
Output:-
Stu_Id |
L_Name | F_Name | Add | City |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
Or Operator:
The OR operator displays a record if either the first condition or the second condition is true.
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
Syntax-
Select *from student where F_Name=’Ranchiki’ or F_Name=’Pankaj’ |
Output:-
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
SQL Commands In :
There are two uses of the IN keyword ,value of the returned values we want to see for at least one of the columns
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
Syntax-
| WHERE "column_name" = 'value1' |
Examples:-
SELECT * FROM student Where F_Name IN (‘Pankaj’, 'Ranchiki') |
Output:-
F_Name |
| Pankaj |
| Ranchiki |
LIKE allows you to do a search based on a pattern rather than specifying exactly what is desired (as in IN) or spell out a range
Syntax
SELECT "column_name" FROM "table_name" WHERE "column_name" LIKE {PATTERN } |
Note:find all Student Name whose name contains 'AN'
SELECT * FROM student WHERE F_Name LIKE '%AN%' |
Output:
| Stu_Id | L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
The ORDER BY Keyword:
It is used to sort the result-set by a particular column.The ORDER BY keyword sort the records in ascending order.
If you want to sort the records in a descending order, you can use the DESC keyword.
Syntax: By
Default ascending order record show.
SELECT column_name(s)FROM table_name ORDER BY column_name(s) ASC |
Cmd-
SELECT * FROM Student ORDER BY F_Name |
Output:
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
Order By DESC Command:-
It is used to short descending order.
Syntax:
SELECT column_name(s) FROM table_name ORDER BY column_name(s)ESC |
Cmd:
SELECT column_name(s)FROM table_name ORDER BY column_name(s) ESC |
OUTPUT:
Stu_Id |
L_Name | F_Name | Add | City |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
| 1 | Singh | Pankaj | B-10 | Lucknow |
Primary Key constraint:-
The primary key of a relational table uniquely identifies each record in the table. It can either be a normal attribute that is an unique .Primary keys may consist of a single attribute or multiple attributes in combination.A primary key column cannot contain NULL values.Each table should have a primary key, and each table can have only ONE primary key.

or
The primary Key is a column in a table that uniquely identifies the row in that table.
Table Name------Student
Stu_Id(Set primary Key) DataType(int)
F_Name –Data Type varchar(50)
L_Name—Data Type varchar(50)
Add—Data Type varchar(50)
City—Data Type varchar(50)
Now generate primary key in student table at Stu_Id
CREATE TABLE student
(
Stu_Id int NOT NULL PRIMARY KEY,
L_Name varchar(50) ,
F_Name varchar(50),
Add varchar(50),
City varchar(50)
)
If you have already created table and still there is no any primary key you have made then you need to write command like this:-
alter table student add primary key(Stu_Id)
Let say if you want to make primary key in your table student in the column stu_id it should be not null at the time of creating your table named student.
SQL FOREIGN KEY Constraint
Why we use foreign key?
Here, let say if we have two table and I want to make relation between them for that we need to use Foreign key. But there are a lot’s of point that we should keep in mind for Example:-
There must be a uniqe column in both table containing same data types.
At first you should create second table.
Now create second table:
CREATE TABLE Info
(
Roll_No int NOT NULL PRIMARY KEY,
Section varchar(50) ,
Stu_Id int Not Null
)
Now Create Foreign key for Stu_Id
ALTER TABLE Info ADD FOREIGN KEY (Stu_Id) REFERENCES student (Stu_Id)
Or
If you want to generate a foreign key easily method then you can use
1-Go to second table and select Stu_id then right click relationship click .2- now click add and go to tables and columns specifies
3-tables and columns now generate foreign key.
Join:-
Definition-The SQL JOIN command is used to join the data contained in two relational database tables based upon a common attribute.
Here these are two table one is student and other one is Info first table (student have a Stu_Id (Primary Key) and other one is table (Info) Stu_Id (Foreign key).
Student table:
Stu_Id |
L_Name | F_Name | Add | City |
| 1 | Singh | Pankaj | B-10 | Lucknow |
| 2 | Singh | Ranchiki | C-23 | Dehradun |
| 3 | Ranjan | Rajesh | d-11 | Delhi |
Roll_No |
Section | Stu_Id |
| 101 | Science | 1 |
| 102 | Art | 2 |
| 103 | Commerce | 3 |
Select *from student tb1 join info tb2 on tb2.Stu_Id = tb1.Stu_Id
Now the table will look like this:
| Stu_Id | L_Name | F_Name | Add | City | Roll_No | Section | Stu_Id |
| 1 | Singh | Pankaj | B-10 | Lucknow | 101 | Science | 1 |
| 2 | Singh | Ranchiki | C-23 | Dehradun | 102 | Art | 2 |
| 3 | Ranjan | Rajesh | d-11 | Delhi | 103 | Commerce | 3 |
Inner Join-
Inner Join basically used to join the row which is common in both table.
Here these are two table one is Employee and other one is dep_Emp first table (Employee have a Emp_Id (Primary Key) and other one is table (dep_Emp) Emp_Id (Foreign key).
First Table Name Employee:
| Emp_Id | Emp_Name |
| 1 | Sagar |
| 2 | Ashish |
| 3 | Rajesh |
Dep_Id |
project_Id | Emp_Id |
| 101 | 11 | 1 |
| 102 | 14 | 1 |
| 103 | 12 | 2 |
| 104 | 13 | 2 |
Now give the Inner join:
SELECT Employee.Emp_Name, dep_Emp.project_Id
FROM Employee
Inner JOIN dep_Emp
ON Employee.Emp_Id=dep_Emp.Emp_Id
ORDER BY Employee.Emp_Name
Result will be look like this:
Emp_Name |
Project_Id |
| Ashish | 12 |
| Ashish | 13 |
| Sagar | 11 |
| Sagar | 14 |
Equi Join:-
Equi Join returns all the columns from both tables and filters the records if matching order then use ‘ON ‘statement.
Note: Columns need to specify.
Natural Join:-
It should be specify column where you should keep natural join.
Left Outer Join-:
These keywords revisit all rows from the left table .Unmatched row in the right table do not come into sight.
select *from Employee t11 left outer join dep_Emp t22 on t11.Emp_Id=t22.Emp_Id
or
SELECT Employee.Emp_Name, dep_Emp.dep_Id
FROM Employee
LEFT JOIN dep_Emp
ON Employee.Emp_Id=dep_Emp.Emp_Id
ORDER BY Employee.Emp_Name
Right Outer Join :-
These keywords revisit all rows from the Right table .Unmatched row in the left table do not come into sight.
select *from Employee t1 right outer join dep_Emp t2 on t1.Emp_Id=t2.Emp_Id
The DDL(Data definition Language) part of SQL permits database tables to be created or deleted.
Statements in SQL are:
Create Data base:-
It is used to create database.
Syntax: Create database database_Name
Example:-create database customer
Now create new database name is customer.
Alter Database:
Alter statement is used to modify the database.
Create Table:-
Create statement is used to create a new table.
Syntax:-create table table_name
{
Column1 data type,
Column2 data type,
……
………
}
Example:-
Create table student
{
Stu_Id int,
F_Name varchar(50),
L_Name varchar(50),
Address varchar(50),
City varchar(50),
}
Now Create New table written in below:
Stu_Id |
F_Name | L_Name | Address | City |
Alter Table:-
Alter table is used to modify, add, delete columns.
Syntax:-alter table table_name add column_name datatype
Example:-alter table student add column state_Name varchar(50)
Drop column :
Syntax:-Alter table student drop column state_Name
Modify column :
Syntax:- alter table table_name modify column_name(data type)
Create Index:-Index is mainly used for duplicat values.
Syntax:-create index index_name on table_name(column_name)
Drop Index:-To drop index in a table
Syntax:-drop index index_Name on table_name
How to define a View?
View:-
You can explain simply way that view is used to make a table virtual or make a map same table.
A view comprise of a stored query get-at-able as a virtual table in a relational database.
Views can constitute a subset of the data include in a table.
Views can join and simplify multiple tables into a single virtual table.
How to Create View?
First you should select database (Employee_Record) and give the command.
Create view:
Create a view
create view viewemp
as
select *from Employee
How to do show Operation ?
use Employee_Record
select *from Employee
How to do update operation ?
Update view:-
use Employee_Record
update Employee set Emp_Name='pankaj' where Emp_Id=1
How to do delete operation?
Delete view:-
use Employee_Record
drop view virtualemp
Define Trigger and Type of trigger.How to use trigger with example?
Trigger : It is a
special type of store procedure that is automatically execute in a database
.There are two type of Trigger first is After trigger and second
is instead of Trigger.
Syntax of Trigger:-
CREATE TRIGGER (or Replace Trigger) trigger_name
{BEFORE | AFTER | INSTEAD OF
}
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON tab_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements write
END;
Type of Trigger
:-
Explain In
brief :-
DML(Data
Manipulation Language) Trigger:- According to DML trigger in create statement s(Insert, Update
and Delete).
DDL(Data
Definition Language) Trigger :- According to DDL trigger in create statements(Create, Drop and
Delete)
Data
Manipulation Language Trigger are two types-
i)
After
trigger(Using For/After Clause)
ii)
Instead
of trigger (Using instead of clause)
After
Trigger:- Here we will explain after insert trigger, update trigger,
delete trigger.
First of all we will create New Table
for example:-
- First
create table office_details
v
CREATE TABLE employee_details
(
Emp_ID int identity,
Emp_Name varchar(60),
Emp_Sal decimal (18,0)
)
-- Here we will Insert
records ---
Insert into Employee_details values ('Pankaj',2000);
Insert into Employee_Demo values ('Gaurav',1800);
Insert into Employee_Demo values ('Rani',1600);
Insert into Employee_Demo values ('Avanish',1900);
Insert into Employee_Demo values ('Vijay',2100);
--Now create table Employee_performance --
(
Emp_ID int,
Emp_Name varchar(60),
Emp_Sal decimal(18,0),
Emp_perform varchar(150),
Time_perform datetime
)
i)After
Insert Trigger:-
We will
create trigger in table, than process insert new record in a
table we will insert trigger and insert query than fetch record.
v
Create trigger on table employee_details for Insert statement
CREATE TRIGGER aftertriggerInsert on employee_details
FOR INSERT
AS declare @empid int, @empname varchar(60), @empsal
decimal(18,0);
select @empid=k.Emp_ID from inserted k;
select @empname=k.Emp_Name from inserted k;
select @empsal=k.Emp_Sal from inserted k;
set @Emp_perform_action='Inserted Record -- After Insert
Trigger.'; insert into Employee_performance (Emp_ID,Emp_Name,Emp_Sal,Emp_perform,Time_perform)
values (@empid,@empname,@empsal,@Emp_perform_action,getdate());
PRINT 'AFTER INSERT trigger fired.'
Now
we will write a query of insert :-
insert
into Employee_details(Emp_Name,Emp_Sal)values ('Ravindra',3600);
Now
we will fetch data from both table and output is below----
select * from Employee_details
select
* from Employee_performance
ii)Use After Update Trigger:-
Here we will update trigger with the help of sql query we will
update some field change in a table:-
v Create trigger on
table Employee_details for Update statement
CREATE TRIGGER aftertriggerUpdate ON dbo.Employee_details
FOR UPDATE
AS
declare @empid int, @empname varchar(60), @empsal decimal(18,0),
@Emp_perform varchar(150);
select @empid=k.Emp_ID from inserted k;
select @empname=k.Emp_Name from inserted k;
select @empsal=k.Emp_Sal from inserted k; if update(Emp_Name)
set @Emp_perform=After Update Trigger Emp_details Record.';
if update (Emp_Sal)
set @Emp_details= After Update Trigger Emp_details Record.';
insert intoEmployee_performance(Emp_ID,Emp_Name,Emp_Sal,Emp_perform,
Time_perform)
values (@empid,@empname,@empsal,@ Emp_perform,getdate());
PRINT 'AFTER UPDATE trigger fired.'
We will upadte data in Employee_details….
v update
Employee_details set Emp_Name='Give Employee Name' Where Emp_ID =”Give employee
id where updte name”;
iii)After Delete Trigger-Here we will learn how to delete trigger use with the help of
sql query ,I will delete some record in a table and show a record .
v Create trigger on
table Employee_details for Delete statement
CREATE TRIGGER aftertriggerdelete ON
dbo.Employee_details
FOR DELETE
AS
declare @empid int, @empname
varchar(60), @empsal decimal(18,0), @audit_action varchar(150); select
@empid=e.Emp_ID FROM deleted e;
select @empname=e.Emp_Name from
deleted e;
select @empsal=e.Emp_Sal from deleted
e;
select @Emp_perform=After Delete
Trigger in a employee_details.';
insert into Employee_performance
(Emp_ID,Emp_Name,Emp_Sal,Emp_perform, Time_perform)
values (@empid,@empname,@empsal,@ Emp_perform,getdate());
PRINT 'AFTER DELETE TRIGGER fired.'
-We will delete data in Employee_details table….
v DELETE FROM
Employee_details where emp_id = “here give a id which record deleted”
-We will select data from both the tables …
select * from Employee_details
select * from Employee_performance
Instead Of Insert Trigger:-
Here we will write a query Instead of insert trigger
statement in sql server
Now create insert trigger before we discuss instead of insert
trigger we insert the data in a virtual table prior checking the constraints.
Use of instead of Insert Trigger:-
Now create a new table of customer For Instead of Insert Trigger..
CREATE
TABLE [dbo].[Customer]([Cus_id] CHAR(20) PRIMARY KEY,[Cus_Name] VARCHAR(60)
)
GO
INSERT
INTO [dbo].[Customer] VALUES('001','Mahi')
GO
Now Consider to insert a query in sql database :-
INSERT INTO [dbo].[Customer] (cus_Name)
VALUES('Sobhagya')
Here enter the data in the column (name). We will be excecuting
NULL values in the column (id). I have a primary key column in the (cus_id)
column. There is no any permit for NULL in a primary because primary key constraint
is violated.
If we will use of After Trigger so that constraints are proper
checkingto insert the data in the table. In case of primary key
constraint is violated if you have not put data into virtual table. Data is
inserted into the virtual table proper constraint check.
Syntax With Example:-
Syntax With Example:-
CREATE TRIGGER INSTEAD_TRIGG ON dbo.Employee_details
INSTEAD OF Insert
AS
declare @@empid int, @empname varchar(55), @empsal decimal(10,2), @emp_perform varchar(150);
select @empid =k.empid from inserted k;
select @empname=k.empname from inserted k;
select @empsal=k.empsal from inserted k;
SET @emp_perform=' Instead Of Insert Trigger.';
BEGIN
BEGIN TRAN
SET NOCOUNT ON
if(@emp_sal>=2050)
begin
RAISERROR('Cannot Insert where salary < 2050',16,1); ROLLBACK; end
else begin Insert into Employee_details empname,empsal) values (@empname,@empsal); Insert into Employee_performance (empid,empname,empsal,emp_perform,Time_perform) values(@@identity,@empname,@empsal,@emp_perform,getdate());
COMMIT;
PRINT 'Record Inserted -- Instead Of Insert Trigger.'
END



