You are on page 1of 8

Lab Assignment–3

1. Create table emp which has the following attributes (employee table)
(@empno, ename, job, sal, deptno)
Where empno is primary key, ename is unique, job in (Prof, AP, and Lect), sal is not
NULL, and deptno is foreign key

CREATE TABLE EMPPP2(EMPNO NUMBER(10) PRIMARY KEY,ENAME


VARCHAR(35) UNIQUE,JOB VARCHAR2 (4)CHECK (JOB IN('JOB','AP','LECT')),SAL
NUMBER(6) ,DEPTNO NUMBER(2) REFERENCES DEPTTT2(DEPTNO));

2. Create table dept which has the following attributes (department table)
(@deptno, dname)
Where deptno is primary key, dname in (Acc, comp, elect)

CREATE TABLE DEPTTT2(DEPTNO NUMBER(2) PRIMARY KEY,DNAME


VARCHAR2(4) CHECK(DNAME IN('Acc','comp','elect')));

3. Create table S which has the following attributes (Salesperson table)


(@sno, sname, city)
Where sno is primary key

CREATE TABLE S2(SNO NUMBER(5)PRIMARY KEY,SNAME


VARCHAR2(10),CITY VARCHAR2 (15));

4. Create table P which has the following attributes (Part table)


(@pno, pname, color)
Where pno is primary key
CREATE TABLE P2(PNO NUMBER(5)PRIMARY KEY,PNAME
VARCHAR2(10),COLOR VARCHAR2 (15));
5. Create table J which has the following attributes (ProJect table)
(@jno, jname, city)
Where jno is primary key

CREATE TABLE J2(JNO NUMBER(5)PRIMARY KEY,JNAME VARCHAR2(10),CITY


VARCHAR2 (15));

6. Create table SPJ which has the following attributes


(@ (sno, pno, jno), qty)
Where combination of (sno, pno, jno) is primary key, also sno, pno, jno are foreign
keys

CREATE TABLE SPJ2(SNO NUMBER(5),PNO NUMBER(5),JNO


NUMBER(5),FOREIGN KEY (SNO) REFERENCES STABLE_01(SNO),FOREIGN KEY
(PNO) REFERENCES PTABLE_01(PNO),FOREIGN KEY (JNO) REFERENCES
JTABLE_01(JNO));

7. Insert appropriate records in above tables.


Lab Assignment–4
Q1) Check the structure of tables.
describe emppp2;

describe depttt2;

DESCRIBE S2;
DESCRIBE P2;
DESCRIBE J2;
DESCRIBE SPJ2;

Q2) Check the constraint name for applied constraints?


SELECT * FROM user_cons_columns WHERE table_name='DEPTTT2';

SELECT * FROM user_cons_columns WHERE table_name='EMPPP2';

SELECT * FROM user_cons_columns WHERE table_name='S2';

SELECT * FROM user_cons_columns WHERE table_name='P2';

SELECT * FROM user_cons_columns WHERE table_name='J2';

SELECT * FROM user_cons_columns WHERE table_name='SPJ2';


Q3) Drop the unique constraint on ENAME
ALTER TABLE emppp2 DROP U_ename WHERE TABLE_NAME=’emppp2’;

Q4) Drop the Foreign Key constraint on DEPTNO


ALTER TABLE depttt2 DROP F_KEY WHERE TABLE_NAME=’depttt2’;

Q5) Add Foreign Key constraint on DEPTNO


ALTER TABLE emppp2 ADD CONSTRAINT F_KEY FOREIGN KEY(deptno)
REFERENCES depttt2(deptno);

Q6) Change Data type of ENAME


ALTER TABLE emppp2 MODIFY (ename number(10));

Q7) Change width of DNAME


ALTER TABLE depttt2 MODIFY (dname number(15));

Q8) Add COMM column in EMP table


ALTER TABLE emppp2 ADD (comm varchar(15));

Q9) Drop CITY column from J table


ALTER TABLE J2 DROP COLUMN JCITY;

Q10) Create duplicate copy of EMP table


create table empdata as (select * from emppp2);

Q11) Copy structure of DEPT table in another table with different column names
create table depdata as (select * from depttt2);
alter table depdata rename column deptno to dno;
alter table depdata rename column dname to dename;
Q12) Change the name and job of the employee whose EMPNO =100
Update emppp2 set ename=’Swati’ ,job=’Lect’ where empno=100;

Q14) Drop DEPT Table


Drop table depppt2;

Q15) Drop duplicate table of EMP table


Drop table empdata;
Lab Assignment–5
1. List the total number of employees?

select count(*) as num_of_employees from emppp2;

2. List the total no of departments?

select count(*) as num_of_departments from depttt2;

3. List the total, maximum, & minimum salary where deptno is 30?

select sum(sal),min(sal),max(sal) from emppp2 where deptno=30;

4. Display the name of the employee getting maximum salary?

select ename,sal from emppp2 where sal=(select max(sal) from emppp2);

5. Display the total salary for each department?

select deptno,sum(sal) from emppp2 group by deptno;

6. Display the total salary for each job.

select job,sum(sal) from emppp2 group by job;


7. Display the total salary for each job within each department.

select deptno,job,sum(sal) from emppp2 group by deptno,job;

8. Display the average salary for each job in deptno 30.

select job,avg(sal) from emppp2 where deptno=30 group by job;

9. Display the total salary for each job excluding the ‘AP’ job.

select job,sum(sal) from emppp2 where not job='AP' group by job;

10. Display the average salary for each job in deptno 20, but only display those jobs
where total salary is greater than 2000 & display the output in descending order of
salary?

select job,avg(sal) from emppp2 where deptno=20 group by job having sum(sal)>2000 order
by sum(sal) desc;

11. Display the total no of employees for each department excluding the dno 10 &
display only those departments where more then five (1) employees work. Display the
output in descending order of total no of employees?

select deptno,count(*) from emppp2 where not deptno=10 group by deptno having
count(*)>1 order by count(*) desc;
13. Display total number of prof in univ.

select count(job)"no_of_prof" from emppp2 where job='Prof';

14. Display total number of prof in deptno 10.

select count(job)"no_of_Prof" from emppp2 where job='Prof' and deptno=10;

15. Display total number of prof in each dept.


select deptno,count(job)"no_of_Prof" from emppp2 where job='Prof' group by deptno;

16. Display total number of emp working in each job in each dept.
select deptno,job,count(*) from emppp2 group by deptno,job;

You might also like