Uploaded by STEP UP

DATABASE SYSTEMS

advertisement
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Using of table
use azeem
 Retrieve all data of student
select * from stu
 Retrieve reg no and first name of student
select reg_no, first_name from stu
 Retrieve reg no and first name of student whose reg no is …
select reg_no, first_name from stu where reg_no = ' '
 Retrieve reg no and first name of student whose reg no is 1005
select reg_no, first_name from stu where reg_no = '1005'
azeemalkhairi@hotmail.com
Page 1
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Retrieve reg no and first name of student whose first name is marya
select reg_no, first_name from stu where first_name = 'marya'
 Retrieve data of student whose reg no is 1004 and first name is marya
select * from stu where reg_no = '1004' and first_name = 'marya'
 Retrieve data of student whose reg no is 1004 and first name is Ali
select * from stu where reg_no = '1004' and first_name = 'Ali'
 Retrieve data of student whose reg no is 1001 or first name is marya
select * from stu where reg_no = '1001' or first_name = 'marya'
 Retrieve data of student whose reg no is 1004 or first name is Ali
select * from stu where reg_no = '1004' or first_name = 'Ali'
 Retrieve data of student whose reg no is … or first name is marya
select * from stu where reg_no = ' ' or first_name = 'marya'
azeemalkhairi@hotmail.com
Page 2
DATABASE SYSTEMS

MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
Retrieve reg no and show as stu and first name as fullname of student
select reg_no as stu, first_name as fullname from stu

Retrieve reg no and show as stu and first name as fullname of student
order on base of first name ascending
select reg_no as stu, first_name as fullname from stu order by first_name asc

Retrieve reg no and show as stu and first name as fullname of student
order on base of first name descending
select reg_no as stu, first_name as fullname from stu order by first_name
desc
azeemalkhairi@hotmail.com
Page 3
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Retrieve fname and lname (and show as full name)
select first_name +' '+ last_name as [full name] from stu
 Retrieve top 20 records of student
select top 20 percent * from stu
 Retrive reg no whose assets is greater than 200000 using relational
operator
select reg_no from stu where assets > 200000
 Retrive record of student whose age is greater than 20 and less than
40 using relational operator
select * from stu where age > 20 and age < 40
azeemalkhairi@hotmail.com
Page 4
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Retrive record of student whose age is greater than 20 and less than
40 without using relational operator
select * from stu where age between 20 and 40

Retrive record of student whose second last letter of lname is a
select * from stu where last_name like '%a[^ ]'

Retrive record of student whose lname is ali and hussain
select * from stu where last_name in ('ali', 'hussain')

Retrive age sum of student show as agesumition
select sum(age) as agesumition from stu

Retrive minimum age show as ageminimum
select min(age) as ageminimum from stu

Retrive maximum age show as agemaximum
select max(age) as agemaximum from stu
azeemalkhairi@hotmail.com
Page 5
DATABASE SYSTEMS

MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
Retrive average age of student show as ageaverage
select avg(age) as ageaverage from stu

Display total number of records
select count(*) from stu

Display total number of records and show as totalcount
select count(*) as totalcount from stu

Retrive maximum assets
select max(assets) from stu

Retrive maximum assets whose regno is greater than equal to 1002
and lessthan equal to 1007
select max(assets) from stu where reg_no >= 1002 and reg_no <=1007
 Retrive number of gender and sum of assets in ascending order
select gender, sum(assets) from stu group by gender order by gender asc
 Retrive number of gender and sum of assets in descending order
select gender, sum(assets) from stu group by gender order by gender desc
azeemalkhairi@hotmail.com
Page 6
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Retrive age and asset and show as total amount
select age + assets as [total amount] from stu
 Retrieve gender of student
select gender from stu
 Retrive distinct gender of student
select distinct gender from stu
 Retrive total number of gender
select count(gender) from stu
azeemalkhairi@hotmail.com
Page 7
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Retrive distinct record of student
select distinct * from stu
 Retrive distinct number of gender
select count (distinct gender) from stu
 Retrive data of student whose fname length is lessthan 4
select * from stu where len(first_name)< 4
 Retrive regno and fname from student whose assets is less than
average assets
select reg_no, first_name from stu where assets < (select avg (assets) from
stu)
azeemalkhairi@hotmail.com
Page 8
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Delete
delete from stu where first_name = 'ali'
delete from stu where first_name is null
 Update
