Вы находитесь на странице: 1из 15

578 - BRCE POLYTECHNIC

DBMS & GUI LAB


Lab Manual
GAYATHRI M.
Assistant Professor, CSE
COMPANY SCHEMA WITH REFERENTIAL INTEGRITY CONSTRAINT
COMPANY DATABASE STATE
EXERCISE 3 & 4
DETAILS OF EMPLOYEES WORKING IN THE COMPANY
CREATE TABLE employee
(
fname VARCHAR2(20) NOT NULL,
minit CHAR,
lname VARCHAR2(20) NOT NULL,
ssn CHAR(9),
bdate DATE,
address VARCHAR2(50),
gender CHAR,
salary NUMBER(8,2),
super_ssn CHAR(9),
dno NUMBER,
CONSTRAINT emp_pk PRIMARY KEY (ssn),
CONSTRAINT emp_sssn_fk FOREIGN KEY (super_ssn) REFERENCES employee
(ssn),
--CONSTRAINT emp_dno_fk FOREIGN KEY (dno) REFERENCES department (dnumber),
------see NOTE1
CONSTRAINT emp_gen_ck CHECK (gender IN ('M','F','T')),
CONSTRAINT emp_sal_ck CHECK (salary BETWEEN 10000 AND 100000)
);
NOTE1:
--the line, if executed will result in error, because it refers to
DEPARTMENT table, which has not yet been created;
--so the line has been made a comment line, which will not be executed;
--so, to include this constraint, alter the EMPLOYEE table after
creating DEPARTMENT table

DETAILS OF DEPARTMENTS IN THE COMPANY


CREATE TABLE department
(
dname VARCHAR2(20) NOT NULL,
dnumber NUMBER,
mgr_ssn CHAR(9) NOT NULL,
mgr_start_date DATE DEFAULT current_date,
CONSTRAINT dept_pk PRIMARY KEY (dnumber),
CONSTRAINT dept_uk UNIQUE (dname),
CONSTRAINT dept_mssn_fk FOREIGN KEY (mgr_ssn) REFERENCES employee (ssn)
);
TO ADD FOREIGN KEY CONSTRAINT ON 'dno' IN EMPLOYEE TABLE
ALTER TABLE employee
ADD CONSTRAINT emp_dno_fk FOREIGN KEY (dno) REFERENCES department
(dnumber);

DETAILS OF LOCATIONS OF THE DEPARTMENTS


CREATE TABLE dept_locations
(
dnumber NUMBER,
dlocation VARCHAR2(20),
CONSTRAINT dloc_pk PRIMARY KEY (dnumber, dlocation),
CONSTRAINT dloc_dnum_fk FOREIGN KEY (dnumber) REFERENCES department
(dnumber)
);

DETAILS OF PROJECTS UNDERTAKEN BY THE COMPANY

CREATE TABLE project


(
pname VARCHAR2(20) NOT NULL,
pnumber NUMBER,
plocation VARCHAR2(20),
dnum NUMBER NOT NULL,
CONSTRAINT proj_pk PRIMARY KEY (pnumber),
CONSTRAINT proj_uk UNIQUE (pname),
CONSTRAINT proj_fk FOREIGN KEY (dnum) REFERENCES department(dnumber)
);

DETAILS OF PROJECTS ON WHICH THE EMPLOYEES WORKS

CREATE TABLE works_on


(
essn CHAR(9),
pno NUMBER,
hours NUMBER(3,1) NOT NULL,
CONSTRAINT works_pk PRIMARY KEY (essn, pno),
CONSTRAINT works_essn_fk FOREIGN KEY (essn) REFERENCES employee(ssn),
CONSTRAINT works_pno_fk FOREIGN KEY (pno) REFERENCES project(pnumber)
);
DETAILS OF DEPENDENTS OF THE EMPLOYEES
CREATE TABLE dependent
(
essn CHAR(9),
dependent_name VARCHAR2(30),
gender CHAR,
bdate DATE,
relationship VARCHAR2(20),
CONSTRAINT depend_pk PRIMARY KEY (essn, dependent_name),
CONSTRAINT depend_essn_fk FOREIGN KEY (essn) REFERENCES employee(ssn),
CONSTRAINT depend_gen_ck CHECK (gender IN ('M','F','T'))
);

EXERCISE 5

