SESSION-2019-20
LAB MANUAL SEM-4th Sem
Programme (UG/PG) : UG
Semester IV
Course Code : IT-405
Course Title : DATABASE MANAGEMENT SYSTEM
IT-405 IT/GGITS 1|125
/Database Management System Lab /SRM
LIST OF EXPERIMENTS
Course Code: IT-4002
Course Title: Database Management System
Exp. No. Title
1 To create a DDL to perform creation of table, alter, modify
and drop column.
2 To study the various DML commands and implement them
on the database.
3 Implementation of data and built in functions in sql
4 To execute Nested Queries, Join Queries, order-by, having
clause and string operation.
5 To perform set operators like Union, Intersect, Minus on a set
of tables.
6 Creating Views using SQL
7 To perform DCL Commands
8 Develop Programs units using Pl/ SQL
9 Write a program for implementation of cursors
10 Write a program for Implementation of triggers
11 Design and Implementation of Student Information System
FACULTY HoD
IT-405 IT/GGITS 2|125
/Database Management System Lab /SRM
List of Experiments: According to RGPV
1. To perform various SQL Commands of DDL, DML, DCL.
2. Write SQL Commands such as Insertion, deletion and updation for any schema.
3. To execute Nested Queries, Join Queries, order-by, having clause and string operation.
4. To perform set operators like Union, Intersect, Minus on a set of tables.
5. To execute various commands for GROUP functions (avg, count, max, min, Sum).
6. Write a PL/SQL block for transaction application using Triggers.
7. Write a DBMS program to prepare report for an application using function.
8. Designing of various Input screens/Forms.
9. Create reports using database connectivity of Front end with back end.
10. Create database Design with normalization and implementing in any application.
IT-405 IT/GGITS 3|125
/Database Management System Lab /SRM
HARDWARE AND SOFTWARE REQUIREMENTS
HARDWARE REQUIREMENTS:
INTEL PENTIUM 915 GV
80GB HDD
512MB DDR
SOFTWARE REQUIREMENTS:
ORACLE 10G or MY SQL, or DB2.
IT-405 IT/GGITS 4|125
/Database Management System Lab /SRM
[Link] IMPLEMENTATION OF DDL COMMANDS
AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
DDL COMMANDS
1. The Create Table Command: - it defines each column of the table uniquely. Each column
has minimum of three attributes, a name , data type and size.
Syntax:
Create table <table name> (<col1> <datatype>(<size>),<col2> <datatype><size>));
Ex:create table emp(empno number(4) primary key, ename char(10));
2. Modifying the structure of tables.
a) Add new columns
Syntax:
Alter table <tablename> add(<new col><datatype(size),<new col>datatype(size));
Ex:alter table emp add(sal number(7,2));
3. Dropping a column from a table.
Syntax:
Alter table <tablename> drop column <col>;
Ex:alter table emp drop column sal;
4. Modifying existing columns.
Syntax:
Alter table <tablename> modify(<col><newdatatype>(<newsize>));
Ex:alter table emp modify(ename varchar2(15));
IT-405 IT/GGITS 5|125
/Database Management System Lab /SRM
5. Renaming the tables
Syntax:
Rename <oldtable> to <new table>;
Ex:rename emp to emp1;
6. truncating the tables.
Syntax:
Truncate table <tablename>;
Ex:trunc table emp1;
7. Destroying tables.
Syntax:
Drop table <tablename>;
Ex:drop table emp;
IT-405 IT/GGITS 6|125
/Database Management System Lab /SRM
CREATION OF TABLE:
SYNTAX:
create table<tablename>(column1 datatype,column2 datatype...);
EXAMPLE:
SQL>create table std(sno number(5),sname varchar(20),age number(5),sdob date,sm1
number(4,2),sm2 number(4,2),sm3 number(4,4));
Table created.
SQL>insert into std values(101,‟AAA‟,16,‟03-jul-88‟,80,90,98); 1
row created.
SQL>insert into std values(102,‟BBB‟,18,‟04-aug-89‟,88,98,90); 1
row created.
OUTPUT:
Select * from std;
SNO SNAME AGE SDOB SM1 SM2 SM3
101 AAA 16 03-jul-88 80 90 98
102 BBB 18 04-aug-89 88 98 90
ALTER TABLE WITH ADD:
SQL>create table student(id number(5),name varchar(10),game varchar(20));
Table created.
SQL>insert into student values(1,‟mercy‟,‟cricket‟); 1
row created.
SYNTAX:
alter table<tablename>add(col1 datatype,col2 datatype..);
EXAMPLE:
SQL>alter table student add(age number(4));
SQL>insert into student values(2,‟sharmi‟,‟tennis‟,19);
IT-405 IT/GGITS 7|125
/Database Management System Lab /SRM
OUTPUT:
ALTER: select * from student;
ID NAME GAME
1 Mercy Cricket
ADD: select * from student;
ID NAME GAME AGE
1 Mercy cricket
2 Sharmi Tennis 19
ALTER TABLE WITH MODIFY:
SYNTAX:
Alter table<tablename>modify(col1 datatype,col2 datatype..);
EXAMPLE:
SQL>alter table student modify(id number(6),game varchar(25));
OUTPUT:
MODIFY
desc student;
NAME NULL? TYPE
Id Number(6)
Name Varchar(20)
Game Varchar(25)
Age Number(4)
IT-405 IT/GGITS 8|125
/Database Management System Lab /SRM
DROP:
SYNTAX: drop table<tablename>;
EXAMPLE:
SQL>drop table student;
SQL>Table dropped.
TRUNCATE TABLE
SYNTAX: TRUNCATE TABLE <TABLE NAME>;
Example: Truncate table stud;
DESC
Example: desc emp;
Name Null? Type
--------------------------------- --------
EmpNo NOT NULL number(5)
EName VarChar(15)
Job NOT NULL Char(10)
DeptNo NOT NULL number(3)
PHONE_NO number (10)
CONSTRAINTS:
Create table tablename (column_name1 data_ type constraints, column_name2 data_ type
constraints …)
Example:
Create table Emp ( EmpNo number(5), EName VarChar(15), Job Char(10) constraint un
unique, DeptNo number(3) CONSTRAINT FKey2 REFERENCES DEPT(DeptNo));
Create table stud (sname varchar2(20) not null, rollno number(10) not null,dob date not null);
IT-405 IT/GGITS 9|125
/Database Management System Lab /SRM
DOMAIN INTEGRITY
Example: Create table cust(custid number(6) not null, name char(10));
Alter table cust modify (name not null);
CHECK CONSTRAINT
Example: Create table student (regno number (6), mark number (3) constraint b check (mark
>=0 and mark <=100)); Alter table student add constraint b2 check (length(regno<=4));
ENTITY INTEGRITY
a) Unique key constraint
Example: Create table cust(custid number(6) constraint unique, name char(10)); Alter table
cust add(constraint c unique(custid));
b) Primary Key Constraint
Example: Create table stud(regno number(6) constraint primary key, name char(20));
Queries:
Q1. Create a table called EMP with the following structure.
Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Allow NULL for all columns except ename and job.
Solution:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create primary key constraint for each table as understand from logical table structure.
Ans:
SQL> create table emp(empno number(6),ename varchar2(20)not null,job varchar2(10) not
null, deptno number(3),sal number(7,2));
Table created.
IT-405 IT/GGITS 10 | 1 2 5
/Database Management System Lab /SRM
Q2: Add a column experience to the emp table.
experience numeric null allowed.
Solution:
1. Learn alter table syntax.
2. Define the new column and its data type.
3. Use the alter table syntax.
Ans: SQL> alter table emp add(experience number(2));
Table altered.
Q3: Modify the column width of the job field of emp table.
Solution:
1. Use the alter table syntax.
2. Modify the column width and its data type.
Ans: SQL> alter table emp modify(job varchar2(12));
Table altered.
SQL> alter table emp modify(job varchar(13));
Table altered.
Q4: Create dept table with the following structure.
Name Type
------------ ---------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
Deptno as the primarykey
Solution:
1. Understand create table syntax.
2. Decide the name of the table.
3. Decide the name of each column and its data type.
4. Use the create table syntax to create the said tables.
5. Create primary key constraint for each table as understand from logical table structure.
Ans:
SQL> create table dept(deptno number(2) primary key,dname varchar2(10),loc
varchar2(10));
Table created.
Q5: create the emp1 table with ename and empno, add constraints to check the empno value
while entering (i.e) empno > 100.
Solution:
1. Learn alter table syntax.
2. Define the new constraint [columns name type]
3. Use the alter table syntax for adding constraints.
Ans:
IT-405 IT/GGITS 11 | 1 2 5
/Database Management System Lab /SRM
SQL> create table emp1(ename varchar2(10),empno number(6) constraint
check(empno>100));
Table created.
Q6: drop a column experience to the emp table.
Solution:
1. Learn alter table syntax. Use the alter table syntax to drop the column.
Ans:
SQL> alter table emp drop column experience; Table altered.
Q7: Truncate the emp table and drop the dept table
Solution:
1. Learn drop, truncate table syntax.
Ans: SQL> truncate table emp; Table truncated.
QUESTIONS
1. Define DDL
2. What are constraints?
3. Categories of SQL Command.
4. Difference between truncate and drop.
5. Define primary and referential integrity.
RESULT:
Thus the DDL commands have been executed successfully.
IT-405 IT/GGITS 12 | 1 2 5
/Database Management System Lab /SRM
[Link] IMPLEMENTATION OF DML AND DCL COMMANDS
AIM;
To study the various DML commands and implement them on the database.
DML COMMANDS
DML commands are the most frequently used SQL commands and is used to query
and manipulate the existing database objects. Some of the commands are Insert, Select,
Update, Delete.
Insert Command This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The values must be
entered in the same order as they are defined.
Select Commands It is used to retrieve information from the table. It is generally referred to
as querying the table. We can either display all columns in a table or only specify column
from the table.
Update Command It is used to alter the column values in a table. A single column may be
updated or more than one column could be updated.
Delete command After inserting row in a table we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
Q1: Insert a single record into dept table.
Ans: SQL> insert into dept values (1,'IT','Tholudur');
1 row created.
Q2: Insert more than a record into emp table using a single insert command.
Ans: SQL> insert into emp values(&empno,'&ename','&job',&deptno,&sal);
Enter value for empno: 1
Enter value for ename: Mathi
Enter value for job: AP
Enter value for deptno: 1
Enter value for sal: 10000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)
IT-405 IT/GGITS 13 | 1 2 5
/Database Management System Lab /SRM
new 1: insert into emp values(1,'Mathi','AP',1,10000)
1 row created.
SQL> / Enter value for empno: 2
Enter value for ename: Arjun
Enter value for job: ASP
Enter value for deptno: 2
Enter value for sal: 12000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)
new 1: insert into emp values(2,'Arjun','ASP',2,12000)
1 row created.
SQL> / Enter value for empno: 3
Enter value for ename: Gugan
Enter value for job: ASP
Enter value for deptno: 1
Enter value for sal: 12000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)
new 1: insert into emp values(3,'Gugan','ASP',1,12000)
1 row created.
Q3: Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP
Ans: SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 1 12000
IT-405 IT/GGITS 14 | 1 2 5
/Database Management System Lab /SRM
SQL> update emp set sal=15000 where job='ASP'; 2 rows updated.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
Q4: Create a pseudo table employee with the same structure as the table emp and insert rows
into the table using select clauses.
Ans: SQL> create table employee as select * from emp;
Table created.
SQL> desc employee;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(6)
ENAME NOT NULL VARCHAR2(20)
JOB NOT NULL VARCHAR2(13)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Q5: select employee name, job from the emp table
Ans: SQL> select ename, job from emp;
ENAME JOB
-------------------- -------------
Mathi AP
Arjun ASP
Gugan ASP
Karthik Prof
Akalya AP
suresh lect
6 rows selected.
IT-405 IT/GGITS 15 | 1 2 5
/Database Management System Lab /SRM
Q6: Delete only those who are working as lecturer
Ans: SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
6 suresh lect 1 8000
6 rows selected.
SQL> delete from emp where job='lect';
1 row deleted.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
- --------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
Q7: List the records in the emp table orderby salary in ascending order.
Ans: SQL> select * from emp order by sal;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q8: List the records in the emp table orderby salary in descending order.
Ans: SQL> select * from emp order by sal desc;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
4 Karthik Prof 2 30000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
1 Mathi AP 1 10000
5 Akalya AP 1 10000
IT-405 IT/GGITS 16 | 1 2 5
/Database Management System Lab /SRM
Q9: Display only those employees whose deptno is 30.
Solution: Use SELECT FROM WHERE syntax.
Ans: SQL> select * from emp where deptno=1;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
3 Gugan ASP 1 15000
5 Akalya AP 1 10000
Q10: Display deptno from the table employee avoiding the duplicated values.
Solution:
1. Use SELECT FROM syntax.
2. Select should include distinct clause for the deptno.
Ans: SQL> select distinct deptno from emp;
DEPTNO
----------
1
IT-405 IT/GGITS 17 | 1 2 5
/Database Management System Lab /SRM
IMPLEMENTATION OF DATA AND BUILT IN FUNCTIONS IN SQL
CHARACTER/STRING FUNCTION:
SQL> select upper('welcome') from dual;
-----------
WELCOME
SQL> select upper('hai') from dual;
---
HAI
SQL> select lower('HAI') from dual;
LOW
---
hai
SQL> select initcap(„hello world') from dual;
INITCAP('Hello
--------------
Hello World
SQL> select ltrim(' hai') from dual;
LTR
---
hai
SQL> select rtrim('hai ')from dual;
IT-405 IT/GGITS 18 | 1 2 5
/Database Management System Lab /SRM
RTR
---
hai
SQL> select rtrim(' hai ')from dual;
RTRIM('
-------
hai
SQL> select concat('GGITS',' university')from dual;
------------------------
GGITS university
SQL> select length('GGITS‟)from dual;
LENGTH('GGITS')
----------------------
12
SQL> select replace('GGITS university', 'GGITS','RGPV')from dual;
----------------
RGPV university
SQL> select substr('GGITS', 7,6)from
dual; SUBSTR
------
lingam
IT-405 IT/GGITS 19 | 1 2 5
/Database Management System Lab /SRM
SQL> select rpad('hai',3,'*')from dual;
RPAD('
------
hai***
SQL> select lpad('hai',3,'*')from dual;
LPAD('
------
***hai
SQL> select replace('Dany','y','ie')from dual;
REPLACE
-------
Danie
SQL> select translate('cold','ld','ol')from dual;
TRANSL
------
cool
IT-405 IT/GGITS 20 | 1 2 5
/Database Management System Lab /SRM
DATE & TIME FUNCTION
SQL> select sysdate from dual;
SYSDATE
---------
07-APR-10
SQL> select round(sysdate)from dual;
ROUND(SYS
---------
07-APR-10
SQL> select add_months(sysdate,3)from dual;
ADD_MONTH
---------
07-JUL-10
SQL> select last_day(sysdate)from dual;
LAST_DAY(
---------
30-APR-10
SQL> select sysdate+20 from dual;
SYSDATE+2
---------
27-APR-10
SQL> select next_day(sysdate,'tuesday')from dual;
NEXT_DAY(
---------
13-APR-10
IT-405 IT/GGITS 21 | 1 2 5
/Database Management System Lab /SRM
NUMERIC FUNCTION
SQL> select round(15.6789)from dual;
ROUND(15.6789)
--------------
16
SQL> select ceil(23.20)from dual;
CEIL(23.20)
-----------
24
SQL> select floor(34.56)from dual;
FLOOR(34.56)
------------
34
SQL> select trunc(15.56743)from dual;
TRUNC(15.56743)
---------------
15
SQL> select sign(-345)from dual;
SIGN(-345)
----------
-1
SQL> select abs(-70)from dual;
ABS(-70)
---------
70
IT-405 IT/GGITS 22 | 1 2 5
/Database Management System Lab /SRM
MATH FUNCTION:
SQL> select abs(45) from dual;
ABS(45)
---------
45
SQL> select power(10,12) from dual;
POWER(10,12)
------------
1.000E+12
SQL> select mod(11,5) from dual;
MOD(11,5)
---------
SQL> select exp(10) from dual;
EXP(10)
---------
22026.466
SQL> select sqrt(225) from dual;
SQRT(225)
---------
15
IT-405 IT/GGITS 23 | 1 2 5
/Database Management System Lab /SRM
NESTED QUERIES AND JOIN QUERIES
Aim: To execute Nested Queries, Join Queries, order-by, having clause and string
operation.
Q1: Display all employee names and salary whose salary is greater than minimum salary of
the company and job title starts with ‗M„.
Solution:
1. Use select from clause.
2. Use like operator to match job and in select clause to get the result.
Ans: SQL> select ename,sal from emp where sal>(select min(sal) from emp where job like
'A%');
ENAME SAL
-------------------- ----------
Arjun 12000
Gugan 20000
Karthik 15000
Q2: Issue a query to find all the employees who work in the same job as Arjun.
Ans: SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ---------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 2 20000
4 Karthik AP 1 15000
SQL> select ename from emp where job=(select job from emp where ename='Arjun');
ENAME
--------------
Arjun
Gugan
IT-405 IT/GGITS 24 | 1 2 5
/Database Management System Lab /SRM
SET OPERATORS
Aim: To perform set operators like Union, Intersect, Minus on a set of tables.
QUERIES:
Q1: Display all the dept numbers available with the dept and emp tables avoiding duplicates.
Solution:
1. Use select from clause.
2. Use union select clause to get the result.
Ans: SQL> select deptno from emp union select deptno from dept;
DEPTNO
----------
12
30
40
Q2: Display all the dept numbers available with the dept and emp tables.
Solution:
1. Use select from clause.
2. Use union all in select clause to get the result.
Ans: SQL> select deptno from emp union all select deptno from dept;
DEPTNO
----------
1
IT-405 IT/GGITS 25 | 1 2 5
/Database Management System Lab /SRM
12
30
40
9 rows selected.
Q3: Display all the dept numbers available in emp and not in dept tables and vice versa.
Solution:
1. Use select from clause.
2. Use minus in select clause to get the result.
Ans: SQL> select deptno from emp minus select deptno from dept;
DEPTNO
----------
12
SQL> select deptno from dept minus select deptno from emp;
DEPTNO
----------
30
40
IT-405 IT/GGITS 26 | 1 2 5
/Database Management System Lab /SRM
VIEWS
Q1: The organization wants to display only the details of the employees those who are ASP.
Solution:
1. Create a view on emp table named managers
2. Use select from clause to do horizontal portioning
Ans: SQL> create view empview as select * from emp where job='ASP';
View created.
IT-405 IT/GGITS 27 | 1 2 5
/Database Management System Lab /SRM
SQL> select * from empview;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ---------- ---------- ----------
2 Arjun ASP 2 12000 3 Gugan ASP 2 20000
Q2: The organization wants to display only the details like empno, empname, deptno,
deptname of the employees. (Vertical portioning)
Solution: 1. Create a view on emp table named general
2. Use select from clause to do vertical partioning
Ans: SQL> create view empview1 as select ename,sal from emp;
View created.
Q3: Display all the views generated.
Ans: SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
EMPVIEW VIEW
EMPVIEW1 VIEW
Q4: Execute the DML commands on the view created.
Ans: SQL> select * from empview;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ---------- ---------- ----------
2 Arjun ASP 2 12000
3 Gugan ASP 2 20000
Q5: Drop a view.
IT-405 IT/GGITS 28 | 1 2 5
/Database Management System Lab /SRM
Ans: SQL> drop view empview1;
View dropped.
Q3: Issue a query to display information about employees who earn more than any employee
in dept 1. Ans: SQL> select * from emp where sal>(select max(sal) from emp where
empno=1); EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ---------- ---------- ----------
2 Arjun ASP 2 12000
3 Gugan ASP 2 20000
4 Karthik AP 1 15000
JOINS Tables used
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ---------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 2 20000
4 Karthik AP 1 15000
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
1 ACCOUNTING NEW YORK
2 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
EQUI-JOIN
Q4: Display the employee details, departments that the departments are same in both the emp
and dept. Solution: 1. Use select from clause.
2. Use equi join in select clause to get the result.
Ans: SQL> select * from emp,dept where [Link]=[Link];
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
---------- ------------------ ---------- ---------- ---------- ---------- -------------- -------------
1 Mathi AP 1 10000 1 ACCOUNTING NEW YORK
2 Arjun ASP 2 12000 2 RESEARCH DALLAS
3 Gugan ASP 2 20000 2 RESEARCH DALLAS
4 Karthik AP 1 15000 1
ACCOUNTING NEW YORK
NON-EQUIJOIN
IT-405 IT/GGITS 29 | 1 2 5
/Database Management System Lab /SRM
Q5: Display the employee details, departments that the departments are not same in both the
emp and dept. Solution: [Link] select from clause. 2. Use non equi join in select clause to get
the result.
Ans: SQL> select * from emp,dept where [Link]!=[Link];
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
---------- -------------------- ---------- ---------- ---------- ------------------------ -------------
2 Arjun ASP 2 12000 1 ACCOUNTING NEW YORK
3 Gugan ASP 2 20000 1 ACCOUNTING NEW YORK
1 Mathi AP 1 10000 2 RESEARCH DALLAS
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
---------- -------------------- ---------- ---------- ---------- ---------- -------------- -------------
4 Karthik AP 1 15000 2 RESEARCH DALLAS
1 Mathi AP 1 10000 30 SALES CHICAGO
2 Arjun ASP 2 12000 30 SALES CHICAGO
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
---------- -------------------- ---------- ---------- ---------- ---------- -------------- -------------
3 Gugan ASP 2 20000 30 SALES CHICAGO
4 Karthik AP 1 15000 30 SALES CHICAGO
1 Mathi AP 1 10000 40 OPERATIONS BOSTON
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
---------- -------------------- ---------- ---------- ---------- ---------- -------------- -------------
2 Arjun ASP 2 12000 40 OPERATIONS BOSTON
3 Gugan ASP 2 20000 40 OPERATIONS BOSTON
4 Karthik AP 1 15000 40 OPERATIONS BOSTON
12 rows selected.
LEFTOUT-JOIN Tables used SQL> select * from stud1;
Regno Name Mark2 Mark3 Result
---------- ----------- ---------- ---------- ---------------------------------------
101 john 89 80 pass
102 Raja 70 80 pass
103 Sharin 70 90 pass
104 sam 90 95 pass
SQL> select * from stud2;
NAME GRA
----------- ----------
john s raj s sam a sharin a
Q6: Display the Student name and grade by implementing a left outer join.
Ans: SQL> select [Link],grade from stud1 left outer join stud2 on
[Link]=[Link]; Name Gra
----------- ----------
john s raj
s sam a sharin a smith null RIGHTOUTER-JOIN
IT-405 IT/GGITS 30 | 1 2 5
/Database Management System Lab /SRM
Q7: Display the Student name, register no, and result by implementing a right outer join.
Ans: SQL> select [Link], regno, result from stud1 right outer join stud2 on [Link]
= [Link]; Name Regno Result
----------- ---------- --------------------------
john 101 pass
raj 102 pass
sam 103 pass
sharin 104 pass
Rollno Name Mark1 Mark2 Total
---------- ---------- ---------- ---------- ----------
1 sindu 90 95 185
2 arul 90 90 180
FULLOUTER-JOIN
Q8: Display the Student name register no by implementing a full outer join.
Ans: SQL> select [Link], regno from stud1 full outer join stud2 on ([Link]=
[Link]); Name Regno
----------- ----------
john 101
raj 102 sam
103 sharin 104
SELFJOIN
Q9: Write a query to display their employee names
Ans: SQL> select distinct ename from emp x, dept y where [Link]=[Link];
ENAME
--------------------
Arjun
Gugan
Karthik
Mathi
Q10: Display the details of those who draw the salary greater than the average salary.
Ans: SQL> select distinct * from emp x where [Link] >= (select avg(sal) from emp);
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ---------- ---------- ----------
3 Gugan ASP 2 20000
4 Karthik AP 1 15000
11 kavitha designer 12 17000
IT-405 IT/GGITS 31 | 1 2 5
/Database Management System Lab /SRM
Aim: To perform DCL Commands
DCL COMMANDS
The DCL language is used for controlling the access to the table and hence securing the
database. DCL is used to provide certain privileges to a particular user. Privileges are rights
to be allocated. The privilege commands are namely, Grant and Revoke. The various
privileges that can be granted or revoked are, Select Insert Delete Update References Execute
All.
GRANT COMMAND: It is used to create users and grant access to the database. It requires
database administrator (DBA) privilege, except that a user can change their password. A user
can grant access to their database objects to other users.
REVOKE COMMAND: Using this command , the DBA can revoke the granted database
privileges from the user.
TCL COMMAND
COMMIT: command is used to save the Records.
ROLL BACK: command is used to undo the Records.
SAVE POINT command is used to undo the Records in a particular transaction.
Queries:
Tables Used: Consider the following tables namely “DEPARTMENTS” and
“EMPLOYEES”
Their schemas are as follows , Departments ( dept _no , dept_ name , dept_location );
Employees ( emp_id , emp_name , emp_salary );
Q1: Develop a query to grant all privileges of employees table into departments table
Ans: SQL> Grant all on employees to departments;
Grant succeeded.
Q2: Develop a query to grant some privileges of employees table into departments table
Ans: SQL> Grant select, update , insert on departments to departments with grant option;
Grant succeeded.
Q3: Develop a query to revoke all privileges of employees table from departments table
Ans: SQL> Revoke all on employees from departments; Revoke succeeded.
Q4: Develop a query to revoke some privileges of employees table from departments table
Ans: SQL> Revoke select, update , insert on departments from departments;
Revoke succeeded.
IT-405 IT/GGITS 32 | 1 2 5
/Database Management System Lab /SRM
Q5: Write a query to implement the save point
Ans: SQL> SAVEPOINT S1;
Savepoint created.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
SQL> INSERT INTO EMP VALUES(5,'Akalya','AP',1,10000); 1 row created.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
Q6: Write a query to implement the rollback
Ans: SQL> rollback s1; SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
IT-405 IT/GGITS 33 | 1 2 5
/Database Management System Lab /SRM
Q6: Write a query to implement the commit
Ans: SQL> COMMIT;
Commit complete.
RESULT
Thus the DML, DCL,TCL commands was performed successfully and executed.
IT-405 IT/GGITS 34 | 1 2 5
/Database Management System Lab /SRM
[Link] IMPLEMENTATION OF CURSORS
CURSOR PROGRAM FOR ELECTRICITY BILL CALCULATION:
SQL> create table bill(name varchar2(10), address varchar2(20), city varchar2(20), unit
number(10));
Table created.
SQL> insert into bill values('&name','&addess','&city','&unit');
Enter value for name: yuva
Enter value for addess: srivi
Enter value for city: srivilliputur
Enter value for unit: 100
old 1: insert into bill values('&name','&addess','&city','&unit')
new 1: insert into bill values('yuva','srivi','srivilliputur','100')
1 row created.
SQL> /
Enter value for name: nithya
Enter value for addess: Lakshmi nagar
Enter value for city: sivakasi
Enter value for unit: 200
old 1: insert into bill values('&name','&addess','&city','&unit')
new 1: insert into bill values('nithya','Lakshmi nagar','sivakasi','200')
1 row created.
SQL> /
IT-405 IT/GGITS 35 | 1 2 5
/Database Management System Lab /SRM
Enter value for name: maya
Enter value for addess: housing board
Enter value for city: sivakasi
Enter value for unit: 300
old 1: insert into bill values('&name','&addess','&city','&unit')
new 1: insert into bill values('maya','housing board','sivakasi','300')
1 row created.
SQL> /
Enter value for name: jeeva
Enter value for addess: RRR nagar
Enter value for city: sivaganagai
Enter value for unit: 400
old 1: insert into bill values('&name','&addess','&city','&unit')
new 1: insert into bill values('jeeva','RRR nagar','sivaganagai','400')
1 row created.
SQL> select * from bill;
NAME ADDRESS CITY UNIT
---------- -------------------- -------------------- ---------
yuva srivi srivilliputur 100
nithya Lakshmi nagar sivakasi 200
maya housing board sivakasi 300
jeeva RRR nagar sivaganagai 400
IT-405 IT/GGITS 36 | 1 2 5
/Database Management System Lab /SRM
SQL> declare
2 cursor c is select * from bill;
3 b bill %ROWTYPE;
4 begin
5 open c;
6 dbms_output.put_line('Name Address city Unit Amount');
7 loop
8 fetch c into b;
9 if(c % notfound) then
10 exit;
11 else
12 if([Link]<=100) then
13 dbms_output.put_line([Link]||' '||[Link]||' '||[Link]||' '||[Link]||' '||[Link]
t*1);
14 elsif([Link]>100 and [Link]<=200) then
15 dbms_output.put_line([Link]||' '||[Link]||' '||[Link]||' '||[Link]||' '||b.
unit*2);
16 elsif([Link]>200 and [Link]<=300) then
17 dbms_output.put_line([Link]||' '||[Link]||' '||[Link]||' '||[Link]||' '||b.
unit*3);
18 elsif([Link]>300 and [Link]<=400) then
19 dbms_output.put_line([Link]||' '||[Link]||' '||[Link]||' '||[Link]||' '||[Link]*
4);
20 else
21 dbms_output.put_line([Link]||' '||[Link]||' '||[Link]||' '||[Link]||' '||[Link]*
5);
22 end if;
IT-405 IT/GGITS 37 | 1 2 5
/Database Management System Lab /SRM
23 end if;
24 end loop;
25 close c;
26 end;
27 /
Name Address city Unit Amount
yuva srivi srivilliputur 100 100
nithya Lakshmi nagar sivakasi 200 400
maya housing board sivakasi 300 900
jeeva RRR nagar sivaganagai 400 1600
PL/SQL procedure successfully completed.
IT-405 IT/GGITS 38 | 1 2 5
/Database Management System Lab /SRM
PROGRAM FOR STUDENT GRADE CALCULATION
SQL> create table std(name varchar(10), rollno number(3),mark1 number(3), mark2
number(3), mark3 number(3));
Table created.
SQL> insert into std values('&name','&rollno','&mark1','&mark2','&mark3');
Enter value for name: gowri
Enter value for rollno: 101
Enter value for mark1: 78
Enter value for mark2: 89
Enter value for mark3: 99
old 1: insert into std values('&name','&rollno','&mark1','&mark2','&mark3')
new 1: insert into std values('gowri','101','78','89','99')
1 row created.
SQL> /
Enter value for name: prem
Enter value for rollno: 102
Enter value for mark1: 88
Enter value for mark2: 99
Enter value for mark3: 90
old 1: insert into std values('&name','&rollno','&mark1','&mark2','&mark3')
new 1: insert into std values('prem','102','88','99','90')
IT-405 IT/GGITS 39 | 1 2 5
/Database Management System Lab /SRM
1 row created.
SQL> /
Enter value for name: ravathi
Enter value for rollno: 103
Enter value for mark1: 67
Enter value for mark2: 89
Enter value for mark3: 99
old 1: insert into std values('&name','&rollno','&mark1','&mark2','&mark3')
new 1: insert into std values('ravathi','103','67','89','99')
1 row created.
SQL> /
Enter value for name: arun
Enter value for rollno: 104
Enter value for mark1: 56
Enter value for mark2: 66
Enter value for mark3: 77
old 1: insert into std values('&name','&rollno','&mark1','&mark2','&mark3')
new 1: insert into std values('arun','104','56','66','77')
1 row created.
SQL> set serveroutput on;
IT-405 IT/GGITS 40 | 1 2 5
/Database Management System Lab /SRM
SQL> declare
2 tot number;
3 average number;
4 cursor c is select * from std;
5 s std %ROWTYPE;
6 begin
7 open c;
8 dbms_output.put_line('Name Rollno Mark1 Mark2 Mark3 Total Average
Grade');
9 loop
10 fetch c into s;
11 tot:=s.mark1+s.mark2+s.mark3;
12 average:=floor(tot/3);
13 if(c % notfound)then
14 exit;
15 else
16 if(s.mark1<50 or s.mark2<50 or s.mark3<50)then
17 dbms_output.put_line([Link]||' '||[Link]||' '||s.mark1||' '||s.mark2||' '||s.mark3||
' '||tot||' '||average||' '||'F');
18 elsif(average>=90 and average<=100)then
19 dbms_output.put_line([Link]||' '||[Link]||' '||s.mark1||' '||s.mark2||' '||s.mark3||
' '||tot||' '||average||' '||'S');
20 elsif(average>=80 and average<90)then
21 dbms_output.put_line([Link]||' '||[Link]||' '||s.mark1||' '||s.mark2||' '||s.mark3||
' '||tot||' '||average||' '||'A+');
22 elsif(average>=70 and average<80)then
23 dbms_output.put_line([Link]||' '||[Link]||' '||s.mark1||' '||s.mark2||' '||s.mark3||
IT-405 IT/GGITS 41 | 1 2 5
/Database Management System Lab /SRM
' '||tot||' '||average||' '||'B');
24 elsif(average>=60 and average<70)then
25 dbms_output.put_line([Link]||' '||[Link]||' '||s.mark1||' '||s.mark2||' '||s.mark3||
' '||tot||' '||average||' '||'C');
26 else
27 dbms_output.put_line([Link]||' '||[Link]||' '||s.mark1||' '||s.mark2||' '||s.mark3||
' '||tot||' '||average||' '||'D');
28 end if;
29 end if;
30 end loop;
31 close c;
32 end;
33 /
Name Rollno Mark1 Mark2 Mark3 Total Average Grade
gowri 101 78 89 99 266 88 A+
prem 102 88 99 90 277 92 S
ravathi 103 67 89 99 255 85 A+
arun 104 56 66 77 199 66 C
PL/SQL procedure successfully completed.
RESULT:
Thus the program to implement cursors was executed and output was verified successfully.
IT-405 IT/GGITS 42 | 1 2 5
/Database Management System Lab /SRM
[Link] IMPLEMENTATION OF TRIGGERS
TRIGGER FOR DISPLAYING GRADE OF THE STUDENT
SQL> create table stdn(rollno number(3),name varchar(2),m1 number(3),m2 number(3),m3
number(3),tot num
ber(3),avrg number(3),result varchar(10));
Table created.
SQL> create or replace trigger t1 before insert on stdn
2 for each row
3 begin
4 :[Link]:=:new.m1+:new.m2+:new.m3;
5 :[Link]:=:[Link]/3;
6 if(:new.m1>=50 and :new.m2>=50 and :new.m3>=50) then
7 :[Link]:='pass';
8 else
9 :[Link]:='Fail';
10 end if;
11 end;
12 /
Trigger created.
SQL> insert into stdn values(101,'SM',67,89,99,'','','');
1 row created.
SQL> select * from stdn;
ROLLNO NA M1 M2 M3 TOT AVRG RESULT
--------- -- --------- --------- --------- --------- --------- ----------
101 SM 67 89 99 255 85 pass
IT-405 IT/GGITS 43 | 1 2 5
/Database Management System Lab /SRM
PROGRAM TO INDICATE INVALID CONDITION USING TRIGGER
SQL> create table emp (name varchar(10),empno number(3),age number(3));
Table created.
SQL>
1 create or replace trigger t2 before insert on emp
2 for each row
3 when([Link]>100)
4 begin
5 RAISE_APPLICATION_ERROR(-20998,'INVALID ERROR');
6* end;
SQL> /
Trigger created.
SQL> insert into emp values('nithya',101,24);
1 row created.
SQL> insert into emp values('nithya',101,103);
insert into emp values('nithya',101,103)
ERROR at line 1:
ORA-20998: INVALID ERROR
ORA-06512: at "SCOTT.T2", line 2
ORA-04088: error during execution of trigger 'SCOTT.T2'
RESULT:
Thus triggers were implemented successfully.
IT-405 IT/GGITS 44 | 1 2 5
/Database Management System Lab /SRM
EXNO:5 PROCEDURES AND FUNCTIONS
PROCEDURE TO INSERT NUMBER
SQL> create table emp1(id number(3),First_name varchar2(20));
Table created.
SQL> insert into emp1 values(101,'Nithya');
1 row created.
SQL> insert into emp1 values(102,'Maya');
1 row created.
SQL> select * from emp1;
ID FIRST_NAME
--------- --------------------
101 Nithya
102 Maya
SQL> set serveroutput on;
SQL> create or replace
2 procedure insert_num(p_num number)is
IT-405 IT/GGITS 45 | 1 2 5
/Database Management System Lab /SRM
3 begin
4 insert into emp1(id,First_name) values(p_num,user);
5 end insert_num;
6/
Procedure created.
SQL> exec insert_num(3);
PL/SQL procedure successfully completed.
SQL> select * from emp1;
ID FIRST_NAME
--------- --------------------
101 Nithya
102 Maya
103 SCOTT
IT-405 IT/GGITS 46 | 1 2 5
/Database Management System Lab /SRM
FUNCTION TO FIND FACTORIAL
SQL> create or replace function fact(n number)
2 return number is
3 i number(10);
4 f number:=1;
5 begin
6 for i in 1..N loop
7 f:=f*i;
8 end loop;
9 return f;
10 end;
11 /
Function created.
SQL> select fact(2) from dual;
FACT(2)
---------
RESULT:
Thus procedures and functions were implemented successfully.
IT-405 IT/GGITS 47 | 1 2 5
/Database Management System Lab /SRM
[Link] IMPLEMENTATION OF EMBEDDED SQL
PL/SQL PROGRAM FOR BONUS CALCULATION
SQL> set serveroutput on;
SQL> declare
2 salary number;
3 bonus number;
4 begin
5 salary:=&sa;
6 if salary>5000 then
7 bonus:=salary*0.5;
8 else
9 bonus:=0;
10 end if;
11 dbms_output.put_line(bonus);
12 end;
13 /
Enter value for sa: 10000
old 5: salary:=&sa;
new 5: salary:=10000;
5000
PL/SQL procedure successfully completed.
IT-405 IT/GGITS 48 | 1 2 5
/Database Management System Lab /SRM
PROGRAM FOR ARMSTRONG NUMBER
SQL> set serveroutput on;
SQL> declare
2 a number;
3 b number;
4 i number;
5 begin
6 i:=#
7 a:=i;
8 b:=0;
9 while a>0
10 loop
11 b:=b+power(mod(a,10),3);
12 a:=trunc(a/10);
13 end loop;
14 if b=i then
15 dbms_output.put_line(i||'IS AN ARMSTRONG NUMBER');
16 else
17 dbms_output.put_line(i||'IS NOT AN ARMSTRONG NUMBER');
18 end if;
19 end;
20 /
Enter value for num: 123
old 6: i:=#
new 6: i:=123;
IT-405 IT/GGITS 49 | 1 2 5
/Database Management System Lab /SRM
123 IS NOT AN ARMSTRONG NUMBER
PL/SQL procedure successfully completed.
SQL> /
Enter value for num: 407
old 6: i:=#
new 6: i:=407;
407IS AN ARMSTRONG NUMBER
PL/SQL procedure successfully completed.
IT-405 IT/GGITS 50 | 1 2 5
/Database Management System Lab /SRM
PROGRAM FOR MULTIPLICATION TABLE:
SQL> set serveroutput on;
SQL> declare
2 a number;
3 b number;
4 i number;
5 n number;
6 s number;
7 begin
8 a:=&ulimit;
9 b:=&llimit;
10 n:=&n;
11 for i in a..b loop
12 s:=i*n;
13 dbms_output.put_line(i||'*'||n||'='||s);
14 end loop;
15 end;
16 /
Enter value for ulimit: 1
old 8: a:=&ulimit;
new 8: a:=1;
Enter value for llimit: 10
old 9: b:=&llimit;
new 9: b:=10;
Enter value for n: 5
IT-405 IT/GGITS 51 | 1 2 5
/Database Management System Lab /SRM
old 10: n:=&n;
new 10: n:=5;
1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50
PL/SQL procedure successfully completed.
RESULT:
Thus Embedded SQL was executed successfully.
IT-405 IT/GGITS 52 | 1 2 5
/Database Management System Lab /SRM
EX NO:7 Database design using E-R model and Normalization
ER diagram:
Chen Notation
ORDER (OrderNum (key), OrderDate, SalesPerson)
ORDERITEMS (OrderNum (key)(fk) , ItemNum (key), PartNum, Quantity, Cost)
In the above example, in the ORDERITEMS Relation: OrderNum is the Foreign
Key and OrderNum plus ItemNum is the Composite Key.
Chen Notation
In the ORDER Relation: OrderNum is the Key.
Representing Relationships
1:1 Relationships. The key of one relation is stored in the second relation.
Look at example queries to determine which key is queried most often.
1:N Relationships.
Parent - Relation on the "1" side.
Child - Relation on the "Many" side.
Represent each Entity as a relation.
Copy the key of the parent into the child relation.
CUSTOMER (CustomerID (key), Name, Address, ...)
ORDER (OrderNum (key), OrderDate, SalesPerson, CustomerID (fk))
IT-405 IT/GGITS 53 | 1 2 5
/Database Management System Lab /SRM
M:N Relationships. Many to Many relationships can not be directly implemented in
relations.
Solution: Introduce a third Intersection relation and copy keys from original two
relations.
Chen Notation
SUPPLIER (SupplierID (key), FirmName, Address, ...)
COMPONENT (CompID (key), Description, ...)
SUPPLIER_COMPONENT (SupplierID (key), CompID (key))
Note that this can also be shown in the ER diagram. Also, look for potential added
attributes in the intersection relation.
RESULT:
Thus the ER Database design using E-R model and Normalization was implemented
successfully.
IT-405 IT/GGITS 54 | 1 2 5
/Database Management System Lab /SRM