update stu set first_name = 'karan' where reg_no = '1010'
update stu set gender = 'm' where reg_no = '1010'
 Update and replace
update stu set gender = 'f' where gender = 'm'
azeemalkhairi@hotmail.com
Page 9
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 Alter query
alter table stu add fname char(20)null
alter table stu alter column fname varchar(25)
alter table stu drop column fname
alter table stu alter column reg_no int not null
alter table stu add constraint reg_pk primary key (reg_no)
azeemalkhairi@hotmail.com
Page 10
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
 View create
create view stuazeem as select reg_no, first_name, age from stu
select * from stuazeem
 Change style of date of birth
alter table stu add dob datetime
update stu set DOB='12/12/12'
select reg_no, first_name, convert(varchar, dob, 100) as birthdate from stu
azeemalkhairi@hotmail.com
Page 11
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
select reg_no, first_name, convert(varchar, dob, 101) as birthdate from stu
select reg_no, first_name, convert(varchar, dob, 103) as birthdate from stu
 Inner join (conditional join)
select s.reg, s.name, e.education from stu s, edu e where s.reg = e.reg and
s.reg = 103
select s.reg, s.name, e.edu_no from stu s,edu e where s.reg=e.reg
azeemalkhairi@hotmail.com
Page 12
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
select s.reg, s.name, e.edu_no from stu s,edu e where s.reg=e.reg and
e.reg='102'
select s.reg, s.name, e.edu_no from stu s,edu e where s.reg=e.reg and
s.reg='104'
create database stucourse_2848
use stucourse_2848
create table stu
(
reg char(12) primary key,
name char(20) null,
age int,
discipline char(4)
)
create table course
(
course_no char(12) primary key,
descc char(20) null,
credits char(6)
)
create table allocate
(
reg char(12) foreign key references stu(reg),
course_no char(12) foreign key references course(course_no),
semester int,
sec char(1)
constraint PK_allot primary key (reg, course_no, semester)
)
insert into stu values ('101', 'ali', '18', 'mcs')
insert into stu values ('102', 'rahat', '20', 'mit')
insert into stu values ('103', 'imran', '22', 'bcs')
insert into course values ('1', 'cs103', '3(3-0)')
insert into course values ('2', 'cs104', '3(3-1)')
insert into course values ('3', 'cs105', '4(3-0)')
azeemalkhairi@hotmail.com
Page 13
DATABASE SYSTEMS
MUHAMMAD AZEEM MUSHTAQ (12-ARID-2848)
insert into course values ('4', 'cs108', '4(3-0)')
insert into course values ('5', 'cs107', '4(3-0)')
insert into allocate values ('101', '1', '1', 'a')
insert into allocate values ('101', '2', '1', 'a')
insert into allocate values ('101', '3', '1', 'a')
insert into allocate values ('102', '1', '2', 'b')
insert into allocate values ('102', '2', '2', 'b')
insert into allocate values ('102', '4', '2', 'b')
insert into allocate values ('103', '1', '3', 'c')
insert into allocate values ('103', '2', '3', 'c')
insert into allocate values ('103', '5', '3', 'c')
select s.reg, s.name, c.course_no, a.semester,a.sec, c.descc from stu
s,course c, allocate a where s.reg = a.reg and c.course_no = a.course_no and
s.reg = '102'
use stucourse_2848
create table semester
(
sem_no int primary key,
dess char(7),
start_date datetime,
end_date datetime
)
insert into semester values ('01', 'fall 09', '10/12/12', '10/12/14')
insert into semester values ('02', 'spring', '08/02/13', '08/02/15')
insert into semester values ('03', 'fall 08', '07/11/10', '07/11/13')
alter table allocate add sem_no int
update allocate set sem_no ='01' where reg = '101'
update allocate set sem_no ='02' where reg = '102'
update allocate set sem_no ='03' where reg = '103'
alter table allocate alter column sem_no int not null
alter table allocate drop constraint PK_allot
alter table allocate add constraint FK_all123 foreign key (sem_no) references
semester
alter table allocate add constraint pk_allot primary key(reg, course_no,
semester, sem_no)
select s.reg, s.name, c.course_no, a.semester, a.sec, c.descc from stu
s,course c,allocate a, semester m where s.reg = a.reg and c.course_no =
a.course_no and m.sem_no = a.sem_no and s.reg = '102'
alter table stu add constraint def_age default(0) for age
select left(credits, 1) from course
azeemalkhairi@hotmail.com
Page 14
Download