INSERT INTO employee VALUES('James','E','Borg','888665555','10-nov-1937','450 Stone,


Houston, Texas','M',55000,null,null);
INSERT INTO employee VALUES('Jennifer','S','Wallace','987654321','20-jun-1941','291 Berry,
Bellaire, Texas','F',43000,'888665555',null);
INSERT INTO employee VALUES('Franklin','T','Wong','333445555','08-dec-1955','638 Voss,
Houston, Texas','M',40000,'888665555',null);

INSERT INTO department VALUES('Headquarters',1,'888665555','19-jun-1981');


INSERT INTO department VALUES('Administration',4,'987654321','01-jan-1995');
INSERT INTO department VALUES('Research',5,'333445555','22-may-1988');

UPDATE employee SET dno = 1 WHERE ssn = '888665555';


UPDATE employee SET dno = 4 WHERE ssn = '987654321';
UPDATE employee SET dno = 5 WHERE ssn = '333445555';
INSERT INTO employee VALUES('John','B','Smith','123456789','09-jan-1965','731 Fondren,
Houston, Texas','M',30000,'333445555',5);
INSERT INTO employee VALUES('Alicia','J','Zelaya','999887777','19-jan-1968','3321 Castle,
Spring, Texas','F',25000,'987654321',4);
INSERT INTO employee VALUES('Ramesh','K','Narayan','666884444','15-sep-1962','975 Fire
Oak, Humble, Texas','M',38000,'333445555',5);
INSERT INTO employee VALUES('Joyce','A','English','453453453','31-jul-1972','5631 Rice,
Houston, Texas','F',25000,'333445555',5);
INSERT INTO employee VALUES('Ahmad','V','Jabbar','987987987','29-mar-1969','980 Dallas,
Houston, Texas','M',25000,'987654321',4);

INSERT INTO dept_locations VALUES(1,'Houston');


INSERT INTO dept_locations VALUES(4,'Stafford');
INSERT INTO dept_locations VALUES(5,'Bellaire');
INSERT INTO dept_locations VALUES(5,'Sugarland');
INSERT INTO dept_locations VALUES(5,'Houston');

INSERT INTO project VALUES('ProductX',1,'Bellaire',5);


INSERT INTO project VALUES('ProductY',2,'Sugarland',5);
INSERT INTO project VALUES('ProductZ',3,'Houston',5);
INSERT INTO project VALUES('Computerization',10,'Stafford',4);
INSERT INTO project VALUES('Reorganization',20,'Houston',1);
INSERT INTO project VALUES('Newbenefits',30,'Stafford',4);

INSERT INTO works_on VALUES('123456789',1,32.5);


INSERT INTO works_on VALUES('123456789',2,7.5);
INSERT INTO works_on VALUES('666884444',3,40.0);
INSERT INTO works_on VALUES('453453453',1,20.0);
INSERT INTO works_on VALUES('453453453',2,20.0);
INSERT INTO works_on VALUES('333445555',2,10.0);
INSERT INTO works_on VALUES('333445555',3,10.0);
INSERT INTO works_on VALUES('333445555',10,10.0);
INSERT INTO works_on VALUES('333445555',20,10.0);
INSERT INTO works_on VALUES('999887777',30,30.0);
INSERT INTO works_on VALUES('999887777',10,10.0);
INSERT INTO works_on VALUES('987987987',10,35.0);
INSERT INTO works_on VALUES('987987987',30,5.0);
INSERT INTO works_on VALUES('987654321',30,20.0);
INSERT INTO works_on VALUES('987654321',20,15.0);
INSERT INTO works_on VALUES('888665555',20,10.0);

INSERT INTO dependent VALUES('333445555','Alice','F','05-APR-1986','Daughter');


INSERT INTO dependent VALUES('333445555','Theodore','M','25-oct-1983','Son');
INSERT INTO dependent VALUES('333445555','Joy','F','25-oct-1983','Spouse');
INSERT INTO dependent VALUES('987654321','Abner','M','28-feb-1942','Spouse');
INSERT INTO dependent VALUES('123456789','Michael','M','04-jan-1988','Son');
INSERT INTO dependent VALUES('123456789','Alice','F','30-dec-1988','Daughter');
INSERT INTO dependent VALUES('123456789','Elisabeth','F','05-may-1967','Spouse');

Ex. No. 6 (SELECT statement) & 7 (WHERE clause)


(i)Retrieve names of all the projects and its location.
SELECT pname, plocation FROM project;

(ii)Retrieve all the attribute values of all the employees.


SELECT * FROM employee;

(iii)
(a)Retrieve all the attribute values of any EMPLOYEE who works in DEPARTMENT
number 4.
SELECT * FROM employee WHERE dno = 4;

(b)Retrieve all the attribute values of female employees.


SELECT * FROM employee WHERE gender = 'F';

(c)Retrieve all the attribute values of employees whose salary is greater than or equal to
20000.
SELECT * FROM employee WHERE salary >= 20000;

(iv)Retrieve the birth date and address of the employee(s) whose name is ‘John B. Smith’.
SELECT bdate, address
FROM employee
WHERE fname=‘John’ AND minit=‘B’ AND lname=‘Smith’;

(v)Retrieve the name and address of all employees who work for the ‘Research’
department.
SELECT fname, lname, address
FROM employee, department
WHERE dnumber=dno AND dname=‘Research’;
Ex. No. 8 (ORDER BY clause)

(​ i) Retrieve employee details in the ascending order of salary.


SELECT * FROM employee ORDER BY salary;

(ii) Retrieve first name, last name, date of birth of employees who are managers along with
the name of the department that they manage.
SELECT fname, lname, bdate, dname
FROM employee, department
WHERE ssn = mgr_ssn
ORDER BY bdate;

(iii) Retrieve a list of employees and the projects they are working on, ordered by
department (descending) and, within each department, ordered alphabetically by last name
(ascending), then first name (ascending).
SELECT D.dname, E.lname, E.fname, P.pname
FROM department D, employee E, works_on W, project P
WHERE D.dnumber= E.dno AND E.ssn= W.essn AND W.pno= P.pnumber
ORDER BY D.dname DESC, E.lname ASC, E.fname ASC;

Ex. No. 9 (GROUP BY and HAVING clause)


(i) For each department, retrieve the department number, the number of employees in the
department, and their average salary.

SELECT Dno, COUNT (*), AVG (salary)


FROM EMPLOYEE
GROUP BY dno;
(ii) For each project on which more than two employees work, retrieve the project number,
the project name, and the number of employees who work on the project.

SELECT pnumber, pname, COUNT (*)


FROM project, works_on
WHERE pnumber=pno
GROUP BY pnumber, pname
HAVING COUNT (*) > 2;

Ex. No. 10 (Aggregate functions in SQL (Count, Sum, Max, Min, Avg))

(i) Find the sum of salary, maximum salary, minimum salary, and average salary of all
employees.

SELECT SUM (salary), MAX (salary), MIN (salary), AVG (salary) FROM employee;

(ii) Find the total number of employees working in the company.

SELECT COUNT(*) FROM employee;

(iii) Find the total number of employees working in research department.

SELECT COUNT(*)
FROM employee, department
WHERE dno = dnumber AND dname = 'Research';

(iv) Find the number of distinct salary given to the employees.

SELECT COUNT(DISTINCT salary) FROM employee;

Ex. No. 11 (SQL operators(And, Or, Like, Between, In))

(i) Retrieve names of employees who resides in Houston.

SELECT fname, lname FROM employee WHERE address LIKE '%Houston%';


(ii) Retrieve names of employees who were born in January.

SELECT fname, lname FROM employee WHERE bdate LIKE '___JAN___';

(iii) Show the resulting salaries if every employee working in the research department is
given a 10 percent raise.

SELECT fname, lname, 1.1*salary AS increased_salary


FROM employee, department
WHERE dno = dnumber AND dname = 'Research';
Ex. No. 12 (JOIN operation)

(i) Retrieve name and address of employees working in the research department.

SELECT fname, lname, address


FROM (employee JOIN department ON dno=dnumber)
WHERE dname=‘Research’;

(ii) For every project located in Stafford, list the project number, the controlling
department number, and the department manager's last name, address, and birth date.

SELECT pnumber, dnum, lname, address, bdate


FROM ((project JOIN department ON dnum=dnumber) JOIN employee ON mgr_ssn=ssn)
WHERE plocation=‘Stafford’;

Ex. No. 13 (Nested and complex queries)


(i) Make a list of all project numbers for projects that involve an employee whose last name
is ‘Smith’, either as a worker or as a manager of the department that controls the project.

(SELECT DISTINCT pnumber


FROM project, department, employee
WHERE dnum=dnumber AND mgr_ssn=ssn
AND lname=‘Smith’)
UNION
( SELECT DISTINCT pnumber
FROM project, works_on, employee
WHERE pnumber=pno AND essn=ssn
AND lname=‘Smith’ );

(ii) Retrieve the names of employees whose salary is greater than the salary of all the
employees in department 5.

SELECT lname, fname


FROM employee
WHERE salary > ALL ( SELECT salary
FROM employee
WHERE dno=5 );

(iii) Retrieve the names of employees who have no dependents.


SELECT fname, lname
FROM employee
WHERE NOT EXISTS ( SELECT *
FROM dependent
WHERE ssn=essn );

(iv) List the names of managers who have at least one dependent.
SELECT fname, lname
FROM employee
WHERE EXISTS ( SELECT *
FROM dependent
WHERE ssn=essn )
AND
EXISTS ( SELECT *
FROM department
WHERE ssn=mgr_ssn );

Ex. No. 14 (UPDATE, ALTER, DELETE, DROP)

(i) Create a table to store students' details.

CREATE TABLE students


(reg_no CHAR(10),
stu_name VARCHAR2(30),
bdate DATE,
gender CHAR
);

(ii) Insert some records.

INSERT INTO students VALUES('578CS17001','Adithya','25-AUG-2000','M');


INSERT INTO students VALUES('578CE17005','Aravind','29-JUN-2001','M');
INSERT INTO students VALUES('578EC17003','Boomika','10-JUL-2000','F');
INSERT INTO students VALUES('578CS17003','Dhanya','30-AUG-2001','F');
INSERT INTO students VALUES('578ME17005','Guhan','29-DEC-2000','M');
(iii) Alter table to add a column 'branch'.

ALTER TABLE students ADD branch CHAR(4);

(iv) Update students table to add branch of all the students.

UPDATE students SET branch='CS' WHERE stu_name='Adithya';


UPDATE students SET branch='CE' WHERE stu_name='Aravind';
UPDATE students SET branch='EC' WHERE stu_name='Boomika';
UPDATE students SET branch='CS' WHERE stu_name='Dhanya';
UPDATE students SET branch='ME' WHERE stu_name='Guhan';

(v) Delete any one record.

DELETE FROM students WHERE stu_name='Guhan';

(vi) Drop any one column.

ALTER TABLE students DROP COLUMN gender;

(vii) Delete all the records.

DELETE FROM students;

(viii) Drop the students table.

DROP TABLE students;


Ex. No. 15 (CREATE VIEW command and manipulating)

(i) Create a view 'works_on1' to store employee's first and last name, projects they have
worked on, and the number of hours they worked on each project.

CREATE VIEW works_on1


AS SELECT fname, lname, pname, hours
FROM employee, project, works_on
WHERE ssn=essn AND pno=pnumber;

(ii) Retrieve names of the employees who worked on project 'ProductX';

SELECT fname, lname


FROM works_on1
WHERE pname=‘ProductX’;

(iii) Drop the view 'works_on1'.

DROP VIEW works_on1;

Ex. No. 16 & 17 (COMMIT, SAVEPOINT, AND ROLL BACK)

TCL Command

Transaction Control Language (TCL) commands are used to manage transactions (collection of
queries) in database. These are used to manage the changes made by DML statements.

COMMIT​ - This command is used to permanently save any transaction into database.
SAVEPOINT​ - This command is used to temporarily save a transaction so that you can
rollback to that point whenever necessary.
ROLLBACK​ - This command restores the database to last committed state. It is also used
with savepoint command to jump to a savepoint in a transaction.
Consider the 'students' table and its data in Ex. No. 14

Q1. SELECT * FROM students;

Q2. INSERT INTO students VALUES('578ME17006','John','01-JAN-2000','M');

Q3. DELETE FROM students WHERE stu_name='Guhan';

Q4. SELECT * FROM students;

Q5. COMMIT;

(After COMMIT, transaction made using Q2 and Q3 will be permanently saved in the database.)

Q6. UPDATE students SET bdate='10-JAN-2000' WHERE name = 'John';

Q7. INSERT INTO students VALUES('578EC17010','Sushma','14-SEP-2001','F');

Q8. SAVEPOINT a;

Q9. DELETE FROM students WHERE stu_name='Boomika';

Q10. INSERT INTO students VALUES('578CE17015','Sabatini','20-APR-1999','F');

Q11. SAVEPOINT b;

Q12. UPDATE students SET name='Jhonny' WHERE name = 'Jhon';

Q13. ROLLBACK to b;
(This command will remove the transaction made using Q12.)

Q14. COMMIT;
(This command will make permanent all the transactions made using Q6,Q7,Q9, and Q10.)

Вам также может понравиться