Sei sulla pagina 1di 7

SELECT DISTINCT(TYPE),TYPE_DESC FROM SYS.

OBJECTS; IF OBJECT_ID('employee', 'U') IS NOT NULL begin print'Table dose not exist' DROP TABLE person end sp_addtype Saltype,int,null sp_addtype Commtype,int,null sp_addtype Depttype,int,null sp_addtype Licen_type,int,null sp_addtype Agetype,int,'not null' drop type Ageype --Default create default locationDefault as 'Madras' sp_bindefault locationDefault,"Dept.location" drop default salaryDefault --Rule create rule ageRule as @age between 20 and 50 sp_bindrule ageRule,"person.age" --1. Create table Dept create table Dept ( deptno Depttype not null primary key, dname char(30) null, location char(30) null ); insert dept(deptno,dname) values (101,'Accounts') select * from dept delete from person --Create table Employee create table employee ( emp_id int not null primary key, emp_name char(30) null, job char(15) null, manager int null, deptno Depttype null FOREIGN KEY REFERENCES Dept(deptno), salary Saltype null, comm commtype null default 0 ) insert employee(emp_id,emp_name,job,manager,deptno,salary) values (1002,'Raja',' Asst.Manager',1,101,35000) select * from employee --Create Table Person create table person ( name char(30) not null, age agetype not null, license_no licen_type not null, soc_sec_no char(5) unique, )

insert person(name,age,license_no,soc_sec_no) values('Mani',21,987542,51) select * from person --3. Add a new column to the table employee called HIREDATE of the data type DA TETIME. alter table employee add hiredate datetime --4. Drop the unique constraint on SOC_SEC_NO column on the PERSONS table. alter table person drop constraint UQ__person__0DAF0CB0 --5. Add a default constraint to the MGR column of the employee table setting the default to 1000. create default mgrDefault as 1000 sp_bindefault mgrDefault,"employee.manager" drop default mgrDefault --6. Add a primary constraint on SOC_SEC_NO. alter table person alter column SOC_SEC_NO char(5) not null alter table person add PRIMARY KEY (SOC_SEC_NO) --7. Add a CITY column to the persons table and define a default constraint f or it as Madras alter table person add city char(15) default 'Madras' --8. Create a default DEF_AGE with the value 21. Bind it to the UDD AGETYPE. create default ageDefault as 21 sp_bindefault ageDefault,"agetype" --9. Create a non-clustered index SOC_SEC_IND on the combination of LICEN_NO and SOC_SEC_NO columns of the persons table. create nonclustered index SOC_SEC_IND on person(LICENSE_NO ,SOC_SEC_NO) --10. View the details of the UDDs and TABLEs you have created using appropria te system procedures. SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES SELECT DOMAIN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE DOMAIN_NAME IS NOT NULL sp_help employee sp_help dept sp_help person --11. Create a rule SALRULE which doesnt allow employees to have a salary great er than Rs. 10,000. Create Rule salRule As @salary<=10000 sp_bindrule salRule,'employee.salary' --12. Display all the titles details in sorted order. select * from titles order by title asc --13. Display the full name(AU_FNAME + AU_LNAME) of authors not residing in C alifornia. select AU_FNAME,AU_LNAME from authors where city not in('California')

