SQL Worksheet Answer
SQL Worksheet Answer
use ABC_CAMPANY
create table customer(cid varchar(10)not null,cname varchar(30),sex char(6)default 'f' check(sex='f' or sex='m'),ctype
char(20),address char(10),salary money check(salary>1000 and salary<5000))
create table product(pid varchar(10)not null,pname varchar(30),pdate datetime default getdate(),quantity char(5),price
money)
create table orderdetails(cid varchar(10)not null,pid varchar(10)not null,qty char(12))
alter table customer add constraint pk_cid primary key(cid)
alter table product add constraint pk_pid primary key(pid)
alter table orderdetails add constraint fk_cid foreign key(cid)references customer(cid)on delete cascade on update
cascade
alter table orderdetails add constraint fk_pid foreign key(pid)references product(pid)on delete cascade on update cascade
select * from customer where cid not in(select cid from orderdetails)
select * from product where pid in(select pid from orderdetails group by pid having COUNT(pid)>=2)
select GETDATE()as pdate,pname from product
select * from customer where cid in(select cid from orderdetails)
select *from product where pname like 'p%'
select UPPER(cname) from customer
select salary,sum(salary) as salary from customer group by salary having sum(salary)>=5000
select ctype,address,COUNT(address) as count from customer group by address,ctype having COUNT(address)>=2
update customer set salary=salary+(salary*0.25) where salary>=3000
alter table customer drop constraint CK__customer__salary__00551192
select pname,quantity,price,cname from customer,product,orderdetails where customer.cid=orderdetails.cid and
product.pid=orderdetails.pid
select customer.cid,cname,product.pdate,pname, price from customer,product,orderdetails where
customer.cid=orderdetails.cid and product.pid=orderdetails.pid and price>300
delete from product where price<=400
select top 3 *from customer order by cname desc
sp_renamedb ABC_CAMPANY,' ABC '
backup database ABC to disk='e:\bam.bak'
drop database ABC
restore database ABC from disk='e:\bam.bak'
create database college
use college
create table student(sid varchar(10)primary key,sname varchar(15),sex char(6)
check(sex='male' or sex='female'),section char(10),age int check(age>20),salary money check(salary>2000)
,dname varchar(10) check(dname in('ict','accounting','electrical')))
select*from student
select *from course
select * from grade_report
select COUNT(*) from sys.databases