Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sample Manual

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 62

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

B.TECH. COMPUTER SCIENCE AND ENGINEERING


CSE2004 – DATABASE MANAGEMENT SYSTEMSLAB

Hard copy of Lab manual


Lab Manual

Ex. No : 1 – DDL and DML

Question:
Create the following schemas
student(regno, sname, cgpa)
course(ccode, cname, credits)
faculty(empid, fname, designation, salary)
SQL Query :
create table student(regno varchar(9), sname varchar(20), cgpa number(2));
create table course(ccode varchar(9), cname varchar(20), credits
number(3));
create table faculty(empid varchar(9), fname varchar(20), designation
varchar(20), salary number(7));
Output:
Table created.
Table created.
Table created.

Question: Add email id attribute to student relation and describe the


structure
SQL> alter table student add email varchar(30);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0

SQL> desc student;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(9) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| cgpa | int(2) | YES | | NULL | |
| email | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: Change the data type of sname in student relation and describe
the structure

SQL> alter table student modify sname integer(20);


Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0

SQL> desc student;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(9) | YES | | NULL | |
| sname | int(20) | YES | | NULL | |
| cgpa | int(2) | YES | | NULL | |
| email | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: Change the size of any attribute and describe the structure

SQL> alter table faculty modify fname varchar(30);


Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

SQL> desc faculty;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| empid | varchar(9) | YES | | NULL | |
| fname | varchar(30) | YES | | NULL | |
| designation | varchar(20) | YES | | NULL | |
| salary | int(7) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: Add phone number attribute to student relation and describe the
structure
SQL> alter table student add phone integer(10);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0

SQL> desc student;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(9) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| cgpa | int(2) | YES | | NULL | |
| email | varchar(30) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Question: Rename the empid attribute as fid in faculty relation and


describe the structure
SQL> alter table faculty rename column empid to fid;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

SQL> desc faculty;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| fid | varchar(9) | YES | | NULL | |
| fname | varchar(30) | YES | | NULL | |
| designation | varchar(20) | YES | | NULL | |
| salary | int(7) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: Delete the email id attribute from student and describe the
structure
SQL> alter table student drop column email;
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

SQL> desc student;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(9) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| cgpa | int(2) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

Question: Insert 5 records each to all the schemas


SQL> insert into student values('16BCE1001','harsh',9,8979867898);
Query OK, 1 row affected (0.05 sec)

SQL> insert into student values('16BCE1002','alex',9.3,8979867899);


Query OK, 1 row affected (0.02 sec)

SQL> insert into student values('16BCE1003','abhi',8.9,8979867800);


Query OK, 1 row affected (0.01 sec)

SQL> insert into student values('16BCE1004','virat',7.8,8979867810);


Query OK, 1 row affected (0.02 sec)

SQL> insert into student values('16BCE1005','anmol',8.3,8979867811);


Query OK, 1 row affected (0.01 sec)

SQL> insert into faculty values(1001,'mohan','prof.',30000);


Query OK, 1 row affected (0.07 sec)

SQL> insert into faculty values(1002,'rohan','prof.',300000);


Query OK, 1 row affected (0.08 sec)

SQL> insert into faculty values(1003,'mona','asst. prof.',10000);


Query OK, 1 row affected (0.05 sec)

SQL> insert into faculty values(1004,'tony','asst. prof.',10000);


Query OK, 1 row affected (0.05 sec)

SQL> insert into faculty values(1005,'john','prof.',100000);


Query OK, 1 row affected (0.05 sec)

SQL> insert into course values('c1011','DBMS',4);


Query OK, 1 row affected (0.04 sec)

SQL> insert into course values('c2034','IWP',4);


Query OK, 1 row affected (0.07 sec)

SQL> insert into course values('c3004','ML',4);


Query OK, 1 row affected (0.11 sec)

SQL> insert into course values('c4014','AI',4);


Query OK, 1 row affected (0.04 sec)
SQL> insert into course values('c1002','DLD',4);
Query OK, 1 row affected (0.02 sec)

Question: Display the contents of all the schemas


SQL> select * from student;
+-----------+-------+------+------------+
| regno | sname | cgpa | phone |
+-----------+-------+------+------------+
| 16BCE1001 | harsh | 9 | 8979867898 |
| 16BCE1002 | alex | 9.3 | 8979867899 |
| 16BCE1003 | abhi | 8.9 | 8979867800 |
| 16BCE1004 | virat | 7.8 | 8979867810 |
| 16BCE1005 | anmol | 8.3 | 8979867811 |
+-----------+-------+------+------------+
5 rows in set (0.00 sec)

SQL> select * from faculty;


+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1001 | mohan | prof. | 30000 |
| 1002 | rohan | prof. | 300000 |
| 1003 | mona | asst. prof. | 10000 |
| 1004 | tony | asst. prof. | 10000 |
| 1005 | john | prof. | 100000 |
+------+-------+-------------+--------+
5 rows in set (0.00 sec)

SQL> select * from course;


+-------+-------+---------+
| ccode | cname | credits |
+-------+-------+---------+
| c1011 | DBMS | 4 |
| c2034 | IWP | 4 |
| c3004 | ML | 4 |
| c4014 | AI | 4 |
| c1002 | DLD | 4 |
+-------+-------+---------+
5 rows in set (0.00 sec)

Question: Copy the contents of student table and name the table as student
new.
SQL> create table student_new as select * from student;
Query OK, 5 rows affected (0.14 sec)
Records: 5 Duplicates: 0 Warnings: 0
SQL> select * from student_new;
+-----------+-------+------+------------+
| regno | sname | cgpa | phone |
+-----------+-------+------+------------+
| 16BCE1001 | harsh | 9 | 8979867898 |
| 16BCE1002 | alex | 9.3 | 8979867899 |
| 16BCE1003 | abhi | 8.9 | 8979867800 |
| 16BCE1004 | virat | 7.8 | 8979867810 |
| 16BCE1005 | anmol | 8.3 | 8979867811 |
+-----------+-------+------+------------+
5 rows in set (0.00 sec)

Question: Delete the contents of student new table, describe the structure
and display the contents of the table
SQL> truncate table student_new;
Query OK, 0 rows affected (0.08 sec)