--14. Display the names of publishers who publish books on Psychology. Select pub_name from publishers where pub_id in(Select distinct pub_id from titl es where type='psychology') Select * from authors Select * from titles Select * from publishers --15. Display all the books which have received an advance equal to that paid to The Busy Executives Database Guide. --16. Display the authors of all books whose titles have the word COOK in them. SELECT au_lname,au_fname FROM AUTHORS WHERE au_id IN( (SELECT au_id FROM TITLEAUTHOR where TITLE_ID IN (SELECT TITLE_ID FROM TITLES WH ERE TITLE LIKE '%Cook%'))) --17. Create a table CPRICE which contains the following columns - pub_id, tit le_id, title, price, ytd_sales of all books which have a price less than the ave rage price of all books. --(HINT : Use SELECT .INTO clause.) DROP TABLE CPRICE SELECT P.PUB_ID,T.TITLE_ID,T.TITLE,T.PRICE,T.YTD_SALES INTO CPRICE FROM publishe rs P,TITLES T SELECT * FROM CPRICE --18. Insert the following records into the CPRICE table. INSERT INTO CPRICE VALUES(1389,'PC6543','Computing with an Abacus',1595.00,200); INSERT INTO CPRICE VALUES(0736,'PS2876','Hatred of vi explored',5251.00,154) --19. Change the price of Hatred of vi explored to be the same as that of Is Ange r the Enemy. UPDATE CPRICE SET PRICE=(SELECT PRICE FROM CPRICE WHERE TITLE='Is Anger the Enem Y') WHERE TITLE='Hatred of vi explored' --20. Which book has an advance greater than the maximum advance paid by the p ublisher Algodata Infosystems. select * from titles where advance>(select max(advance)from titles where pub_id= (Select pub_id from publishers where pub_name='Algodata Infosystems')) --21. List all the publishers in descending order of their names. SELECT E.FNAME,E.LNAME FROM EMPLOYEE E ORDER BY 2 DESC --22. List the sales, total sales, average sales, minimum sales, maximum sales for each title and also the grand total of sales. select s.title_id,sum(s.qty*t.price)as 'Total Sales',max(s.qty*t.price) as Maxim um,min(s.qty*t.price) as Minimum,avg(s.qty*t.price) as Average from sales s , titles t group by s.title_id order by t.title_id compute sum(s.qty*t.price) by t.title_id SELECT * FROM SALES SELECT * FROM TITLES --23. Display all employees who have a middle name/initial and are males. SELECT * FROM EMPLOYEE WHERE MINIT != '' --24. Given the following table and record structure, generate the required ou tput from a single query.

/*Schema for Table : ACC_INFO acc_no acc_name type amount 1 2 3 4 5 6 7 Inventory Cash Furniture Bank Loans Investments Sales DR DR CR CR DR CR CR 1000.00 5300.00 230.00 5500.00 100.50 1100.00 7600.50

The required output : ACC_NO 1 2 5 3 4 6 7 ACC_NAME Inventory Cash Loans Furniture Bank Investments Sales DEBIT 1000.00 5300.00 100.50 230.00 5500.00 1100.00 7600.50*/ CREDIT

create table account ( acc_no int, acc_name varchar(25), type char(3), amount int ); drop table account; insert insert insert insert insert insert insert account(acc_no,acc_name,type,amount) account(acc_no,acc_name,type,amount) account(acc_no,acc_name,type,amount) account(acc_no,acc_name,type,amount) account(acc_no,acc_name,type,amount) account(acc_no,acc_name,type,amount) account(acc_no,acc_name,type,amount) values(1,'Inventory','DR',1000.00) values(2,'Cash','DR',5300.00) values(3,'Furniture','CR',230.00) values(4,'Bank','CR',5500.00) values(5,'Loans','DR',100.50) values(6,'Investments','CR',1100.00) values(7,'Sales','CR',7600.50)

select acc_no,acc_name,amount as 'Debit','' as 'Credit' from account where type= 'DR' union select acc_no,acc_name,'' as 'Debit',amount as 'Credit' from account where type='CR' --25. Display all books in the descending order of price. SELECT TITLE,PRICE FROM TITLES ORDER BY 2 DESC -- 26. There are 31 system tables in the system catalog of the MASTER database, whereas only 18 system tables in the catalogs of all other databases. Write a q uery that will display the 13 system tables unique to the MASTER database. USE Test GO SELECT * FROM Master.sys.Tables --27. Display the USER ID, USER NAME, SERVER USER ID, SERVER USER NAME using a ppropriate system procedures. SELECT SYSTEM_USER as 'System User' Select USER_NAME() as 'User Name'

