Sei sulla pagina 1di 5

SQL Test – 7

Time: 1 Hour

Total Marks: 35
PART - 1

1. What is a constraint? Types? Describe with syntax.

 constraints are used to limit the type of date go into the table
Primary key
Fireign key
Unique key
Not null
Check
Create table
(empno int not null)
Create table (p_id int ,ename varchar2(10), primary key(p_id)

2. What is on delete cascade,ondelete set null?

 on delete cascade=Use the ON DELETE CASCADE option to specify whether you want rows
deleted in a child table when corresponding rows are deleted in the parent table
On delete null =it used to set the child records to null

3. What are the constraints which can be enabled and disabled? How do u enable and
disable them? Example

 ALTER TABLE emp


enable CONSTRAINT emp__pk;
ALTER TABLE emp
drop CONSTRAINT emp_pk;

4. What are levels of constraints? how do you define?

5. Can you drop a constraint? How?

 yes

6. Can we add a constraint after creating a table? How?

 yes we can add


Constraint
Add constraint cons_name (column)

7. How many primary, foreign, unique, check constraints we can have on a table?
SQL Test – 7
Time: 1 Hour

Total Marks: 35

 many

8. Display the total Salary of emps belongs to Grade 3.in descending order

 select sum(sal) from emp e,salgrade g


where e.sal between g.losal and g.hisal
and g.grade=3;

9. List the emp name, sal and deptno for each emp who earns a sal greater than the avg
for their dept order by deptno


select * from emp e
where sal > (select avg(sal) from emp where e.deptno = deptno );

10.Find out all the emps who earn highest sal in each job type. Sort in des order of sal

 select * from emp where sal in (select max(sal) from emp group by job)
Order by sal desc

11. List out the name, job, sal of the emps in the department with the highest avg sal.


select * from emp where deptno in
(select deptno from emp e
having avg(sal) =(select max(avg(sal)) from emp group by deptno)
group by deptno);

12. Find all the emps who earn minimum sal for each job wise in asc order of sal

 select * from emp where sal in(select min(sal) from emp group by job)order by sal
13.List the dept in asc order of job and the desc order of emps print empno, ename

select * from emp e order by e.job asc,e.empno desc ;

14.write a select statement to fetch constraint names?

 select* form all_constraints

15.how to drop a primary key constraint followed by its foreign key?

 ALTER TABLE emp


drop CONSTRAINT fk_ename
SQL Test – 7
Time: 1 Hour

Total Marks: 35
PART -2
1) List the managers name who is having max no of emps working under him.

select m.ename,count(*) from emp w,emp m


where w.mgr = m.empno
group by m.ename
having count(*) = (select max(count(*)) from emp group by mgr);

2) List the emps with hiredate in format June 4, 1988.

 select to_char(hiredate,’mon dd,yyyy’) from emp


;
3) Print a list of emp’s listing ‘just salary’ if salary is more than 1500, ‘On target’ if salary is 1500
and ‘Below 1500’ if salary is less than 1500.


select empno,ename,sal,job,
case
when sal = 1500 then 'ON TARGET'
when sal < 1500 then 'BELOW 1500'
when sal > 1500 then 'JUST SALARY'
else 'nothing'
end "REVISED SALARY"
from emp;

4) Write a query to return the day of the week for any date entered in format ‘DD-MM-YY’

1 select to_char(to_date('& s','dd-mm-yy'),'day') from dual

5) Print the details of all the emps who are sub-ordinates to BLAKE.

select * from emp where mgr in (select empno from emp where ename = 'BLAKE');

6) Find out how many managers are there in the company


select * from emp where empno in (select mgr from emp );

select count(*) from emp where empno in (select mgr from emp);

7) Change the last name of employee 3 to Drexler.


SQL Test – 7
Time: 1 Hour

Total Marks: 35
UPDATE employees
SET lastname = ’Drexler’
WHERE id = 3;

8) Change the salary to 1000 for all employees with a salary less than 900.

 UPDATE employees
SET salary = 1000
WHERE salary < 900
9) Empty the entire table.

 delete table emp

10) Confirm that the table is empty.

 delete table emp


commit
Commit;
11) Confirm that both the DEPT and EMP tables are stored in the data dictionary.

 commit;
Select * from emp

12) Drop the EMP table.

;drop table emp;

13) Rename the EMPLOYEES2 table as EMP.

 ALTER TABLE TABLE_NAME


RENAME TO new_table_name;

14)Insert into emp select * from employee2

INSERT INTO EMP AS SELET * FROM EMPLOYEE2


15)Add a comment to the DEPT and EMP table definitions describing the tables. Confirm your
additions in the data dictionary.

 SELECT table_name
FROM user_tables
WHERE table_name IN ('DEPT', 'EMP');

16)Drop the FIRST_NAME column from the EMP table. Confirm your modification by checking
the description of the table.
SQL Test – 7
Time: 1 Hour

Total Marks: 35
 ALTER TABLE emp
DROP COLUMN first_name
Desc emp
17) In the EMP table, mark the DEPT_ID column in the EMP table as UNUSED. Confirm your
modification by checking the description of the table.
ALTER TABLE emp
SET UNUSED (dept_id);

18) Drop all the UNUSED columns from the EMP table. Confirm your modification by checking
the description of the table.
 ALTER TABLE emp
DROP UNUSED COLUMNS

19. Add a table-level PRIMARY KEY constraint to the EMP table on the ID column. The
constraint should be named at creation. Name the constraint my_emp_id_pk.
Hint: The constraint is enabled as soon as the ALTER TABLE command executes
successfully.

 ALTER TABLE emp


ADD CONSTRAINT my_emp_id_pk PRIMARY KEY (id);

20. Create a PRIMARY KEY constraint to the DEPT table using the ID column. The constraint
should be named at creation. Name the constraint my_dept_id_pk.
Hint: The constraint is enabled as soon as the ALTER TABLE command executes
successfully.

create table dept(ename varchar2(10) primary key)


Alter table dept
Alter table dept
Add primary key (

ALTER TABLE dept


ADD CONSTRAINT my_deptid_pk PRIMARY KEY(id);

21. Add a column DEPT_ID to the EMP table. Add a foreign key reference on the EMP table
that ensures that the employee is not assigned to a nonexistent department. Name the constraint
my_emp_dept_id_fk.

ALTER TABLE emp


ADD (dept_id NUMBER(7));
ALTER TABLE emp
ADD CONSTRAINT my_emp_dept_id_fk
FOREIGN KEY (dept_id) REFERENCES dept(id);)

Potrebbero piacerti anche