DBMS 1 1
DBMS 1 1
DBMS 1 1
LAB MANUAL
1902BS451 - DATABASE MANAGEMENT SYSTEMS
2022-2023 – Even Semester
Course Coordinator
Mr.G. Murugan
Assistant Professor CSBS
E.G.S.Pillay Engineering College
Nagapattinam
hodcsbs@egspec.org | www.egspec.org
1
SYLLABUS DATABASE MANAGEMENT SYSTEMS LAB L T P C
0 0 2 1
PREREQUISITES::
Computer Programming Languages
COURSE OBJECTIVES:
1. Learn to create and use a database
2. Be familiarized with a query language
3. Have hands on experience on DDL Commands
4. Have a good understanding of DML Commands and DCL commands
5. Familiarize advanced SQL queries.
LIST OF EXPERIMENTS:
1 Various Data definition language commands
2 Data manipulation language commands and aggregate functions
3 Set Operations, Joins and Nested Queries using commands
4 Constraints and Views
5 High level programming language extensions Control structures, procedures and functions
6 High level programming language extensions cursors and triggers
7 Normal forms
TOTAL: 35 HOURS
REQUIREMENTS:
Hardware:
Standalone desktops 30 Nos. (or) Server supporting 30 terminals or more.
Software:
Oracle / MySQL/ Sql Server DB2 or Equivalent.
FURTHER READING / CONTENT BEYOND SYLLABUS / SEMINAR :
Under MoU with Oracle Academy, In this programme extensive hands-on training on SQL and PL/SQL will be
given to students during the Lab sessions.
Writing SQL queries for Hierarchical retrieval of data (tree structured data)
Using stored procedures and Functions for implementing object level data security
Application Development using front end tools
Study of Cloud Storage
COURSE OUTCOMES:
After completion of the course, Student will be able to
CO1 Able to understand various queries and their execution
CO4 Programming pl/sql including stored procedures, stored functions, cursors, triggers
CO5 Apply the normalization techniques for development of application software to realistic problems
REFERENCES:
1.http://ilearning.oracle.com
2. http://coursera.org/
3. http://nptel.ac.in/
2
REG NO:8208E22BSR050
NAME:SRIMATHI.J
3
~ DROP:-
SQL> alter table stud drop ( sdept varchar2(20));
Table altered.
SQL> desc studs;
Name Null? Type
------------------------------------------ ---------- ---------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
4.TRUNCATE THE TABLE:-
SQL> truncate table studs;
Table truncated.
SQL> desc studs;
Name Null? Type
---------------------------------------- ------------ ------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SDEPT VARCHAR2(20)
5.DROP THE TABLE:-
SQL> drop table studs;
Table dropped.
RESULT:-
Thus the DDL commands have been implemented and the output is verified.
4
REG NO:8208E22BSR050
NAME:SRIMATHI.J
AIM:-
To study the various categories of DML commands such as logical operations, aggregate
functions, string functions , numeric functions, date functions and conversion functions.
INSERT:-
This command is used to insert values into the table.
SELECT:-
This command is used to display the contents of the table or those of a particular column.
RENAME:-
This command renames the name of the table.
1.OPERATORS:-
ARITHMETIC OPERATOR:-
Various operations such as addition, multiplication, subtraction and division can be
performed using the numbers available in the table.
DISTINCT:-
This keyword is used along with select keyword to display unique values from the
specified column. It avoids duplicates during display.
CONCATENATION OPERATOR:-
This combines information from two or more columns in a sentence according to the format
specified.
LOGICAL OPERATORS:-
~AND : -
The oracle engine will process all rows in a table and displays the result only when all of the
conditions specified using the AND operator are specified.
~OR :-
The oracle engine will process all rows in a table and displays the result only when any of the
conditions specified using the OR operators are satisfied.
~NOT :-
The oracle engine will process all rows in a table and displays the result only when none of the
conditions specified using the NOT operator are specified.
~BETWEEN :-
In order to select data that is within a range of values, the between operator is used. (AND
should be included).
5
PATTERN MATCH LIKE PREDICATE : -
The use of like predicate is that it allows the comparison of one string value with another string
value, which is not identical. This is achieved by using wildcard characters which are % and _.
The purpose of % is that it matches any string and _ matches any single character.
2.NUMERIC FUNCTIONS:-
~ABS:-
It returns the absolute value of ‘n’.
~POWER:-
It returns m raised to nth power. n must be an integer else an error is returned.
~ROUND: -
It returns n rounded to m places right of the decimal point. If m is omitted, n is rounded to zero
places. m must be an integer.
~SQRT:-
It returns square root of n. n should be greater than zero.
3.STRING FUNCTIONS:-
~LOWER:-
It returns char with letters in lower case.
~INITCAP:-
It returns char with the first letter in upper case.
~UPPER: -
It returns char with all letters forced to upper case.
~SUBSTR:-
It returns a portion of char beginning at character m, exceeding up to n characters. If n is
omitted result is written up to the end character. The 1 st position of char is one.
~LENGTH: -
It returns the length of char
~LTRIM:-
It removes characters from the left of char with initial characters removed up to the 1 st
character not in set.
~RTRIM:-
It returns char with final characters removed after the last character not in the set. Set is optional.
It defaults to spaces.
~LPAD:-
It returns char1, left padded to length n with the sequence of characters in char2. char2
defaults to blanks.
6
~RPAD:-
It returns char1, right padded to length n with the characters in char2, replicated as many
times as necessary. If char2 is omitted, it is padded with blanks.
4.AGGREGATE FUNCTIONS:-
~AVG (N):-
It returns average value of n ignoring null values.
~MIN (EXPR):-
It returns minimum value of the expression.
~COUNT (EXPR):-
It returns the number of rows where expression is not null.
~COUNT (*):-
It returns the number of rows in the table including the duplicates and those with null values.
~MAX (EXPR):-
It returns maximum value of the expression.
~SUM(N):-
It returns sum of values of n.
5.DATE FUNCTIONS:-
~SYSDATE:-
The sysdate is a pseudo column that contains the current date and time. It requires no arguments
when selected from the table dual and returns the current date.
~ADD_MONTHS(D,N):-
It returns date after adding the number of months specified with the function.
~LAST_DAY(D):-
It returns the last date of the month specified with the function
~MONTHS_BETWEEN(D1,D2):-
It returns number of months between D1 and D2.
~NEXT_DAY(DATE, CHAR):-
It returns the date of the first week day named by char . char must be a day of the week.
COMMANDS
1.CREATION OF TABLE:-
SQL>create table stud (sname varchar2(30), sid varchar2(10), sage number(10), sarea
varchar2(20), sdept varchar2(20));
Table created.
7
SQL> insert into stud values ('pruthvik',103,20,'anna nagar','aerospace');
1 row created.
SQL> insert into stud values ('charith',104,20,'kilpauk','mechanical');
1 row created.
ARITHMETIC OPERATION:-
SNAME stid
------------------------------ ---------
ashwin 201
bhavesh 202
pruthvik 203
charith 204
CONCATENATION OPERATOR:-
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
-------------------------------------------------------------------
ashwin is a aeronautical engineer.
bhavesh is a marine engineer.
pruthvik is a aerospace engineer.
charith is a mechanical engineer.
8
USING THE WHERE CLAUSE:-
SNAME SAGE
------------------------------- ----------
ashwin 19
bhavesh 18
BETWEEN OPERATOR:-
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
IN PREDICATE:-
PATTERN MATCHING:-
SQL> select sname, sarea from studs where sarea like '%g%';
SNAME SAREA
--------------------------- -----------------------
ashwin anna nagar
bhavesh nungambakkam
pruthvik anna nagar
SQL> select sname ,sid from studs where sid>102 and sarea='anna nagar';
SNAME SID
-------------------------- --------
pruthvik 103
9
LOGICAL OR OPERATOR:-
SQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
charith 104
NOT IN PREDICATE:-
SQL> select sname, sid from studs where sid not in(102,104);
SNAME SID
------------------------ -----------
ashwin 101
pruthvik 103
AGGREGATE FUNCTIONS :-
~AVG:-
SQL> select avg( spocket ) result from studs;
RESULT
-----------
400
10
~MIN:-
~COUNT:-
~MAX:-
~SUM:-
NUMERIC FUNCTIONS:-
~ABS:-
11
----------- 1024
~ROUND:-
STRING FUNCTIONS:-
~LOWER:-
~UPPER:-
~INTICAP:-
~SUBSTR:-
RESULT
-----
racle
~IPAD:-
12
~RPAD:-
RESULT
----------
oracle^^^^
DATE FUNCTIONS:-
ORDER BY CLAUSE:-
13
GROUP BY CLAUSE:-
HAVING CLAUSE:-
SQL> select sarea, sum(spocket) result from studs group by sarea having spocket<600;
SAREA RESULT
-------------------- ------------
nungambakkam 500
kilpauk 100
DELETION:-
RESULT:-
Thus the DML commands have been implemented and the output is verified.
14
REG NO:8208E22BSR050
NAME:SRIMATHI.J
AIM:-
To study the Set Operations, Joins and Nested Queries using DML commands.
COMMANDS:-
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
15
TO CREATE SALE TABLE:-
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SET OPERATIONS:-
SQL> select prodname from product where prodno=10010 union select prodname from sale
where prodno=10010;
PRODNAME
------------------------------
chair
sofa
SQL> select prodname from product where prodno=11110 intersect select prodname from
sale where prodno=11110;
16
PRODNAME
------------------------------
cot
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> create table sstud2 ( sname varchar2(20), dept varchar2(10), marks number(10));
Table created.
1 row created.
SQL> insert into sstud2 values ('anand','it',650);
17
1 row created.
1 row created.
1 row created.
JOIN OPERATIONS:-
SNAME DEPT
------------- --------------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
------------- --------------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 left outer join sstud2 on ( sstud1.sname=
sstud2.sname);
SNAME DEPT
------------- ---------------
prajan cse
anand it
ravi it
SQL> select sstud1.sname, dept from sstud1 right outer join sstud2 on ( sstud1.sname=
sstud2.sname);
18
SNAME DEPT
--------------- -------------
prajan cse
anand it
ravi it
SQL> select sstud1.sname, dept from sstud1 full outer join sstud2 on ( sstud1.sname=
sstud2.sname);
SNAME DEPT
----------------- -----------
prajan cse
anand it
ravi it
kumar cse
NESTED QUERIES:-
SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from
2 sstud2 );
SNAME
--------------------
anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from
sstud2 );
SNAME
--------------------
kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2 where
dept='cse');
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2 where
dept='cse' );
SNAME
--------------------
prajan
vasu
19
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where
dept='cse' );
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2 where
dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where
dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where
dept='cse' );
SNAME
--------------------
anand
ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
--------------------
prajan
anand
ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from sstud2 where
sstud1.sname=sstud2.sname );
SNAME
--------------------
Kumar
20
SQL> select ename from e234 where exists(select * from e234);
ENAME
--------------------
x
y
z
a
c
SQL> select ename from e234 where not exists(select * from e234);
no rows selected
TO RENAME A COLUMN:-
NAME ID
-------------------- ----------
x 1
a 2
g 3
d 4
EMP_NAME ID
-------------------- ----------
x 1
a 2
g 3
d 4
JOIN OPERATIONS:-
21
OUTER JOIN:-
It is an extension of join operation to deal with missing information.
Table created.
22
SQL> insert into emp2 values(111,'saketh','analyst',444,10);
1 row created.
SQL> insert into emp2 values(222,'sandeep','clerk',333,20);
1 row created.
SQL> insert into emp2 values(333,'jagan','manager',111,10);
1 row created.
SQL> insert into emp2 values(444,'madhu','engineer',222,40);
1 row created.
1. EQUIJOIN:-
A join which contains an equal to ‘=’ operator in this joins condition
USING CLAUSE:-
ON CLAUSE:-
23
ENO ENAME JOB DNAME LOC
------ --------------- ------------ --------------- ------------
111 saketh analyst inventory hyd
222 sandeep clerk finance bglr
333 jagan manager inventory hyd
2. NON-EQUIJOIN:-
A join which contains an operator other than equal to ‘=’ in the join condition.
3.SELF JOIN -:
Joining the table itself is called self join.
4.NATURAL JOIN :-
It compares all the common columns.
5. CROSS JOIN:-
This will give the cross product.
SQL> select eno,ename,job,dname,loc from emp2 cross join dept;
24
444 madhu engineer inventory hyd
111 saketh analyst finance bglr
222 sandeep clerk finance bglr
333 jagan manager finance bglr
444 madhu engineer finance bglr
111 saketh analyst HR mumbai
222 sandeep clerk HR mumbai
333 jagan manager HR mumbai
444 madhu engineer HR mumbai
12 rows selected.
6. OUTER JOIN:
It gives the non matching records along with matching records.
SQL> select eno,ename,job,dname,loc from emp2 e left outer join dept d on(e.dno=d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno(+);
This will display the all matching records and the non matching records from both tables.
25
SQL> select eno,ename,job,dname,loc from emp2 e full outer join dept d on(e.dno=
d.dno);
RESULT:-
Thus the Set Operations, Joins and Nested Queries using DML commands have been
implemented and the output is verified.
26
REG NO:8208E22BSR050
NAME:SRIMATHI.J
AIM:
To create views, synonyms, sequences, indexes and save points using DDL, DML and DCL
statements.
DESCRIPTION:-
Views:-
A database view is a logical or virtual table based on a query. It is useful to think of a
view as a stored query. Views are queried just like tables.
A DBA or view owner can drop a view with the DROP VIEW command.
TYPES OF VIEWS:-
• Updatable views – Allow data manipulation
• Read only views – Do not allow data manipulation
Table created.
1 row created.
1 row created.
1 row created.
27
SQL> create table dviews( dno number(5), dname varchar2(20));
Table created.
1 row created.
1 row created.
DNO DNAME
--------- -------------
11 x
12 y
SQL> create view sview as select name,no,sal,dno from fviews where dno=11;
View created.
Updates made on the view are reflected only on the table when the structure of the table and the
view are not similar -- proof
1 row created.
28
NAME NO SAL DNO
----------- -------- ------------- ----------
Xxx 1 19000 11
aaa 2 19000 12
yyy 3 40000 13
zzz 4 20000 14
Updates made on the view are reflected on both the view and the table when the structure of the
table and the view are similar – proof
View created.
1 row created.
29
SQL> create view ssview( cusname,id) as select name, no from fviews where dno=12;
View created.
CUSNAME ID
-------------------- ---------
Aaa 2
View dropped.
TO CREATE A VIEW ‘COMBO’ USING BOTH THE TABLES ‘FVIEWS’ AND ‘DVIEWS’:-
View created.
*ERROR at line 1:
ORA-01779: cannot modify a column which maps to a non key-preserved table
This shows that when a view is created from two different tables no manipulations can be performed using
that view and the above error is displayed.
Integrity Constraints:-
1.Domain Constraint:-
a)NULL CONSTRAINT
b)NOT NULL CONSTRAINT
c)CHECK CONSTRAINT
30
2. Entity Constraint
a)UNIQUE
b)PRIMARY KEY
c)FOREIGN KEY
1.DOMAIN CONSTRAINT:-
a)NULL CONSTRAINT:-
Table created.
1 row created.
SQL> /
Enter value for cname: y
Enter value for accno: 2
old 1: insert into customer values('&cname',&accno)
new 1: insert into customer values('y',2)
1 row created.
1 row created.
31
SQL> select * from customer;
CNAME ACCNO
-------------------- ----------------
x 1
y 2
z
1 row updated.
CNAME ACCNO
-------------------- -----------------
x 1
y 2
z 3
Table created.
1 row created.
SQL> /
Enter value for cname: y
Enter value for loan_no: 2
old 1: insert into borrower values('&cname',&loan_no)
new 1: insert into borrower values('y',2)
1 row created.
32
SQL> /
Enter value for cname: z
Enter value for loan_no: null
old 1: insert into borrower values('&cname',&loan_no)
new 1: insert into borrower values('z',null)
insert into borrower values('z',null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."BORROWER"."LOAN_NO")
SQL> /
Enter value for cname: a
Enter value for loan_no:
old 1: insert into borrower values('&cname',&loan_no)
new 1: insert into borrower values('a',)
insert into borrower values('a',)
*
ERROR at line 1:
ORA-00936: missing expression
CNAME LOAN_NO
-------------------- ------------------
x 1
y 2
C) CHECK CONSTRAINT:-
Table created.
1 row created.
33
SQL> /
Enter value for cname: y
Enter value for balance: 200
old 1: insert into withdraw values('&cname',&balance)
new 1: insert into withdraw values('y',200)
insert into withdraw values('y',200)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CONSC) violated
SQL> /
Enter value for cname: y
Enter value for balance: 20000
old 1: insert into withdraw values('&cname',&balance)
new 1: insert into withdraw values('y',20000)
1 row created.
CNAME BALANCE
-------------------- ---------------
x 10000
y 20000
Table created.
1 row created.
34
SQL> /
Enter value for ename: y
Enter value for cname: tcs
Enter value for place: madurai
old 1: insert into company1 values('&ename','&cname','&place')
new 1: insert into company1 values('y','tcs','madurai')
insert into company1 values('y','tcs','madurai')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CONSP) violated
SQL> /
Enter value for ename: y
Enter value for cname: tcs
Enter value for place: madurai
old 1: insert into company1 values('&ename','&cname','&place')
new 1: insert into company1 values('y','tcs','madurai')
1 row created.
SQL> /
Enter value for ename: z
Enter value for cname: wipro
Enter value for place: adyar
old 1: insert into company1 values('&emane','&cname','&place')
new 1: insert into company1 values('z','wipro','adyar')
insert into company1 values('z','wipro','adyar')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CONSP) violated
35
2. ENTITY INTEGRITY CONSTRAINT:-
a)UNIQUE CONSTRAINT:-
Table created.
1 row created.
SQL> /
Enter value for comp_name: tcs
Enter value for location: adyar
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('tcs','adyar')
insert into company values('tcs','adyar')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C003014) violated
SQL> /
Enter value for comp_name: cts
Enter value for location: vadapalani
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('cts','vadapalani')
insert into company values('cts','vadapalani')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C003015) violated
36
SQL> /
Enter value for comp_name: null
Enter value for location: solinganalur
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('null','solinganalur')
1 row created.
SQL> /
Enter value for comp_name:
Enter value for location: padi
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values(','padi')
insert into company values(,'padi')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."COMPANY"."COMP_NAME")
SQL> /
Enter value for comp_name: wipro
Enter value for location:
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('wipro','')
insert into company values('wipro','')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."COMPANY"."LOCATION")
COMP_NAME LOCATION
--------------------- ------------------
tcs vadapalani
null solinganalur
Table created.
37
SQL> /
Enter value for accno: 2
old 1: insert into account values(&accno)
new 1: insert into account values(2)
1 row created.
SQL> /
Enter value for accno: 3
old 1: insert into account values(&accno)
new 1: insert into account values(3)
1 row created.
SQL> /
Enter value for accno: 2
old 1: insert into account values(&accno)
new 1: insert into account values(2)
insert into account values(2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CONSU) violated
Table altered.
Table altered.
38
SQL> select * from account;
ACCNO CNAME
----------- ------------
1
2
3
SQL> update account set cname='y' where accno=1;
1 row updated.
1 row updated.
1 row updated.
ACCNO CNAME
----------- ------------
1 y
2 z
3 a
SQL> create table depositor(cname varchar2(20),accno number,constraint consuni
unique(cname,accno));
Table created.
1 row created.
SQL> /
Enter value for cname: x
Enter value for accno: 2
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('x',2)
1 row created.
39
SQL> /
Enter value for cname: y
Enter value for accno: 2
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('y',2)
1 row created.
SQL> /
Enter value for cname: y
Enter value for accno: 2
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('y',2)
insert into depositor values('y',2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CONSUNI) violated
CNAME ACCNO
------------------ -------------
x 1
x 2
y 2
SQL> create table empv(name varchar2(20),id number not null constraint cons_pri
primary key);
Table created.
1 row created.
40
SQL> /
Enter value for name: henry
Enter value for id: 11
old 1: insert into empv values('&name',&id)
new 1: insert into empv values('henry',11)
insert into empv values('henry',11)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CONS_PRI) violated
SQL> /
Enter value for name: henry
Enter value for id: 12
old 1: insert into empv values('&name',&id)
new 1: insert into empv values('henry',12)
1 row created.
NAME ID
-------------------- ----------
john 11
henry 12
Table created.
1 row created.
41
SQL> /
Enter value for pname: banking_system
Enter value for id: 12
old 1: insert into project values('&pname',&id)
new 1: insert into project values('banking_system',12)
1 row created.
SQL> /
Enter value for pname: airline
Enter value for id: 11
old 1: insert into project values('&pname',&id)
new 1: insert into project values('airline',11)
1 row created.
SQL> /
Enter value for pname: payroll
Enter value for id: 12
old 1: insert into project values('&pname',&id)
new 1: insert into project values('payroll',12)
1 row created.
PNAME ID
-------------------------- --------------
banking_system 11
banking_system 12
airline 11
payroll 12
NAME ID PNAME
------------------------ -------- -----------------
john 11 banking_system
henry 12 banking_system
john 11 airline
henry 12 payroll
Table altered.
42
SQL> insert into empv values('&name',&id);
1 row created.
NAME ID
----------------------- ---------
john 11
henry 12
korth 12
1 row updated.
NAME ID
-------------------- ----------
john 11
henry 12
korth 13
43
SQL> insert into empv values('&name',&id);
Table created.
Table created.
Table altered.
Table altered.
Table altered.
SQL> alter table emp1 add constraint conf_dno foreign key(dno) references dept1(dno);
Table altered.
44
Name Null? Type
---------------------------- ------------- -------------------------------------
DNO NOT NULL NUMBER
DNAME VARCHAR2(15)
1 row created.
SQL> /
Enter value for dno: 2
Enter value for dname: cse
old 1: insert into dept1 values(&dno,'&dname')
new 1: insert into dept1 values(2,'cse')
1 row created.
SQL> /
Enter value for dno: 3
Enter value for dname: ece
old 1: insert into dept1 values(&dno,'&dname')
new 1: insert into dept1 values(3,'ece')
1 row created.
45
old 1: insert into dept1 values(&dno,'&dname')
new 1: insert into dept1 values(1,'eee')
insert into dept1 values(1,'eee')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CON_DNO) violated
DNO DNAME
---------- ---------------
1 it
2 cse
3 ece
SQL> insert into emp1 values('&ename',&eid,&dno);
1 row created.
SQL> /
Enter value for ename: y
Enter value for eid: 2
Enter value for dno: 1
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('y',2,1)
1 row created.
SQL> /
Enter value for ename: a
Enter value for eid: 3
Enter value for dno: 1
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('a',3,1)
1 row created.
SQL> /
Enter value for ename: b
Enter value for eid: 4
Enter value for dno: 2
46
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('b',4,2)
1 row created.
SQL> /
Enter value for ename: c
Enter value for eid: 5
Enter value for dno: 2
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('c',5,2)
1 row created.
SQL> /
Enter value for ename: d
Enter value for eid: 6
Enter value for dno: 3
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('d',6,3)
1 row created.
SQL> /
Enter value for ename: e
Enter value for eid: 6
Enter value for dno: 3
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('e',6,3)
insert into emp1 values('e',6,3)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CON_EID) violated
SQL> /
Enter value for ename: e
Enter value for eid: 7
Enter value for dno: 3
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('e',7,3)
1 row created.
47
a 3 1
b 4 2
c 5 2
d 6 3
e 7 3
7 rows selected.
7 rows selected.
SQL> select ename,dname from emp1,dept1 where emp1.dno=dept1.dno;
ENAME DNAME
----------------- ------------------
x it
y it
a it
b cse
c cse
d ece
e ece
7 rows selected.
ENAME DNAME
--------------- ---------------
x it
y it
a it
48
SQL> select ename,dno,dname from emp1,dept1 where emp1.dno=dept1.dno;
7 rows selected.
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.CONF_DNO) violated - parent key not
found
SQL> /
Enter value for ename: f
Enter value for eid: 8
Enter value for dno: 2
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('f',8,2)
1 row created.
49
SQL> select * from emp1;
8 rows selected.
RESULT:-
Thus the Views and Constraints have been implemented and the output is verified.
50
REG NO:8208E22BSR050
NAME:SRIMATHI.J
AIM:-
PROCEDURE:
FUNCTIONS – SYNTAX:-
create or replace function <function name> (argument in datatype,……) return datatype {is,as}
variable declaration;
constant declaration;
begin
PL/SQL subprogram body;
exception
exception PL/SQL block;
end;
CREATE THE TABLE ‘ITTRAIN’ TO BE USED FOR FUNCTIONS:-
Table created.
51
SQL> set serveroutput on;
SQL> declare
total number;
begin
total:=aaa (1001);
dbms_output.put_line('Train fare is Rs. '||total);
end;
/
Train fare is Rs.550
PL/SQL procedure successfully completed.
RESULT:-
Thus the control structures, procedures and functions have been implemented and its
output is verified.
52
REG NO:
8208E22BSR050
AIM:-
COMMANDS:-
SQL> create table bill(name varchar2(10), address varchar2(20), city varchar2(20), unit
number(10));
Table created.
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>/
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.
53
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>declare
2 cursor c is select* from bill;
3 b bill %ROWTYPE;
4 begin
5 open c;
6 dbms_output.putline(‘Name Address City Unit Amount’);
7 loop
8 fetch c into b;
9 if(c %notfound )then
10 exit;
11 else
12 if(b.unit<=100)
13 dbms_output.put_line(b.name||' '||b.address||' '||b.city||' '||b.unit||' '||b.uni t*1);
14 elsif(b.unit>100 and b.unit<=200) then
15 dbms_output.put_line(b.name||' '||b.address||' '||b.city||' '||b.unit||' '||b. unit*2);
16 elsif(b.unit>200 and b.unit<=300) then
17 dbms_output.put_line(b.name||' '||b.address||' '||b.city||' '||b.unit||' '||b. unit*3);
18 elsif(b.unit>300 and b.unit<=400) then
19 dbms_output.put_line(b.name||' '||b.address||' '||b.city||' '||b.unit||' '||b.unit* 4);
20 else
21 dbms_output.put_line(b.name||' '||b.address||' '||b.city||' '||b.unit||' '||b.unit* 5);
22 end if;
23 end if;
24 end loop;
25 close c;
26 end;
27 /
54
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
TRIGGER:-
DEFINITION:-
TYPES OF TRIGGERS:-
:new
:old
These two variables retain the new and old values of the column updated in the database. The
values in these variables can be used in the database triggers for data manipulation
55
SYNTAX:-
create or replace trigger triggername [before/after] {DML statements}
on [tablename] [for each row/statement]
begin
-------------------------
-------------------------
-------------------------
exception
end;
The package “raise_application_error” is used to issue the user defined error messages
Syntax: raise_application_error(error number,‘error message‘);
The error number can lie between -20000 and -20999.
The error message should be a character string.
SQL> create table itempls (ename varchar2(10), eid number(5), salary number(10));
Table created.
1 row created.
1 row created.
1 row created.
56
TO CREATE A SIMPLE TRIGGER THAT DOES NOT ALLOW INSERT UPDATE AND DELETE
OPERATIONS ON THE TABLE:-
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;
5/
Trigger created.
DELETE OPERATION:-
UPDATE OPERATION:-
Trigger dropped.
57
TO CREATE A TRIGGER THAT RAISES AN USER DEFINED ERROR MESSAGE AND DOES NOT
ALLOW UPDATION AND INSERTION:-
SQL> create trigger ittriggs before insert or update of salary on itempls for each row
declare
2 triggsal itempls.salary%type;
3 begin
4 select salary into triggsal from itempls where eid=12;
5 if(:new.salary>triggsal or :new.salary<triggsal) then
6 raise_application_error(-20100,'Salary has not been changed');
7 end if;
8 end;
9 /
Trigger created.
INSERT OPERATION:-
UPDATE OPERATION:-
RESULT: -
Thus the program for cursor and trigger has been implemented using sql is executed and its
output is verified.
58
REG NO:8208E22BSR050
NAME:SRIMATHI.J
AIM:-
COMMANDS:-
Table created.
SQL> insert into itempls values(' Relational Databases ', ‘Computer Science’,’ Jeremy Brown’,
‘Database Systems’);
1 row created.
Normalize a table that is in the first normal form (1NF) into a set of tables in the third normal form
(3NF).
Database normalization is a very important concept in database design. Every database designer should
know the rules for differ ent nor mal for ms . In practice, the most important nor mal for m is the
third nor mal for m (3NF). If you want to be a database designer, you should know how to check if a
table is in 3NF. You should also know how to normalize a table that’s not in 3NF into one in 3NF.
Typically, you normalize a table from 1NF to 3NF in two steps: first you normalize it into 2NF, then you
normalize that into 3NF. In this article, we’ll show you an example of normalization from 1NF through
2NF into 3NF. We’ll also discuss problems with 1NF tables that are solved when you normalize them into
2NF.
Normalization to 2NF
We’ll work with a table named Courses1, which contains information about courses offered at a certain
university: the titles of the courses, the lecturers, the departments, and the recommended textbooks for
each course. The table is in the first normal form, since all column values are atomic. Here’s the table:
Courses1
course department lecturer textbook
59
course department lecturer textbook
There are several problems with various operations on data in this table:
INSERT: You can't add a course with no textbooks.
UPDATE: To change the lecturer for the course Relational Databases, you have to change two
rows.
DELETE: If you remove the Algorithms course, you also remove the lecturer James Cormen.
These problems come from the fact that this table is not in 2NF. Let’s see why. There are three non-
trivial functional dependencies in this table:
course, textbook -> lecturer, department: The course and textbook determine the lecturer and
the department.
course -> lecturer, department: The course determines the lecturer and the department.
lecturer -> department: The lecturer determines the department.
To decompose a table into a set of columns, you identify these columns, take the data from those columns
only, and remove the duplicates. For example, we create a table named Courses2 by first taking the
columns course, department, and lecturer from Courses1. We then take the data from these columns and
remove the duplicates. There are two rows with all of the values Relational Databases, Computer Science,
and Jeremy Brown in Courses1, but there is only one row with all of these values in Courses2.
60
Here’s the final result of the split:
Courses2
course department lecturer
Both of these tables are in 2NF. There are no functional dependencies within CourseTextbooks, and the
candidate key is {course, textbook}. The functional dependencies in Courses1 are:
The candidate key is {course}, and there are no functional dependencies on only a part of a key, so it is in
2NF.
The following problems have been removed in the new database:
INSERT: You can now add a course with no textbooks. Simply add the course
to Courses2 without adding rows to CourseTextbooks.
UPDATE: You can change the lecturer for the course Relational Databases by changing just one
row in Courses2.
This problem still persists:
61
DELETE: If we remove the Algorithms course, we also remove the lecturer James Cormen.
We’ll address this problem in a little.
Normalization to 3NF
Let’s look again at Courses2. It is in 2NF but not in 3NF.
Courses2
course department lecturer
62
Lecturers
lecturer department
Courses3
course lecturer
RESULT: -
Thus the program study for normal forms has been implemented using sql is executed and its output
is verified.
63
64