Вы находитесь на странице: 1из 18

SOL COMMAND

create database school


use school
create table student(id int,name
varchar(20),address varchar(30),ph_no
bigint)
select * from student
sp_help student
insert into student
values(1,'danish','kandivali',76543234)
insert into student
values(2,'gautam','dadar',76544545)
insert into student
values(3,'ajay','borivali',76763424)
insert into student
values(4,'ram','virar',76543987)
insert into student
values(5,'gopal','khar',98763234)
insert into student
values(5,'krish','mira road',87632451)
select * from student
select * from student where id=4
select * from student where id<3
select * from student where id>3
select * from student where id between 4
and 5
select * from student where id in(1,3,4)
select * from student where name='danish'
and address='kandivali'
select * from student where name between
'a' and 'h'
insert into
student(id,name,address)values(1,'shariqu
e','malad')
select * from student
select * from student where ph_no is null
select * from student where ph_no is not
null

 --like operator
select * from student
select * from student where name like
'v%'
select * from student where address like
'%r'
select * from student where name like
'_i%'
select * from student where name like
'____'
select name + ' ' + address as
'name\address' from student
select convert(varchar(5),rollno) + ' '
+ name from student
select *,fees*0.05 as 'tax' from student

select distinct address from student


select top 3 * from student

 --how to update existing data


select * from student
update student set
name='raj',address='mahim',fees=4567
where rollno=6
 --how to delete existing records from
table
delete from student where rollno=6

 --how to add new column in existing


table
alter table student add PhoneNo
numeric(10)
 --how to drom column
alter table student drop column Phoneno

 --Constraint
--1 primary key
--2 foreign key
--3 unique
--4 check
 --1 primary key:
--(1) it does not allow duplicate value
--(2) it does not allow null value
--(3) we can define only one primary key
in a table
create table Author(Authorid int primary
key,Name varchar(20),Address
varchar(20))\
insert into Author
values(4,'Rajesh','Kandivali')
select * from Author
 --foreign key:-it accept only
references values from references
table's column
create table book(booid int primary
key,Name varchar(20),Authorid int foreign
key references Author(Authorid),Price
int,pages int)
select * from book
drop table book
insert into book values(106,'Core
java',30,600,510)
create table Dealar(
dealarcode int identity(200,1),
Authorid int foreign key references
Author(Authorid),
Bookid int foreign key references
Book(booid),
dealarname varchar(20),
Address varchar(30)
)

select * from Dealar


insert into Dealar
values(3,102,'pankaj','kandivali')
 --1 unique:
--(1) it does not allow duplicate value
--(2) it allow only one null value
--(3) we can define multiple unique in
a table
create table abc(id int null unique ,Name
varchar(20)not null,Phoneno
numeric(10)unique)
insert into abc
values(3,'Ramesh',9087898767)
drop table abc
create table abc(name varchar(20),Age int
check(age between 1 and 100))
insert into abc values('raj',200)
 --Aggregate function
--1 sum
--2 max
--3 min
--4 avg
--5 count
select * from author
select * from book
select * from Dealar

select sum(price) from book


select authorid,SUM(price) from book
group by authorid
select authorid,SUM(price) from book
group by authorid having authorid=1
select MAX(price) from book
select Min(price) from book
select avg(price) from book
select count(*) from book
 --date function
select GETDATE()
select DAY('03/06/2019')
select month(getdate())
select year(getdate())
select DATEPART(dd,getdate())
select DATEPART(MM,getdate())
select DATEPART(YY,getdate())
select DATEPART(dw,getdate())
select
DATEDIFF(yy,'06/09/2000',GETDATE())
select
DATEDIFF(MM,'06/09/2000',GETDATE())
select
DATEDIFF(dd,'06/09/2000',GETDATE())
select DATENAME(dw,getdate())
select DATENAME(MM,getdate())
select DATEADD(DD,7,GETDATE())
select DATEADD(DD,-7,GETDATE())
 --string function
select len('raj')
select UPPER('raj')
select LOWER('RAJ')
select ASCII('A')
select char(65)
select substring('raj computers
academy',4,21)
select LEN(ltrim(' raj'))
select rtrim('raj ')
select CHARINDEX('j','raj computer')
 --union: is used to combine rows from
multiple select statement,but
--each select statement number of column
and data type must be same.
create table savingaccount(accno int,Name
varchar(20))
insert into savingaccount
values(3,'arun')

create table currentaccount(accno


int,Name varchar(20))
insert into currentaccount
values(3,'arun')
select * from savingaccount
union
select * from currentaccount
select * from savingaccount
union all
select * from currentaccount

 --sub query means query within query

select * from book


select * from author
select * from Dealar
select * from book where Authorid
in(select authorid from Author where
Name='Amar')
select * from book where Authorid not
in(select authorid from Author where
Name='Amar')
select MAX(price) from book where price
not in(select MAX(price) from book)
select min(price) from book where price
in(select top 4 price from book order by
price desc)
select distinct bookid from Dealar
select distinct authorid from book where
booid not in(100,101,102)
select * from author where Authorid
in(2,3)
 --subquery
select * from author where Authorid
in(select distinct authorid from book
where booid not in(select distinct bookid
from Dealar ))
select name from Author where address
in(select Address from author where
Address='kandivali')
select distinct address from dealar where
authorid in(Select Authorid from author )
select * from book
select * from author
select * from Dealar
select Authorid from Author where
Name='sumit'
select booid from book where Authorid
in(2)
select count(bookid) from Dealar where
bookid in(102,103)
select count(bookid) from Dealar where
bookid in(select booid from book where
Authorid in(select Authorid from Author
where Name='sumit'))
 --join is used to combine columns from
multiple table based on common fields.
--there are 3 types of join
--1 inner join
--2 outer join
--3 self join
 --inner join returns only matching
rows from each table based on common
field.
--for example
select * from book
select * from author
select
a.authorid,a.name,a.address,b.Name,b.pric
e,b.pages from Author as a inner join
book as b
on a.authorid=b.authorid

 --outer join
--left join
--right join
--full join
--left join returns all data from left
table and only matching data from right
table
select
a.authorid,a.name,a.address,b.Name,b.pric
e,b.pages from Author as a left join
book as b
on a.authorid=b.authorid

 --right join returns all data from


right table and only matching data
from left table.
select
a.authorid,a.name,a.address,b.Name,b.pric
e,b.pages from book as b right join
author as a
on a.authorid=b.authorid

 --full join returns all data from each


table
select
a.authorid,a.name,a.address,b.Name,b.pric
e,b.pages from book as b full join
author as a
on a.authorid=b.authorid
select * from emp
create table emp(empid int,Name
varchar(20),department varchar(20),Mgrid
int)
insert into emp
values(4,'Smit','Account',4)
insert into
emp(empid,name,department)values(6,'Nirav
','Account')
select e.empid,e.name,e1.department from
emp as e inner join emp as e1
on e.empid=e1.Mgrid

 --view
select * from book
create view book_view
as
select * from book

select * from book_view


insert into book_view
values(106,'C#',2,500,700)
update book_view set price=400 where
booid=106
delete from book_view where booid=106
create view join_view
as
select
a.authorid,a.name,a.address,b.price,b.pag
es from Author as a inner join book as b
on a.authorid=b.authorid

select * from join_view where Authorid=1


 --store procedure
 create procedure sum1
as
declare @n1 int,@n2 int,@n3 int
set @n1=10
set @n2=30
set @n3=@n1+@n2
print(@n3)
exec sum1

 create proc sum2


@n1 int,@n2 int
as
declare @n3 int
set @n3=@n1+@n2
select 'sum is:'+convert(varchar(5),@n3)
exec sum2 340,56

 create proc Interest


@p decimal,@r decimal,@t decimal
as
declare @si decimal
set @si=(@p*@r*@t)/100
select 'simple interest
is:'+convert(varchar(5),@si)
exec Interest 6000,2,3

 select * from book


alter proc Search
@name varchar(30)
as
select * from book where Name=@name
execute search 'java'

 create proc mypro


@a int ,@b int
as
declare @c int
set @c=@a+@b
print(@c)

execute mypro 20,43

 alter proc ReverseNumber


@a int
as
declare @b int ,@c int ,@d int,@e int
set @b=@a/100
set @c=@a%100
set @d=@c/10
set @e=@c%10
select 'Reverse Number
is:'+convert(varchar(5),convert(varchar(4
),@e)+convert(varchar(4),@d)+convert(varc
har(4),@b))

exec ReverseNumber 236


select * from Author
create procedure Add_at
@id int,@na varchar(20),@add varchar(20)
as
insert into Author values(@id,@na,@add)
execute Add_at 5,'Amarjeet','andheri’

 create proc update_at


@id int,@na varchar(20),@add varchar(20)
as
update Author set Name=@na,Address=@add
where Authorid=@id
exec update_at 5,'vinod','khar'
create proc Even
@n1 int
as
if(@n1%2=0)
begin
print 'N1 is even number'
end
else
begin
print 'N1 is odd number'
end
exec Even 2

 create proc Login1


@user varchar(20),@pass varchar(20)
as
if(@user='gautam' and
@pass='vishwakarma')
begin
print 'Welcome: Gautam'
end
else
begin
print 'invalid user name or password'
end
exec Login1 'gautam','vishwakarma1'

 create proc while1


as
declare @a int
set @a=1
while(@a<=10)
begin
print(@a)
set @a=@a+1
end
execute while1

 create table Studentz(id int,Name


varchar(20),Address varchar(20))
create procedure InsertData
@id int,@na varchar(20),@add varchar(20)
as
insert into Studentz values(@id,@na,@add)
execute InsertData 5,'Sahir','Dadar'
select * from Studentz

 create procedure EOO


@num int
as
if(@num%2=0)
begin
print 'The number is even'
end
else
begin
print 'The number is odd'
end
exec EOO 5

 create procedure Find


@num int
as
if(@num>=0)
begin
print 'The number is positive'
end
else
begin
print 'The number is negative'
end
exec Find 5

 create Procedure WhileLoop


@n int
as
declare @num int
set @num=1
while(@num<=@n)
begin
print(@num)
set @num=@num+1
end
exec WhileLoop 10

 alter procedure TableNumber


@n int
as
declare @i int,@sum int
set @i=1
set @sum=0
while(@i<=10)
begin
set @sum=@i*@n
print
convert(varchar(2),@n)+'*'+convert(varcha
r(2),@i)+'='+convert(varchar(2),@sum)
set @i=@i+1
end
exec TableNumber 5
 --trigger
select * from book
 alter trigger insert_tri
on book
for insert
as
if(select price from inserted)>1000
begin
rollback transaction
print 'pls enter price<1000'
end

 create trigger update_tri


on book
for update
as
select * from inserted
select * from deleted

select * from book


update book set
Name='Android',Price=240,pages=300 where
booid=107

insert into book


values(108,'php',1,2000,1500)

 create trigger delete_tri


on book
for delete
as
select * from deleted

delete from book where booid=107

 create table stu(id int,Name


varchar(20),Address
varchar(30),Department varchar(30))
insert into stu
values(4,'Rajesh','Virar','IT')
select * from stu

 create trigger Inserted


on stu
for insert
as
select * from inserted

insert into stu values(5,'Prakash','Mira


road','account')

 create trigger updated


on stu
for update
as
select * from inserted
select * from deleted

update stu set Department='IT' where id=5

 create trigger deleted


on stu
for delete
as
select * from deleted

delete from stu where id=5


select * from stu