--28. Display a message showing todays date. (USE PRINT statement). print 'Today date is' print getdate() --29. Find out the day of the week that you were born and your age in months. print datepart(dw,18/01/1990) SELECT 'Month'=DATEDIFF(mm,'04/05/1989',GETDATE()) --30. Create a view called cities which lists the names of the authors and publis hers who live in the same city. CREATE VIEW CITY AS SELECT A.AU_FNAME,P.PUB_NAME,A.CITY FROM AUTHORS A JOIN PUBLISHERS P ON A.CITY= P.CITY SELECT * FROM CITY --31. Print the number of employees working in the company. SELECT COUNT(emp_id) AS 'No of Employee' FROM EMPLOYEE --32. Print a message whether there is an author called John Smith. select * from authors --33 --34 create procedure main_proc( @emp_id nvarchar(20)) as declare @out varchar(250) exec sub_proc @emp_id,@out output print @out drop procedure main_proc drop procedure sub_proc CREATE PROCEDURE sub_proc( @emp_id nvarchar(20), @out nvarchar(250) OUTPUT) AS if(select emp_id from employee where emp_id=@emp_id)!='' select @out='Salary Paid' else select @out='Salary Not Paid' exec main_proc 'JUHYDF345' exec main_proc 'PMA42628M' --35. Write a procedure up_dropobj which takes two arguments Objname and Type. Th e procedure --should drop the specified object. CREATE PROCEDURE up_dropobj(@objtype VARCHAR(30),@objname VARCHAR(30)) AS DECLARE @dropSQL VARCHAR(255) SELECT @dropSQL = 'drop ' SELECT @dropSQL = @dropSQL + @objtype+ ' ' + @objname EXEC (@dropSQL) up_dropobj @objtype = 'TABLE',@objname = 'TEMP'

create table temp ( id int ); drop procedure up_dropobj select * from temp --36. Write a procedure up_help which takes table name as a parameter and displ ays the column,index and constraint details. create procedure up_help( @tab_name nvarchar(20)) as exec sp_help @tab_name exec up_help @tab_name='employee' --37. Write a procedure emp_insert that accepts a record ( Empno, Empname, job, mgr, deptno,salary, comm, hiredate) as parameters and inserts this record into t he employee table. The hiredate parameter should be of char(10). create procedure emp_insert( @emp_id int, @emp_name char(30), @job char(15), @manager int, @deptno int, @salary money, @comm int, @hiredate char(10)) as insert into employee values(@emp_id,@emp_name,@job,@manager,@salary,@deptno,@com m,cast(@hiredate as datetime)) exec emp_insert 784,'Raja','Developer',34,5877,101,78,'10/10/2010' --38. Write a procedure emp_fetch that retrieves record from the employee table based on the Empno given. The hiredate column should be retrieved in the format of DD/MM/YYYY. create procedure emp_fetch( @emp_id int) as select emp_id,emp_name,job,manager,deptno,salary,comm,cast(hiredate as char(15)) from employee where emp_id=@emp_id exec emp_fetch 784 --39. Write a trigger to enforce uniqueness on deptno column of the dept table. create trigger ins_dept ON dept FOR INSERT AS IF ( SELECT COUNT(*)FROM dept d, inserted i WHERE d.deptno = i.deptno ) > 1 BEGIN ROLLBACK TRAN RAISERROR ('Duplicate department no.',16,-1) END ELSE PRINT 'Successfully Inserted into Department'

drop trigger ins_dept select * from dept insert dept(deptno,dname,location) values(103,'Developer','Mumbai') --40. Write a trigger on employee table which prevents the user from further updat ing the deptno columnn of the employee table with a value not present in dept table. create trigger update_employee ON employee FOR update AS IF update(deptno) BEGIN ROLLBACK TRAN RAISERROR ('Authendication Issue : Cannot be Update',16,-1) END update employee set deptno=101 where deptno=101 --41. Write a trigger deldept which will not allow you to delete a dept. record from the dept table as long as there is at least one employee of that dept. pres ent in the employee table. create trigger del_dept ON dept FOR DELETE AS IF(select count(*) from employee)>1 BEGIN ROLLBACK TRAN RAISERROR ('Employee Table contains Record set.',16,-1) END ELSE PRINT 'Successfully deleted from Department' select * from dept delete from employee select * from employee delete from dept where deptno=102 drop trigger del_dept --42. Write a trigger ins_sales that will while inserting a record in the sales tab le also update the YTD_SALES column of the titles table for that particular titl e. create trigger ins_sales ON sales after INSERT AS Declare @QTY int Declare @TITLE_ID varchar(20) set @QTY=0 set @TITLE_ID=NULL select @QTY=qty ,@title_id=title_id from inserted update TITLES set ytd_sales=ytd_sales+@QTY*10 where title_id=@title_id print 'Updated ytd_sales successfully' DROP TRIGGER INS_SALES Go insert into sales values(14,'7067','2007-12-12',4,'2','BU1032') select * from titles where title_id='BU1032'

Potrebbero piacerti anche