DBMS LAB MANUAL
DBMS LAB MANUAL
2.SQL Commands
SQL commands are instructions used to communicate with the database to perform specific
task that work with data. SQL commands can be used not only for searching the database but
also to perform various other functions like, for example, you can create tables, add data to
tables, or modify data, drop the table, set permissions for users.
SQL commands are grouped into four major categories depending on their functionality:
• Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are
CREATE, ALTER, DROP, RENAME, and TRUNCATE.
• Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving, modifying, and deleting data. These commands are SELECT, INSERT,
UPDATE, and DELETE.
• Transaction Control Language (TCL) - These SQL commands are used for managing
changes affecting the data. These commands are COMMIT, ROLLBACK, and
SAVEPOINT.
• Data Control Language (DCL) - These SQL commands are used for providing
security to database objects. These commands are GRANT and REVOKE.
1) Primary key:
This constraint defines a column or combination of columns which uniquely identifies each
row in the table.
This constraint identifies any column referencing the PRIMARY KEY in another table. It
establishes a relationship between two columns in the same table or between different
tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary
Key in the table which it is referring. One or more columns can be defined as Foreign key.
2.ALTER TABLE
Statement The SQL ALTER TABLE command is used to modify the definition structure) of a
table by modifying the definition of its columns.
SELECT Syntax:
SELECT * FROM table_name;
PROGRAME – 1
LIBRARY DATABASE
Table Creation:
1.PUBLISHER
SQL> CREATE TABLE PUBLISHER(
NAME VARCHAR(18) PRIMARY KEY,
ADDRESS VARCHAR(10),
PHONE VARCHAR(10));
Table created.
2.BOOK
SQL> create table BOOK
(
Book_id int primary key,
Title varchar(20),
Publisher_Name varchar(20) references PUBLISHER(Name) ON DELETE
CASCADE,
Pub_Year int
);
Table created.
3.BOOK_AUTHORS
SQL> CREATE TABLE BOOK_AUTHORS(
Table created.
4.LIBRARY_BRANCH
SQL> CREATE TABLE LIBRARY_BRANCH(
BRANCH_ID INTEGER PRIMARY KEY,
BRANCH_NAME VARCHAR(18),
ADDRESS VARCHAR(15));
Table created.
5.BOOK_COPIES
SQL> CREATE TABLE BOOK_COPIES(
BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE
CASCADE, BRANCH_ID INTEGER REFERENCES
LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE, NO_OF_COPIES
INTEGER, PRIMARY KEY(BOOK_ID,BRANCH_ID));
Table created.
6.BOOK_LENDING
SQL> CREATE TABLE BOOK_LENDING(
BOOK_ID INTEGER REFERENCES BOOK(BOOK_ID) ON DELETE
CASCADE, BRANCH_ID INTEGER REFERENCES
LIBRARY_BRANCH(BRANCH_ID) ON DELETE CASCADE, CARD_NO
INTEGER, DATE_OUT VARCHAR(20), DUE_DATE VARCHAR(20), PRIMARY
KEY(BOOK_ID,BRANCH_ID,CARD_NO));
Table created.
BOOK
SQL> INSERT INTO BOOK VALUES(1111,'SE','PEARSON',2005);
SQL> INSERT INTO BOOK VALUES(2222,'DBMS','MCGRAW',2004);
SQL> INSERT INTO BOOK VALUES(3333,'ANOTOMY','PEARSON',2010);
SQL> INSERT INTO BOOK VALUES(4444,'ENCYCLOPEDIA','SAPNA',2010);
BOOK_AUTHORS
SQL> INSERT INTO BOOK_AUTHORS VALUES(1111,'SOMMERVILLE');
SQL> INSERT INTO BOOK_AUTHORS VALUES(2222,'NAVATHE');
SQL> INSERT INTO BOOK_AUTHORS VALUES(3333,'HENRY GRAY');
SQL> INSERT INTO BOOK_AUTHORS VALUES(4444,'THOMAS');
LIBRARY_BRANCH
SQL>INSERT INTO LIBRARY_BRANCH VALUES(11,'CENTRAL TECHNICAL','MG
ROAD');
SQL>INSERT INTO LIBRARY_BRANCH VALUES(22,'MEDICAL','BH ROAD');
SQL> INSERT INTO LIBRARY_BRANCH VALUES(33,'CHILDREN','SS PURAM');
SQL>INSERT INTO LIBRARY_BRANCH VALUES(44,'SECRETARIAT','SIRAGATE');
SQL>INSERT INTO LIBRARY_BRANCH VALUES(55,'GENERAL','JAYANAGAR');
BOOK_COPIES
SQL> INSERT INTO BOOK_COPIES VALUES(1111,11,5);
SQL> INSERT INTO BOOK_COPIES VALUES(3333,22,6);
SQL> INSERT INTO BOOK_COPIES VALUES(4444,33,10);
SQL> INSERT INTO BOOK_COPIES VALUES(2222,11,12);
SQL> INSERT INTO BOOK_COPIES VALUES(4444,55,3);
BOOK_LENDING
SQL> INSERT INTO BOOK_LENDING VALUES(2222,11,1,'10-01-17','20-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(3333,22,2,'09-07-17','12-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(4444,55,1,'11-04-17','09-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(2222,11,5,'09-08-17','19-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(4444,33,1,'10-06-17','15-08-17');
SQL> INSERT INTO BOOK_LENDING VALUES(1111,11,1,'12-05-17','10-06-17');
SQL> INSERT INTO BOOK_LENDING VALUES(3333,22,1,'10-07-17','15-07-17');
Queries:
1) Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each branch, etc
2) Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun 2017.
SELECT CARD_NO
FROM BOOK_LENDING
WHERE DATE_OUT BETWEEN '01-01-17' AND '30-06-17'
GROUP BY CARD_NO
HAVING COUNT(*) > 3;
3) Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
1 row deleted.
SELECT*FROM BOOK;
4) Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
5) Create a view of all books and its number of copies that are currently available in the
Library.
PROGRAME – 2
ORDER DATABASE
Table Creation:
1.SALESMAN
Table created.
2.CUSTOMER
Table created.
3.ORDERS
Table created.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
(SELECT NAME
FROM SALESMAN S, CUSTOMER C
WHERE S.SALESMAN_ID=C.SALESMAN_ID AND S.CITY=C.CITY)
UNION
(SELECT NAME
FROM SALESMAN
WHERE SALESMAN_ID NOT IN(SELECT S1.SALESMAN_ID FROM
SALESMAN S1, CUSTOMER C1 WHERE
S1.SALESMAN_ID=C1.SALESMAN_ID AND S1.CITY=C1.CITY));
4. Create a view that finds the salesman who has the customer with the highest order of a
day
CREATE VIEW SALES_HIGHERODER AS
SELECT SALESMAN_ID, PURCHASE_AMT
FROM ORDERS
WHERE PURCHASE_AMT=(SELECT MAX(O.PURCHASE_AMT) FROM
ORDERS O WHERE O.ORD_DATE='12-APR-16');
View created.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
DELETE from salesman
WHERE salesman_id = 1000;
1 row deleted.
PROGRAME– 3
MOVIE DATABASE
Table Creation:
ACTOR
CREATE TABLE ACTOR( ACT_ID NUMBER(5) CONSTRAINT ACTOR_ACTID_PK
PRIMARY KEY, ACT_NAME VARCHAR(18) CONSTRAINT ACTOR_ACTNAME_NN
NOT NULL, ACT_GENDER VARCHAR(2) CONSTRAINT ACTOR_ACTGENDER_NN
NOT NULL);
Table created.
DIRECTOR
CREATE TABLE DIRECTOR( DIR_ID NUMBER(5) CONSTRAINT
DIRECTOR_DIRID_PK PRIMARY KEY, DIR_NAME VARCHAR(18) CONSTRAINT
DIRECTOR_DIRNAME_NN NOT NULL, DIR_PHONE VARCHAR(10) CONSTRAINT
DIRECTOR_DIRPHONE_NN NOT NULL);
Table created.
MOVIES
CREATE TABLE MOVIES( MOV_ID NUMBER(5) CONSTRAINT
MOVIES_MOVID_PK PRIMARY KEY, MOV_TITLE VARCHAR(10) CONSTRAINT
MOVIES_MOVTITLE_NN NOT NULL, MOV_YEAR NUMBER(5) CONSTRAINT
MOVIES_MOVYEAR_NN NOT NULL, MOV_LANG VARCHAR(10) CONSTRAINT
MOVIES_MOVLANG_NN NOT NULL, DIR_ID NUMBER(5) CONSTRAINT
MOVIES_DIRID_FK REFERENCES DIRECTOR(DIR_ID));
Table created.
MOVIE_CAST
CREATE TABLE MOVIE_CAST (ACT_ID NUMBER(5) CONSTRAINT
MOVIECAST_ACTID_FK REFERENCES ACTOR(ACT_ID), MOV_ID NUMBER(5)
CONSTRAINT MOVIECAST_MOVID_FK REFERENCES MOVIES(MOV_ID), ROLE
VARCHAR(10), CONSTRAINT MOVIECAST_ACTID_MOVID_PK PRIMARY
KEY(ACT_ID,MOV_ID));
Table created.
RATING
CREATE TABLE RATING( MOV_ID NUMBER(5) CONSTRAINT RATING_MOVID_FK
REFERENCES MOVIES(MOV_ID), REV_STARS NUMBER(1) CONSTRAINT
RATING_REVSTARS_NN NOT NULL, CONSTRAINT RATING_MOVID_PK PRIMARY
KEY(MOV_ID))
Table created.
Description of Schema:
2. Find the movie names where one or more actors acted in two or more movies.
SELECT MOV_TITLE FROM MOVIES M, MOVIE_CAST MC WHERE
M.MOV_ID=MC.MOV_ID AND MC.ACT_ID IN (SELECT ACT_ID FROM
MOVIE_CAST GROUP BY ACT_ID HAVING COUNT(MOV_ID)>=2);
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
(SELECT ACT_NAME FROM ACTOR A JOIN MOVIE_CAST C ON 4.
A.ACT_ID=C.ACT_ID JOIN MOVIES M ON C.MOV_ID=M.MOV_ID WHERE
M.MOV_YEAR < 2000) INTERSECT (SELECT ACT_NAME FROM ACTOR A
JOIN MOVIE_CAST C ON A.ACT_ID=C.ACT_ID JOIN MOVIES M ON
C.MOV_ID=M.MOV_ID WHERE M.MOV_YEAR > 2015);
4. Find the title of movies and number of stars for each movie that has at least one rating and
find the highest number of stars that movie received. Sort the result by movie title.
SELECT MOV_TITLE, REV_STARS FROM MOVIES M, RATING R WHERE
M.MOV_ID=R.MOV_ID AND REV_STARS>=1 ORDER BY MOV_TITLE
1 row updated.
PROGRAME - 4
COLLEGE DATABASE
Table Creation:
STUDENT
CREATE TABLE STUDENT
(USN VARCHAR(10) PRIMARY KEY,
SNAME VARCHAR(25),
ADDRESS VARCHAR(25),
PHONE VARCHAR(10),
GENDER CHAR(1));
Table created.
SEMSEC
Table created.
CLASS
KEY(USN,SSID),
FOREIGN KEY(USN) REFERENCES STUDENT(USN),
FOREIGN KEY(SSID) REFERENCES SEMSEC(SSID));
Table created.
SUBJECT
Table created.
IAMARKS
Table created.
SEMSEC:
CLASS:
SUBJECT:
IAMARKS:
Queries: 1.
List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and in each
section.
View created.
SQL> select * from test1;
4. Calculate the FinalIA (average of best two test marks) and update the corresponding table
for all students.
FOR UPDATE;
C_A NUMBER;
C_B NUMBER;
C_C NUMBER;
C_SM NUMBER;
C_AV NUMBER;
BEGIN
OPEN C_IAMARKS;
LOOP
FETCH C_IAMARKS INTO C_A,C_B,C_C;
EXIT WHEN C_IAMARKS%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(C_A||' '||C_B||' '||C_C);
IF(C_A!=C_B) THEN
C_SM:=C_A+C_B;
ELSE
C_SM:=C_A+C_C;
END IF;
C_AV:=C_SM/2; DBMS_OUTPUT.PUT_LINE('SUM='||C_SM);
DBMS_OUTPUT.PUT_LINE('AVERAGE='||C_AV);
UPDATE IAMARKS
SET FINALIA=C_AV
WHERE CURRENT OF C_IAMARKS;
END LOOP;
CLOSE C_IAMARKS;
END AVG;
Procedure created.
SQL> BEGIN
1 AVG;
2 END;
PL/SQL procedure successfully completed.
SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,
CASE WHEN IA.FINALIA BETWEEN 17 AND 20 THEN 'OUTSTANDING'
WHEN IA.FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE'
ELSE 'WEAK'
END AS CAT
FROM STUDENT S,SEMSEC SS,IAMARKS IA,SUBJECT SUB
WHERE S.USN=IA.USN AND
SS.SSID=IA.SSID AND
SUB.SUBCODE=IA.SUBCODE AND
SUB.SEM=7
CHAPTER – 5
COMPANY DATABASE
5. Consider the schema for Company Database:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
Write SQL queries to
i) Make a list of all project numbers for projects that involve an employee whose
last name is ‘Scott’, either as a worker or as a manager of the department that
controls the project.
ii) Show the resulting salaries if every employee working on the ‘IoT’ project is
given a 10 percent raise.
iii) Find the sum of the salaries of all employees of the ‘Accounts’ department, as
well as the maximum salary, the minimum salary, and the average salary in this
department
iv) Retrieve the name of each employee who works on all the projects controlled by
department number
v) (use NOT EXISTS operator). For each department that has more than five
employees, retrieve the department number and the number of its employees
who are making more than Rs. 6,00,000.
Table Creation:
DEPARTMENT
CREATE TABLE DEPARTMENT(
DNO NUMBER(3) CONSTRAINT DEPT_DNO_PK PRIMARY KEY, DNAME
VARCHAR(15) CONSTRAINT DEPT_DNAME_NN NOT NULL, MGRSSN
CHAR(10),
MGRSTARTDATE DATE);
EMPLOYEE
CREATE TABLE EMPLOYEE(
SSN CHAR(10) CONSTRAINT EMP_SSN_PK PRIMARY KEY,
NAME VARCHAR(18) CONSTRAINT EMP_NAME_NN NOT NULL,
ADDRESS VARCHAR(18),
SEX VARCHAR(3), SALARY
REAL, SUPER_SSN CHAR(10),
DNO NUMBER(3) CONSTRAINT EMP_DNO_FK REFERENCES
DEPARTMENT(DNO));
ALTER TABLE DEPARTMENT ADD CONSTRAINT DEPT_MGRSSN_FK FOREIGN
KEY(MGRSSN) REFERENCES EMPLOYEE(SSN);
Table altered.
DLOCATION
CREATE TABLE DLOCATION(
DLOC VARCHAR2 (20),
DNO REFERENCES DEPARTMENT (DNO),
PRIMARY KEY (DNO, DLOC));
PROJECT
CREATE TABLE PROJECT(
PNO INTEGER PRIMARY KEY,
PNAME VARCHAR2 (20),
PLOCATION VARCHAR2 (20),
DNO REFERENCES DEPARTMENT (DNO));
WORKS_ON
CREATE TABLE WORKS_ON(
HOURS NUMBER (2),
SSN REFERENCES EMPLOYEE (SSN),
PNO REFERENCES PROJECT(PNO),
PRIMARY KEY (SSN, PNO));
EMPLOYEE
INSERT INTO EMPLOYEE VALUES ('&SSN','&NAME','&ADDRESS','&SEX',
&SALARY,'&SUPERSSN',& DNO);
DLOCATION
PROJECT
WORKS_ON
1. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls the
project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10
percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department number
and the number of its employees who are making more than Rs. 6,00,000.
SELECT DNO,COUNT(SSN)
FROM EMPLOYEE WHERE SALARY>600000 AND DNO
IN(SELECT DNO FROM EMPLOYEE
GROUP BY DNO HAVING COUNT(SSN)>5) GROUP BY DNO ;
VIVA QUESTIONS
1. Define Data.
2. Define Information.
3. Define Database.
4. Define DBMS.
5. What do you mean by processed data?
6. What do you mean by data management?
7. Which are the actions that are performed on the database?
8. Mention the different types of DBMS.
9. Define Data model.
10. Mention the different types of Data models.
11. Why database approach is advantageous than the file system approach?
12. Who is called as the father of RDBMS?
13. What do you mean by redundant data?
14. What do you mean by Data duplication?
15. Mention the different relational algebra operations.
16. Mention the different User interfaces provided by the database system.
17. Mention the different languages provided by the database system
18. What is the difference between select operation in relational algebra and in SQL?
19. What is the difference between JOIN and Cartesian product?
20. Mention the different types of Join operations.
21. What is the difference between EQUIJOIN and NATURAL JOIN?
22. What is the difference between OUTER JOIN and JOIN.?
23. What is the difference between OUTER UNION and UNION?
24. What do you mean by Union Compatibility.?
25. What do you mean by Type Compatibility?
26. Mention the different types of relational constraints.
27. Mention the different types of structural constraints
28. What do you mean by cardinality?
29. What do you mean by cardinality ratio?
30. What do you mean by degree of a relation?
31. What do you mean by entity integrity constraint?
32. What do you mean by referential integrity constraint?
33. What do you mean by NULL constraint?
34. What do you mean by unique constraint?
35. What do you mean by Check constraint?
36. Define functional dependency.
37. Define normalization.
38. Define normal form
39. Mention the different types of normal forms
40. What is the difference between 3NF and BCNF?
41. What do you mean by JOIN dependencies?
70. Which functional dependencies are to be removed to achieve respective normal form?
Discuss all the normal forms up to 4NF?
71. What is the mathematical basis of SQL? The SQL statement: select * from student will
perform like projection or selection? Give details in support of your answer.