SQL> desc student_new;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(9) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| cgpa | float | YES | | NULL | |
| phone | bigint(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

SQL> select * from student_new;


Empty set (0.01 sec)
Question: Delete the structure of student new table , describe the
structure and display the contents of the table
student(regno, sname, cgpa)
course(ccode, cname, credits)
faculty(empid, fname, designation)
SQL> drop table student_new;
Query OK, 0 rows affected (0.07 sec)

SQL> desc student_new;


ERROR 1146 (42S02): Table 'dbms.student_new' doesn't exist
SQL> select * from student_new;
ERROR 1146 (42S02): Table 'dbms.student_new' doesn't exist
SQL> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| regno | varchar(9) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| cgpa | float | YES | | NULL | |
| phone | bigint(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

SQL> select * from student;


+-----------+-------+------+------------+
| regno | sname | cgpa | phone |
+-----------+-------+------+------------+
| 16BCE1001 | harsh | 9 | 8979867898 |
| 16BCE1002 | alex | 9.3 | 8979867899 |
| 16BCE1003 | abhi | 8.9 | 8979867800 |
| 16BCE1004 | virat | 7.8 | 8979867810 |
| 16BCE1005 | anmol | 8.3 | 8979867811 |
+-----------+-------+------+------------+
5 rows in set (0.01 sec)

SQL> desc faculty;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| fid | varchar(9) | YES | | NULL | |
| fname | varchar(30) | YES | | NULL | |
| designation | varchar(20) | YES | | NULL | |
| salary | int(7) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

SQL> select * from faculty;


+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1001 | mohan | prof. | 30000 |
| 1002 | rohan | prof. | 300000 |
| 1003 | mona | asst. prof. | 10000 |
| 1004 | tony | asst. prof. | 10000 |
| 1005 | john | prof. | 100000 |
+------+-------+-------------+--------+
5 rows in set (0.01 sec)
SQL> desc course;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| ccode | varchar(9) | YES | | NULL | |
| cname | varchar(20) | YES | | NULL | |
| credits | int(2) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

SQL> select * from course;


+-------+-------+---------+
| ccode | cname | credits |
+-------+-------+---------+
| c1011 | DBMS | 4 |
| c2034 | IWP | 4 |
| c3004 | ML | 4 |
| c4014 | AI | 4 |
| c1002 | DLD | 4 |
+-------+-------+---------+
5 rows in set (0.01 sec)

Question: Insert required records to all tables.

Question: List all student details whose cgpa is > 8


SQL> select * from student where cgpa>8;
+-----------+-------+------+------------+
| regno | sname | cgpa | phone |
+-----------+-------+------+------------+
| 16BCE1001 | harsh | 9 | 8979867898 |
| 16BCE1002 | alex | 9.3 | 8979867899 |
| 16BCE1003 | abhi | 8.9 | 8979867800 |
| 16BCE1005 | anmol | 8.3 | 8979867811 |
+-----------+-------+------+------------+
4 rows in set (0.00 sec)
Question: List the name of faculty whose designation is ‘professor’
SQL> select * from faculty where designation='prof.';
+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1001 | mohan | prof. | 30000 |
| 1002 | rohan | prof. | 300000 |
| 1005 | john | prof. | 100000 |
+------+-------+-------------+--------+
3 rows in set (0.00 sec)

Question: List the course list with 2 credits


SQL> select * from course where credits=2;
Empty set (0.00 sec)

Question: List the name and designation of the faculty who receives salary
between 1 lakh and 2 lakhs
SQL> select fname,designation from faculty where salary>100000 and
salary<200000;
Empty set (0.00 sec)

Question: Sort the faculty details in ascending order based on the salary
they receive.
SQL> select * from faculty order by salary asc;
+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1003 | mona | asst. prof. | 10000 |
| 1004 | tony | asst. prof. | 10000 |
| 1001 | mohan | prof. | 30000 |
| 1005 | john | prof. | 100000 |
| 1002 | rohan | prof. | 300000 |
+------+-------+-------------+--------+
5 rows in set (0.00 sec)

Question: Sort the designation of the faculty by descending order and name
of the faculty in ascending order.
SQL> select * from faculty order by designation desc, fname asc;
+------+-------+-------------+--------+
| fid | fname | designation | salary |
+------+-------+-------------+--------+
| 1005 | john | prof. | 100000 |
| 1001 | mohan | prof. | 30000 |
| 1002 | rohan | prof. | 300000 |
| 1003 | mona | asst. prof. | 10000 |
| 1004 | tony | asst. prof. | 10000 |
+------+-------+-------------+--------+
5 rows in set (0.00 sec)

Question: List the course names which has more than 3 credits
SQL> select * from course where credits>3;
+-------+-------+---------+
| ccode | cname | credits |
+-------+-------+---------+
| c1011 | DBMS | 4 |
| c2034 | IWP | 4 |
| c3004 | ML | 4 |
| c4014 | AI | 4 |
| c1002 | DLD | 4 |
+-------+-------+---------+
5 rows in set (0.00 sec)
Question: List the name of the student whose name starts with ‘r’ and ends
with ‘i’
SQL> select sname from student where sname like 'r%i';
Empty set (0.00 sec)

Question: List the course name which has ‘t’ in it.


SQL> select cname from course where cname like '%t%';
Empty set (0.00 sec)
Ex. No 2 – Constraints

Question: Create the following tables and apply constraints as


follows
1. Books Table:
a. isbn – primary key
b. title - unique
c. price – not null
d. qty – not null

2. Authors Table:
a. authorId – primary key
b. email

3. Book_Authors:
a. isbn – foreign key references books table
b. authorId – foreign key references authors table

SQL Query:
create table books(isbn varchar(10), title varchar(20), price
float(5) not null, qty int(3) not null, constraint myuni1
unique(title), constraint pk primary key(isbn));
create table author(authorID varchar(10), name varchar(30),
email varchar(20), constraint pk primary key(authorID));
create table ba(isbn varchar(10), authorID varchar(10),
constraint fk1 foreign key(isbn) references books(isbn),
constraint fk2 foreign key(authorID) references
author(authorID));
desc books;
desc author;
desc ba;

Output:
Query OK, 0 rows affected (0.69 sec)
Query OK, 0 rows affected (0.41 sec)
Query OK, 0 rows affected (0.17 sec)
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | YES | UNI | NULL | |
| price | float | NO | | NULL | |
| qty | int(3) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.45 sec)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| authorID | varchar(10) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | YES | MUL | NULL | |
| authorID | varchar(10) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

1. Constraints

Question: a. Add not null constraint to title in books table

Query:
alter table books modify title varchar2(20) not null;
desc books;

Output:
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | NO | | NULL | |
| qty | int(3) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

Question: b. Add unique constraint to email in authors table

Query:
alter table author add constraint myuni1 unique(email);
desc author;

Output:
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| authorID | varchar(10) | NO | PRI | NULL | |
| email | varchar(20) | YES | UNI | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Question: c. Alter not null constraint in price attribute in


books table and set the check constraint so that value is
greater than 0.0

Query:
alter table books modify price float(5) null;
alter table books add constraint chk1 check(price>0.0);
desc books;

Output:
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | YES | | NULL | |
| qty | int(3) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: d. Drop not null constraint in books table - qty


attribute

Query:
alter table books modify qty int(3) null;
desc books;

Output:
Query OK, 0 rows affected (0.23 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | YES | | NULL | |
| qty | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: e. Set a default value of qty in books table as 0

Query:
alter table books alter qty int set default 0;
desc books;

Output:
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | NO | PRI | NULL | |
| title | varchar(20) | NO | UNI | NULL | |
| price | float | YES | | NULL | |
| qty | int(3) | YES | | 0 | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Question: f. Drop any one foreign key constraint.

Query:
alter table ba drop foreign key fk1;
desc ba;
alter table ba drop foreign key fk1;

Output:
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| isbn | varchar(10) | YES | MUL | NULL | |
| authorID | varchar(10) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

ERROR 1091 (42000): Can't DROP 'fk1'; check that column/key


exists

Question: a. List the authorid without repetition from


book_authors

Query: select distinct authorID from ba;


Output:
+----------+
| authorID |
+----------+
| 1 |
| 2 |
| 3 |
+----------+
3 rows in set (0.01 sec)

Question: b. Update the email of an author

Query:
update author set email='kelvinjones@xyz.com' where authorID=4;
select * from author;

Output:
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
+----------+--------------+---------------------+
| authorID | name | email |
+----------+--------------+---------------------+
| 1 | Tan Ah Teck | teck@nowhere.com |
| 2 | Mohamed Ali | ali@somewhere.com |
| 3 | Kumar | kumar@abc.com |
| 4 | Kelvin Jones | kelvinjones@xyz.com |
+----------+--------------+---------------------+
4 rows in set (0.00 sec)

Question: c. Insert records as given in the above tables

SQL> insert into books values(1001,'Java for Dummies',11.11,11);


Query OK, 1 row affected (0.08 sec)

SQL> insert into books values(1002,'Only Java',22.22,22);


Query OK, 1 row affected (0.08 sec)

SQL> insert into books values(1003,'Java ABC',33.33,33);


Query OK, 1 row affected (0.08 sec)

SQL> insert into books values(1004,'Java 123',44.44,44);


Query OK, 1 row affected (0.12 sec)

SQL> insert into author values(1,'Tan Ah


Teck','teck@nowhere.com');
Query OK, 1 row affected (0.04 sec)

SQL> insert into author values(2,'Mohamed


Ali','ali@somewhere.com');
Query OK, 1 row affected (0.07 sec)

SQL> insert into author values(3,'Kumar','kumar@abc.com');


Query OK, 1 row affected (0.07 sec)

SQL> insert into author values(4,'Kelvin


Jones','kelvin@xzy.com');
Query OK, 1 row affected (0.08 sec)

SQL> insert into ba values(1001,1);


Query OK, 1 row affected (0.04 sec)

SQL> insert into ba values(1001,2);


Query OK, 1 row affected (0.02 sec)

SQL> insert into ba values(1001,3);


Query OK, 1 row affected (0.09 sec)

SQL> insert into ba values(1002,1);


Query OK, 1 row affected (0.09 sec)

SQL> insert into ba values(1002,3);


Query OK, 1 row affected (0.03 sec)

SQL> insert into ba values(1003,2);


Query OK, 1 row affected (0.03 sec)

SQL> insert into ba values(1004,2);


Query OK, 1 row affected (0.04 sec)
SQL> select * from books;
+------+------------------+-------+------+
| isbn | title | price | qty |
+------+------------------+-------+------+
| 1001 | Java for Dummies | 11.11 | 11 |
| 1002 | Only Java | 22.22 | 22 |
| 1003 | Java ABC | 33.33 | 33 |
| 1004 | Java 123 | 44.44 | 44 |
+------+------------------+-------+------+
4 rows in set (0.00 sec)

SQL> select * from author;


+----------+--------------+-------------------+
| authorID | name | email |
+----------+--------------+-------------------+
| 1 | Tan Ah Teck | teck@nowhere.com |
| 2 | Mohamed Ali | ali@somewhere.com |
| 3 | Kumar | kumar@abc.com |
| 4 | Kelvin Jones | kelvin@xzy.com |
+----------+--------------+-------------------+
4 rows in set (0.00 sec)

SQL> select * from ba;


+------+----------+
| isbn | authorID |
+------+----------+
| 1001 | 1 |
| 1001 | 2 |
| 1001 | 3 |
| 1002 | 1 |
| 1002 | 3 |
| 1003 | 2 |
| 1004 | 2 |
+------+----------+
7 rows in set (0.00 sec)

Question: d. Increase the price of all books with 10% of its


price
Query:
update books set price=price*1.1;
select * from books;

Output:
Query OK, 4 rows affected (0.08 sec)
Rows matched: 4 Changed: 4 Warnings: 0
+------+------------------+--------+------+
| isbn | title | price | qty |
+------+------------------+--------+------+
| 1001 | Java for Dummies | 12.221 | 11 |
| 1002 | Only Java | 24.442 | 22 |
| 1003 | Java ABC | 36.663 | 33 |
| 1004 | Java 123 | 48.884 | 44 |
+------+------------------+--------+------+
4 rows in set (0.00 sec)

Question: e. Sort the authors table in ascending order based on


their names

Query: select * from author order by name;

Output:
+----------+--------------+---------------------+
| authorID | name | email |
+----------+--------------+---------------------+
| 4 | Kelvin Jones | kelvinjones@xyz.com |
| 3 | Kumar | kumar@abc.com |
| 2 | Mohamed Ali | ali@somewhere.com |
| 1 | Tan Ah Teck | teck@nowhere.com |
+----------+--------------+---------------------+
4 rows in set (0.00 sec)

Question: f. Sort the books table by price in descending and qty


in ascending order.

Query: select * from books order by price desc, qty asc;


Output:
+------+------------------+--------+------+
| isbn | title | price | qty |
+------+------------------+--------+------+
| 1004 | Java 123 | 48.884 | 44 |
| 1003 | Java ABC | 36.663 | 33 |
| 1002 | Only Java | 24.442 | 22 |
| 1001 | Java for Dummies | 12.221 | 11 |
+------+------------------+--------+------+
4 rows in set (0.00 sec)
Ex. No 3 – Aggregation Functions

1. Add school attribute to student relation and update the


value for all students.

SQL> update student set school='cse' where regno='16BCE1001';


Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

SQL> update student set school='cse' where regno='16BCE1002';


Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SQL> update student set school='it' where regno='16BCE1003';


Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SQL> update student set school='ece' where regno='16BCE1004';


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SQL> update student set school='ecm' where regno='16BCE1005';


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SQL> select * from student;


+-----------+-------+------+------------+--------+
| regno | sname | cgpa | phone | school |
+-----------+-------+------+------------+--------+
| 16BCE1001 | harsh | 9 | 8979867898 | cse |
| 16BCE1002 | alex | 9.3 | 8979867899 | cse |
| 16BCE1003 | abhi | 8.9 | 8979867800 | it |
| 16BCE1004 | virat | 7.8 | 8979867810 | ece |
| 16BCE1005 | anmol | 8.3 | 8979867811 | ecm |
+-----------+-------+------+------------+--------+
5 rows in set (0.00 sec)

2. Count the number of courses with keyword ‘data’

SQL> select count(*) from course where cname like '%data%';


+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)

3. Display the total credits

SQL> select sum(credits) from course;


+--------------+
| sum(credits) |
+--------------+
| 20 |
+--------------+
1 row in set (0.00 sec)

4. Display the average cgpa for each school

SQL> select school,avg(cgpa) from student group by school;


+--------+-------------------+
| school | avg(cgpa) |
+--------+-------------------+
| cse | 9.150000095367432 |
| it | 8.899999618530273 |
| ece | 7.800000190734863 |
| ecm | 8.300000190734863 |
+--------+-------------------+
4 rows in set (0.00 sec)

5. Display the maximum and minimum cgpa for each school

SQL> select school,max(cgpa),min(cgpa) from student group by


school;
+--------+-----------+-----------+
| school | max(cgpa) | min(cgpa) |
+--------+-----------+-----------+
| cse | 9.3 | 9 |
| it | 8.9 | 8.9 |
| ece | 7.8 | 7.8 |
| ecm | 8.3 | 8.3 |
+--------+-----------+-----------+
4 rows in set (0.00 sec)

6. Display the maximum of average cgpa for each school

Since, we cannot use nested functions in SQL, I have used sub-


queries for the given question.

SQL> select max(x.average) from (select avg(cgpa) as average


from student group by school) x;
+-------------------+
| max(x.average) |
+-------------------+
| 9.150000095367432 |
+-------------------+
1 row in set (0.00 sec)

7. Display the number of students whose cgpa is >9


SQL> select count(cgpa) from student where cgpa>9;
+-------------+
| count(cgpa) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)

Ex. No 4 – Joins

1. List the students details who have taken c1002 [cartesian


product and implicit inner join]

The cartesian product and the implicit inner join are the same.

SQL> select * from student,course_student where


student.regno=course_student.regno and ccode='c1002';
+-----------+-------+------+------------+--------+-----------
+-------+
| regno | sname | cgpa | phone | school | regno |
ccode |
+-----------+-------+------+------------+--------+-----------
+-------+
| 16BCE1001 | harsh | 9 | 8979867898 | cse | 16BCE1001 |
c1002 |
| 16BCE1004 | virat | 7.8 | 8979867810 | ece | 16BCE1004 |
c1002 |
+-----------+-------+------+------------+--------+-----------
+-------+
2 rows in set (0.00 sec)

2. List the faculty names with designation “Professor” who


teaches CSE2004 [Natural Join and Explicit Inner Join]
SQL> select fname from faculty natural join course_faculty where
designation='prof.' and ccode='c1002';
+-------+
| fname |
+-------+
| mohan |
+-------+
1 row in set (0.01 sec)

SQL> select fname from faculty inner join course_faculty where


designation='prof.' and ccode='c1002';
+-------+
| fname |
+-------+
| mohan |
| rohan |
| john |
+-------+
3 rows in set (0.01 sec)

3. List the courses with more than 2 credits which was taught
by the faculty “mohan” [Equi Join and Non Equi Join]

SQL> select c.ccode,c.cname from faculty f, course_faculty cf,


course c where f.fid=cf.fid and cf.ccode=c.ccode and
f.fname='mohan' and c.credits>2;
+-------+-------+
| ccode | cname |
+-------+-------+
| c1002 | DLD |
+-------+-------+
1 row in set (0.01 sec)

SQL> select c.ccode,c.cname from faculty f, course_faculty cf,


course c where f.fid>=cf.fid and cf.ccode>=c.ccode and
f.fname='mohan' and c.credits>2;
+-------+-------+
| ccode | cname |
+-------+-------+
| c1002 | DLD |
+-------+-------+
1 row in set (0.00 sec)

4. List the students with >9 CGPA and has taken CSE3002 course
taught by “Prof. Sai” [Cross Join and Left Outer Join]

SQL> select distinct student.regno, student.sname from student


cross join course_student cross join course cross join
course_faculty cross join faculty where student.cgpa>9 and
course.cname='DBMS' and faculty.fname='rohan';
+-----------+-------+
| regno | sname |
+-----------+-------+
| 16BCE1002 | alex |
+-----------+-------+
1 row in set (0.00 sec)

SQL> select s.sname, s.regno from student s left outer join


course_student cs on s.regno=cs.regno left outer join course c
on cs.ccode=c.ccode left outer join course_faculty cf on
c.ccode=cf.ccode left outer join faculty f on cf.fid=f.fid where
s.cgpa>9 and c.cname='DBMS' and f.fname='rohan';
+-------+-----------+
| sname | regno |
+-------+-----------+
| alex | 16BCE1002 |
+-------+-----------+
1 row in set (0.00 sec)
5. List the register number, name, course code, course name and
faculty empid, name involved in the course CSE1001 [Right Outer
and Full Outer Join]

SQL> select s.sname, s.regno, c.ccode, c.cname, f.fid, f.fname


from student s right outer join course_student cs on
s.regno=cs.regno right outer join course c on cs.ccode=c.ccode
right outer join course_faculty cf on c.ccode=cf.ccode right
outer join faculty f on cf.fid=f.fid where c.ccode='c1002';
+-------+-----------+-------+-------+------+-------+
| sname | regno | ccode | cname | fid | fname |
+-------+-----------+-------+-------+------+-------+
| harsh | 16BCE1001 | c1002 | DLD | 1001 | mohan |
| virat | 16BCE1004 | c1002 | DLD | 1001 | mohan |
+-------+-----------+-------+-------+------+-------+
2 rows in set (0.00 sec)

Since, There is not full outer join command in SQL I have


implemented it using union command.

SQL> select s.sname, s.regno, c.ccode, c.cname, f.fid, f.fname


from student s right outer join course_student cs on
s.regno=cs.regno right outer join course c on cs.ccode=c.ccode
right outer join course_faculty cf on c.ccode=cf.ccode right
outer join faculty f on cf.fid=f.fid where c.ccode='c1002' union
select s.sname, s.regno, c.ccode, c.cname, f.fid, f.fname from
student s left outer join course_student cs on s.regno=cs.regno
left outer join course c on cs.ccode=c.ccode left outer join
course_faculty cf on c.ccode=cf.ccode left outer join faculty f
on cf.fid=f.fid where c.cname='c1002';
+-------+-----------+-------+-------+------+-------+
| sname | regno | ccode | cname | fid | fname |
+-------+-----------+-------+-------+------+-------+
| harsh | 16BCE1001 | c1002 | DLD | 1001 | mohan |
| virat | 16BCE1004 | c1002 | DLD | 1001 | mohan |
+-------+-----------+-------+-------+------+-------+
2 rows in set (0.00 sec)
Ex. No 6 – Revision

SQL> select * from student;

REGNO SNAME CGPA


--------- -------------------- ----------
16BCE1001 harsh 9
16BCE1002 abhishek 9
16BCE1003 swapnil 9
16BCE1004 mudit 9
16BCE1005 kaushik 8

SQL> select * from faculty;

EMPID FNAME DESIGNATION


----- -------------------- ----------------
f1001 alok prof.
f1002 ashok asst. prof.
f1003 neha prof.
f1004 radha asst. prof.
f1005 tony prof.

SQL> select * from course_student;

REGNO CCODE
--------- ----------
16BCE1001 CSE1004
16BCE1001 CSE3002
16BCE1001 HUM1001
16BCE1001 CHY1002
16BCE1002 CSE3002
16BCE1003 CSE2004
16BCE1004 CSE1004
16BCE1005 CSE3002

8 rows selected.

SQL> select * from course faculty;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE1004 networks 4
CSE2004 dbms 4
HUM1001 humanities 2
CSE3002 iwp 4
CHY1002 chemistry 3

SQL> select * from course;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE1004 networks 4
CSE2004 dbms 4
HUM1001 humanities 2
CSE3002 iwp 4
CHY1002 chemistry 3

1. List the students who have took CSE2004 taught by Prof. alok
SQL> select regno from course_student natural join
course_faculty natural join faculty where ccode='CSE2004' and
fname='alok';

REGNO
---------
16BCE1003

2. Count the number of students who have taken CSE3002 under


faculty id f1005

SQL> select count(regno) from course_student natural join


course_faculty where ccode='CSE3002' and empid='f1005';

COUNT(REGNO)
------------
3

3. Count the students who have got >8.5 CGPA whose name starts
with a

SQL> select * from student where cgpa>8.5 and sname like 'a%';

REGNO SNAME CGPA


--------- -------------------- ----------
16BCE1002 abhishek 9

4. List the faculty members who teaches C and OS

SQL> select fname from course natural join course_faculty


natural join faculty where cname='dbms' or cname='networks';

FNAME
--------------------
ashok
alok

5. Name the course which has more than 6 characters in it


SQL> select * from course where length(cname)>6;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE1004 networks 4
HUM1001 humanities 2
CHY1002 chemistry 3

6. List the courses with more than 3 credits which was took by
the student harsh

SQL> select cname from course_student natural join course


natural join student where credits>3 and sname='harsh';

CNAME
--------------------
networks

7. Count the number of courses taught by Prof. Ashok

SQL> select count(ccode) from course_faculty natural join


faculty where fname='ashok';

COUNT(CCODE)
------------
2

8. Count the courses which was not taken by the student geeta

SQL> select 5-count(ccode) from course_student natural join


student where sname='harsh';

5-COUNT(CCODE)
--------------
1

9. Name all designations of the faculty members who have


exactly 4 characters in their names

SQL> select designation from faculty where length(fname)=4;


DESIGNATION
--------------------
prof.
prof.
prof.

10. List the student details with their faculty details

SQL> select regno,sname,cgpa,empid,fname,designation from


course_student natural join course_faculty natural join student
natural join faculty;

REGNO SNAME CGPA EMPID FNAME


--------- -------------------- ---------- -----
--------------------
DESIGNATION
--------------------
16BCE1001 harsh 9 f1004 radha
asst. prof.

16BCE1001 harsh 9 f1003 neha


prof.

16BCE1001 harsh 9 f1005 tony


prof.

REGNO SNAME CGPA EMPID FNAME


--------- -------------------- ---------- -----
--------------------
DESIGNATION
--------------------
16BCE1001 harsh 9 f1002 ashok
asst. prof.

16BCE1002 abhishek 9 f1005 tony


prof.

16BCE1003 swapnil 9 f1001 alok


prof.

REGNO SNAME CGPA EMPID FNAME


--------- -------------------- ---------- -----
--------------------
DESIGNATION
--------------------
16BCE1004 mudit 9 f1002 ashok
asst. prof.

16BCE1005 kaushik 8 f1005 tony


prof.

8 rows selected.

Ex. No 6 – Set Operations and Subqueries

With the following created table


student(regno, sname, cgpa)
course(ccode, cname, credits)
faculty(empid, fname, designation)
course_student(regno, ccode)
course_faculty(fid, ccode)
student_faculty(regno,fid,role)

OLD TABLES:
SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 8.9
16BCE1004 anuj 9.1
16BCE1002 abhishek 8.8
16BCE1003 sqapnil 8.4
16BCE1005 mudit 8.8

SQL> select * from course;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE2004 dbms 4
CSE1002 chemistry 4
CSE1004 networks 4
CSE3002 iwp 4
HUM1001 humanities 3

SQL> select * from faculty;

EMPID FNAME DESIGNATION


---------- -------------------- --------------------
f1001 alok prof.
f1005 tony prof.
f1002 ashok asst. prof.
f1003 neha prof.
f1004 radha asst. prof.

SQL> select * from course_student;

REGNO CCODE
---------- ----------
16BCE1001 HUM1001
16BCE1003 HUM1001
16BCE1003 CSE2004
16BCE1001 CSE1004
16BCE1001 CSE3002
16BCE1001 CSE1002
16BCE1002 CSE3002
16BCE1004 CSE1004
16BCE1005 CSE3002
16BCE1003 CSE2004

10 rows selected.

SQL> select * from course_faculty;

CCODE EMPID
---------- ----------
CSE2004 f1003
CSE3002 f1004
CSE1004 f1002
CSE1002 f1001
HUM1001 f1005

SQL> select * from student_faculty;

REGNO EMPID ROLE


---------- ---------- --------------------
16BCE1001 f1002 teacher
16BCE1001 f1004 teacher
16BCE1001 f1005 teacher
16BCE1001 f1003 proctor
16BCE1002 f1004 teacher
16BCE1003 f1003 teacher
16BCE1004 f1002 teacher
16BCE1005 f1004 teacher
16BCE1002 f1001 proctor
16BCE1001 f1001 teacher
16BCE1003 f1002 proctor

REGNO EMPID ROLE


---------- ---------- --------------------
16BCE1004 f1005 proctor
16BCE1003 f1003 teacher
16BCE1005 f1003 proctor
16BCE1003 f1005 teacher
15 rows selected.

Write SQL Queries for the following:

Part A – Set Operations


1. List the courses which are handled by a faculty and have
students registered it.

SQL> select ccode from course_faculty intersect select ccode


from course_student;

CCODE
----------
CSE1002
CSE1004
CSE2004
CSE3002
HUM1001

2. List the courses which are handled by a faculty and have no


students registered in it.

SQL> select ccode from course_faculty minus select ccode from


course_student;

no rows selected

3. List the courses which are not handled by any faculty

SQL> select ccode from course minus select ccode from


course_faculty;

no rows selected

4. List the students who haven’t registered for any course.

SQL> select regno from student minus select regno from


course_student;

no rows selected
5. List the faculty who are not handling any courses.

SQL> select empid from faculty minus select empid from


course_faculty;

no rows selected

Part B – Sub queries

With Select

6. List the students’ regno who took courses with credits more
than 3

SQL> select regno from course_student where ccode in (select


ccode from course where credits>3);

REGNO
----------
16BCE1003
16BCE1003
16BCE1004
16BCE1001
16BCE1005
16BCE1002
16BCE1001

7 rows selected.

7. List the faculty details who are not Professors and


Associate Professors

SQL> select * from faculty where designation not in ('asst.


prof.','prof.');

no rows selected

With Update
8. Increase the cgpa by 0.5 for the students who have taken
CSE3002

SQL> update student set cgpa=cgpa+0.5 where regno in(select


regno from course_student where ccode='CSE3002');

3 rows updated.

SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 9.4
16BCE1004 anuj 9.1
16BCE1002 abhishek 9.3
16BCE1003 sqapnil 8.4
16BCE1005 mudit 9.3

9. Update the credits of the courses except CSE3002

SQL> update course set credits=3 where ccode in (select ccode


from course where ccode != 'CSE3002');

4 rows updated.

SQL> select * from course;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE2004 dbms 3
CSE1002 chemistry 3
CSE1004 networks 3
CSE3002 iwp 4
HUM1001 humanities 3

With Delete

10. Delete all courses which was not taken by any of the
students
SQL> delete from course where ccode in (select ccode from course
minus select ccode from course_student);

0 rows deleted.

Subquery with aggregation

11. List the student registernumber with maximum cgpa.

SQL> select regno from student where cgpa=(select max(cgpa) from


student);

REGNO
----------
16BCE1001

SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 9.4
16BCE1004 anuj 9.1
16BCE1002 abhishek 9.3
16BCE1003 sqapnil 8.4
16BCE1005 mudit 9.3
Ex. No 7 – PL/SQL Basics

1. Write a PL/SQL structure to find the greatest number among


three numbers using if-else.

SQL> declare
2 a number:=10;
3 b number:=20;
4 c number:=30;
5 begin
6 if(a>b and a>c) then
7 dbms_output.put_line('a is greatest');
8 elsif(b>a and b>c) then
9 dbms_output.put_line('b is greatest');
10 else
11 dbms_output.put_line('c is greatest');
12 end if;
13 end;
14 /
c is greatest

PL/SQL procedure successfully completed.

2. Write a PL/SQL structure to find a given number is Armstrong


number or not using loops.
[Individual digits cube is summed – result will be the original
number]
Example 1:

a=153
b=1^3+5^3+3^3 = 1+125+27=153
a=153 is equal to b=153, so 153 is an Armstrong number.

SQL> declare
2 n integer:=153;
3 s integer:=0;
4 r integer;
5 m integer;
6 begin
7 m:=n;
8 while m>0
9 loop
10 r:=mod(m,10); --mod is used to find the
remainder
11 s:=s+(r*r*r);
12 m:=trunc(m/10); --trunc is used to reduce
decimal points to avoid round operation
13 end loop;
14 if n=s
15 then
16 dbms_output.put_line('Armstrong Number');
17 else
18 dbms_output.put_line('Not an Armstrong Number');
19 end if;
20 end;
21 /
Armstrong Number

PL/SQL procedure successfully completed.

3. Write a PL/SQL structure to find addition, subtraction,


multiplication and division of two numbers using ‘case’

SQL> declare
2 a number:=10;
3 b number:=20;
4 c number;
5 ch varchar(20);
6 begin
7 ch:='add';
8 case ch
9 when 'add' then
10 c:=a+b;
11 dbms_output.put_line('Addition is '||c);
12 when 'subtract' then
13 c:=a-b;
14 dbms_output.put_line('Subtraction is '||
c);
15 when 'multiply' then
16 c:=a*b;
17 dbms_output.put_line('Multiplication is
'||c);
18 when 'divide' then
19 c:=a/b;
20 dbms_output.put_line('Division is '||c);
21 end case;
22 end;
23 /
Addition is 30

PL/SQL procedure successfully completed.


Ex. No. 8 – PL/SQL Functions and Procedures

For the already created table of student schema,


Student(regno, name, cgpa)
Course(ccode, cname, credits)
Student_course( regno, ccode)

SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 9.4
16BCE1004 anuj 9.1
16BCE1002 abhishek 9.3
16BCE1003 sqapnil 8.4
16BCE1005 mudit 9.3

SQL> select * from course;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE2004 dbms 3
CSE1002 chemistry 3
CSE1004 networks 3
CSE3002 iwp 4
HUM1001 humanities 3

SQL> select * from course_student;

REGNO CCODE
---------- ----------
16BCE1001 HUM1001
16BCE1003 HUM1001
16BCE1003 CSE2004
16BCE1001 CSE1004
16BCE1001 CSE3002
16BCE1001 CSE1002
16BCE1002 CSE3002
16BCE1004 CSE1004
16BCE1005 CSE3002
16BCE1003 CSE2004

10 rows selected.

1. Write a PL/SQL procedure to update the cgpa of the student


with regno 101

SQL> create or replace procedure pro_update(x1 number, x2


varchar2) as
2 Begin
3 update student set cgpa=x1 where regno=x2;
4 dbms_output.put_line('Record Updated');
5 end;
6 /

Procedure created.

SQL> exec pro_update(9.3,'16BCE1001');


Record Updated

PL/SQL procedure successfully completed.

SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 9.3
16BCE1004 anuj 9.1
16BCE1002 abhishek 9.3
16BCE1003 sqapnil 8.4
16BCE1005 mudit 9.3

2. Write a function to find the maximum cgpa of all the


students.

SQL> create or replace function cgpa


2 return number is
3 a number;
4 begin
5 select max(cgpa) into a
6 from student;
7 return a;
8 end;
9 /

Function created.

SQL> begin
2 dbms_output.put_line('Maximum cgpa is '||cgpa());
3 end;
4 /
Maximum cgpa is 9.3

PL/SQL procedure successfully completed.

3. Insert a column named marks in student_course relation and


update the column values.

SQL> begin
2 execute immediate 'alter table course_student add marks
number';
3 end;
4 /

PL/SQL procedure successfully completed.

SQL> desc course_student;


Name Null? Type
----------------------------------------- --------
----------------------------
REGNO VARCHAR2(10)
CCODE VARCHAR2(10)
MARKS NUMBER
SQL> create or replace procedure pro_update(x1 number, x2
varchar2, x3 varchar2) as
2 Begin
3 update course_student set marks=x1 where regno=x2 and
ccode=x3;
4 dbms_output.put_line('Record Updated');
5 end;
6 /

Procedure created.

SQL> exec pro_update(83,'16BCE1001','HUM1001');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(87,'16BCE1003','HUM1001');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(80,'16BCE1003','CSE2004');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(97,'16BCE1001','CSE2004');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(98,'16BCE1001','CSE3002');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(90,'16BCE1001','CSE1002');


Record Updated

PL/SQL procedure successfully completed.


SQL> exec pro_update(92,'16BCE1002','CSE3002');
Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(87,'16BCE1004','CSE1004');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(87,'16BCE1005','CSE3002');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(87,'16BCE1003','CSE2004');


Record Updated

PL/SQL procedure successfully completed.

SQL> exec pro_update(84,'16BCE1001','CSE1004');


Record Updated

PL/SQL procedure successfully completed.

SQL> select * from course_student;

REGNO CCODE MARKS


---------- ---------- ----------
16BCE1001 HUM1001 83
16BCE1003 HUM1001 87
16BCE1003 CSE2004 87
16BCE1001 CSE1004 84
16BCE1001 CSE3002 98
16BCE1001 CSE1002 90
16BCE1002 CSE3002 92
16BCE1004 CSE1004 87
16BCE1005 CSE3002 87
16BCE1003 CSE2004 87
10 rows selected.

4. Get the marks of the the regno 102 and display his grade

SQL> create or replace procedure pro_display(x1 varchar2) as


2 begin
3 for rec in(select marks,ccode from course_student where
regno=x1)
4 loop
5 dbms_output.put_line('CCODE: '||rec.ccode);
6 dbms_output.put_line('Marks: '||rec.marks);
7 end loop;
8 select cgpa into cgpa from student where regno=x1;
9 dbms_output.put_line('CGPA: '||cgpa);
10 end;
11 /

Procedure created.

SQL> exec pro_display('16BCE1001');


CCODE: HUM1001
Marks: 83
CCODE: CSE1004
Marks: 84
CCODE: CSE3002
Marks: 98
CCODE: CSE1002
Marks: 90
CGPA: 9.3

PL/SQL procedure successfully completed.


Ex. No – 9 – SQL Revision

Hotel (hotelNo, hotelName, city)


Room (roomNo, hotelNo, type, price)
Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo)
Guest (guestNo, guestName, guestAddress)

Write the SQL and its corresponding Relational Algebra


expression for the following

1 Create table for the above schema with primary and foreign key
constraints.
SQL> create table hotel(hotelNo number(10), hotelName
varchar2(20), city varchar2(20), constraint pk1 primary
key(hotelNo));

Table created.

SQL> desc hotel;


Name Null? Type
----------------------------------------- --------
----------------------------
HOTELNO NOT NULL NUMBER(10)
HOTELNAME VARCHAR2(20)
CITY VARCHAR2(20)

SQL> create table room(roomNo number(5), hotelNo number(10),


type varchar2(10), price number(10,2), constraint fk1 foreign
key(hotelNo) references hotel(hotelNo), constraint pk2 primary
key(roomNo));

Table created.

SQL> desc room;


Name Null? Type
----------------------------------------- --------
----------------------------
ROOMNO NOT NULL NUMBER(5)
HOTELNO NUMBER(10)
TYPE VARCHAR2(10)
PRICE NUMBER(10,2)

SQL> create table booking(hotelno number(10), guestno


number(10), dateFrom date, dateTo date, roomNo number(5),
constraint fk2 foreign key(hotelNo) references hotel(hotelNo),
constraint pk3 primary key(guestNo));

Table created.

SQL> desc booking;


Name Null? Type
----------------------------------------- --------
----------------------------
HOTELNO NUMBER(10)
GUESTNO NOT NULL NUMBER(10)
DATEFROM DATE
DATETO DATE
ROOMNO NUMBER(5)

SQL> create table guest(guestNo number(10), guestName


varchar2(20), guestAddress varchar2(50), constraint fk3 foreign
key(guestNo) references booking(guestNo));

Table created.

SQL> desc guest;


Name Null? Type
----------------------------------------- --------
----------------------------
GUESTNO NUMBER(10)
GUESTNAME VARCHAR2(20)
GUESTADDRESS VARCHAR2(50)

2 After table creation, modify the datatype of an attribute with


its size.

SQL> alter table room modify price number(9,2);


Table altered.

SQL> desc room;


Name Null? Type
----------------------------------------- --------
----------------------------
ROOMNO NOT NULL NUMBER(5)
HOTELNO NUMBER(10)
TYPE VARCHAR2(10)
PRICE NUMBER(9,2)

SQL> alter table room modify type varchar2(30);

Table altered.

SQL> desc room;


Name Null? Type
----------------------------------------- --------
----------------------------
ROOMNO NOT NULL NUMBER(5)
HOTELNO NUMBER(10)
TYPE VARCHAR2(30)
PRICE NUMBER(9,2)

3 Add unique constraint for room number

SQL> alter table booking add constraint u1 unique(roomNo);

Table altered.

4 Insert records into the table

SQL> insert into hotel values(&hotelNo, '&hotelName', '&city');


Enter value for hotelno: 1
Enter value for hotelname: pacific
Enter value for city: LA
old 1: insert into hotel values(&hotelNo, '&hotelName',
'&city')
new 1: insert into hotel values(1, 'pacific', 'LA')

1 row created.

SQL> /
Enter value for hotelno: 2
Enter value for hotelname: meridian
Enter value for city: california
old 1: insert into hotel values(&hotelNo, '&hotelName',
'&city')
new 1: insert into hotel values(2, 'meridian', 'california')

1 row created.

SQL> insert into room values(&roomNo, &hotelNo, '&type',


&price);
Enter value for roomno: 101
Enter value for hotelno: 1
Enter value for type: deluxe
Enter value for price: 5000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(101, 1, 'deluxe', 5000)

1 row created.

SQL> /
Enter value for roomno: 102
Enter value for hotelno: 1
Enter value for type: executive deluxe
Enter value for price: 8000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(102, 1, 'executive deluxe',
8000)

1 row created.
SQL> /
Enter value for roomno: 103
Enter value for hotelno: 1
Enter value for type: deluxe
Enter value for price: 5000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(103, 1, 'deluxe', 5000)

1 row created.

SQL> /
Enter value for roomno: 201
Enter value for hotelno: 2
Enter value for type: deluxe
Enter value for price: 6000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(201, 2, 'deluxe', 6000)

1 row created.

SQL> /
Enter value for roomno: 202
Enter value for hotelno: 2
Enter value for type: executive deluxe
Enter value for price: 10000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(202, 2, 'executive deluxe',
10000)

1 row created.

SQL> /
Enter value for roomno: 203
Enter value for hotelno: 2
Enter value for type: executive deluxe
Enter value for price: 10000
old 1: insert into room values(&roomNo, &hotelNo, '&type',
&price)
new 1: insert into room values(203, 2, 'executive deluxe',
10000)

1 row created.

SQL> insert into booking


values(&hotelno,&guestno,'&datefrom','&dateto',&roomno);
Enter value for hotelno: 1
Enter value for guestno: 1
Enter value for datefrom: 01-AUG-18
Enter value for dateto: 15-AUG-18
Enter value for roomno: 101
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(1,1,'01-AUG-18','15-AUG-
18',101)

1 row created.

SQL> /
Enter value for hotelno: 2
Enter value for guestno: 2
Enter value for datefrom: 13-AUG-18
Enter value for dateto: 15-AUG-18
Enter value for roomno: 203
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(2,2,'13-AUG-18','15-AUG-
18',203)

1 row created.

SQL> /
Enter value for hotelno: 2
Enter value for guestno: 3
Enter value for datefrom: 13-SEP-18
Enter value for dateto: 20-SEP-18
Enter value for roomno: 201
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(2,3,'13-SEP-18','20-SEP-
18',201)

1 row created.

SQL> /
Enter value for hotelno: 1
Enter value for guestno: 4
Enter value for datefrom: 14-OCT-18
Enter value for dateto: 16-OCT-18
Enter value for roomno: 103
old 1: insert into booking
values(&hotelno,&guestno,'&datefrom','&dateto',&roomno)
new 1: insert into booking values(1,4,'14-OCT-18','16-OCT-
18',103)

1 row created.

SQL> insert into guest


values(&guestno,'&guestname','&guestaddress');
Enter value for guestno: 1
Enter value for guestname: Alessa
Enter value for guestaddress: Wall Street
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(1,'Alessa','Wall Street')

1 row created.

SQL> /
Enter value for guestno: 2
Enter value for guestname: Alex
Enter value for guestaddress: Sector 33
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(2,'Alex','Sector 33')

1 row created.
SQL> /
Enter value for guestno: 3
Enter value for guestname: Lisa
Enter value for guestaddress: LA
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(3,'Lisa','LA')

1 row created.

SQL> /
Enter value for guestno: 4
Enter value for guestname: Rick
Enter value for guestaddress: LA
old 1: insert into guest
values(&guestno,'&guestname','&guestaddress')
new 1: insert into guest values(4,'Rick','LA')

1 row created.

5 Count the number of hotels in each city

SQL> select count(hotelno) from hotel group by city;

COUNT(HOTELNO)
--------------
1
1

6 List different guests have made bookings for August?

SQL> select guestno from booking where (datefrom>'31-JUL-18' and


datefrom<'01-SEP-18') or (dateto>'31-JUL-18' and dateto<'01-SEP-
18');
GUESTNO
----------
1
2

7 List all hotel details of ‘Executive deluxe’ type

SQL> select distinct(hotelno),hotelname,city from room natural


full outer join hotel where type='executive deluxe';

HOTELNO HOTELNAME CITY


---------- -------------------- --------------------
2 meridian california
1 pacific LA

8 List the number of rooms in each hotel in London.

SQL> select count(roomno),hotelno from room group by hotelno;

COUNT(ROOMNO) HOTELNO
------------- ----------
3 1
3 2

9 List the unreserved rooms in each hotel.

SQL> select roomno,hotelno from room minus select roomno,hotelno


from booking;

ROOMNO HOTELNO
---------- ----------
102 1
202 2
10 List the details of all rooms at the Meridian Hotel,
including the name of the guest
staying in the room, if the room is occupied.

SQL> select
room.roomno,room.hotelno,room.type,room.price,guest.guestname
from room left join booking natural full outer join guest on
room.roomno=booking.roomno where room.hotelno in (select
distinct(hotelno) from hotel where hotelname='meridian');

ROOMNO HOTELNO TYPE PRICE


---------- ---------- ------------------------------ ----------
GUESTNAME
--------------------
203 2 executive deluxe 10000
Alex

201 2 deluxe 6000


Lisa

202 2 executive deluxe 10000


Ex. No 10 – PL/SQL – Cursors and triggers

For the already created table of student schema,


Student(regno, name, cgpa)
Course(ccode, cname, credits)
Student_course( regno, ccode)

SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 9.3
16BCE1004 anuj 9.1
16BCE1002 abhishek 9.3
16BCE1003 swapnil 8.4
16BCE1005 mudit 9.3

SQL> select * from course;

CCODE CNAME CREDITS


---------- -------------------- ----------
CSE2004 dbms 3
CSE1002 chemistry 3
CSE1004 networks 3
CSE3002 iwp 4
HUM1001 humanities 3

SQL> select * from course_student;

REGNO CCODE MARKS


---------- ---------- ----------
16BCE1001 HUM1001 83
16BCE1003 HUM1001 87
16BCE1003 CSE2004 87
16BCE1001 CSE1004 84
16BCE1001 CSE3002 98
16BCE1001 CSE1002 90
16BCE1002 CSE3002 92
16BCE1004 CSE1004 87
16BCE1005 CSE3002 87
16BCE1003 CSE2004 87

10 rows selected.

1. Write a PL/SQL Cursor to update the cgpa of the student with


regno 101

SQL> DECLARE
2 total_rows number(2);
3 BEGIN
4 UPDATE student SET cgpa='9.1' WHERE regno='16BCE1001';
5 IF sql%notfound THEN
6 dbms_output.put_line('no data updated');
7 ELSIF sql%found THEN
8 total_rows:=sql%rowcount;
9 dbms_output.put_line(total_rows||' data updated');
10 END IF;
11 END;
12 /
1 data updated

PL/SQL procedure successfully completed.

SQL> select * from student;

REGNO SNAME CGPA


---------- -------------------- ----------
16BCE1001 harsh 9.1
16BCE1004 anuj 9.1
16BCE1002 abhishek 9.3
16BCE1003 swapnil 8.4
16BCE1005 mudit 9.3

2. Write a cursor to List the credits of all the courses along


with their coursename.

SQL> DECLARE
2 c_cname course.cname%type;
3 c_credits course.credits%type;
4 CURSOR c_course is SELECT cname, credits FROM course;
5 BEGIN
6 OPEN c_course;
7 LOOP
8 FETCH c_course into c_cname, c_credits;
9 EXIT WHEN c_course%notfound;
10 dbms_output.put_line(c_credits||' '||c_cname);
11 END LOOP;
12 CLOSE c_course;
13 END;
14 /
3 dbms
3 chemistry
3 networks
4 iwp
3 humanities

PL/SQL procedure successfully completed.

3. List the name of the students who have more than 9 cgpa

SQL> DECLARE
2 s_sname student.sname%type;
3 CURSOR s_student is SELECT sname FROM student WHERE cgpa>9;
4 BEGIN
5 OPEN s_student;
6 LOOP
7 FETCH s_student into s_sname;
8 EXIT WHEN s_student%notfound;
9 dbms_output.put_line(s_sname);
10 END LOOP;
11 CLOSE s_student;
12 END;
13 /
harsh
anuj
abhishek
mudit

PL/SQL procedure successfully completed.

You might also like