0% found this document useful (0 votes)
85 views50 pages

DBMS Lab Experiment-Reg

1. The document discusses creating simple queries with constraints such as primary keys, not null, check, and referential integrity constraints in SQL. 2. It also covers various subquery operators like UNION, INTERSECT, IN, BETWEEN, LIKE, NOT LIKE, ALL, and ANY. 3. Examples are provided for creating tables with different constraints, inserting values, and performing nested queries.

Uploaded by

vinu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views50 pages

DBMS Lab Experiment-Reg

1. The document discusses creating simple queries with constraints such as primary keys, not null, check, and referential integrity constraints in SQL. 2. It also covers various subquery operators like UNION, INTERSECT, IN, BETWEEN, LIKE, NOT LIKE, ALL, and ANY. 3. Examples are provided for creating tables with different constraints, inserting values, and performing nested queries.

Uploaded by

vinu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Ex No: 2.

A SIMPLE QUERIES WITH CONSTRAINTS AND NESTED QUERIES

AIM:

A) To create simple queries with constraints such as primary key, not null, check,
referential integrity.
B) To perform various subqueries and operators.

PROCEDURE:
Step 1: Start
Step 2: Create the table with its essential attributes.
Step 3: Insert attribute values into the table
Step 4: Execute different nested query Commands with constraints and extract
information from the table.
Step 5: Stop

i) SIMPLE QUERIES WITH CONSTRAINTS:

1. NOT NULL:

Used to set the not null constraint to the specified column name which will not allow null
values.

Syntax:
Create table tablename(fieldname1 datatype(constraint)not null,fieldname2 datatype,
…………….fieldnamen datatype);

Example:
SQL> create table notnull (eno varchar2(10) not null,ename varchar2(10),esalary
number(20));

Table created.

SQL> insert into notnull values('&eno','&ename','&esalary');


Enter value for eno: 1
Enter value for ename: arul
Enter value for esalary: 20000
old 1: insert into notnull values('&eno','&ename','&esalary')
new 1: insert into notnull values('1','arul','20000')

1 row created.

SQL> /
Enter value for eno:
Enter value for ename: raj
Enter value for esalary: 30000
old 1: insert into notnull values('&eno','&ename','&esalary')
new 1: insert into notnull values('','raj','30000')
insert into notnull values('','raj','30000')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."NOTNULL"."ENO")

2. CHECK:

Check constraint specify conditions that each tuple must satisfy.

Syntax:
Create table tablename(Fieldname1 datatype(constraint),Fieldname2 datatype,
………………….Fieldname3 datatype);

Example:
SQL> create table con( empid varchar2(20) not null,empname varchar2(20),empsalary
number(10) check(empsalary>10000));

Table created.

SQL> insert into con values('&empid','&empname','&empsalary');


Enter value for empid: 1
Enter value for empname: kumar
Enter value for empsalary: 20000
old 1: insert into con values('&empid','&empname','&empsalary')
new 1: insert into con values('1','kumar','20000')
1 row created.
SQL> /
Enter value for empid: 2
Enter value for empname: raja
Enter value for empsalary: 9000
old 1: insert into con values('&empid','&empname','&empsalary')
new 1: insert into con values('2','raja','9000')
insert into con values('2','raja','9000')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0010283) violated
3. UNIQUE:
Used to set unique constraint to the specified column name which will not allow
redundant values

Syntax:
Create table tablename(fieldname1 datatype(constraint)unique,fieldname2 datatype,
…………….Fieldname3 datatype);

Example:
SQL> create table conn(eno varchar2(10) unique,ename varchar2(20));

Table created.

SQL> insert into conn values('&eno','&ename');


Enter value for eno: 1
Enter value for ename: hello
old 1: insert into conn values('&eno','&ename')
new 1: insert into conn values('1','hello')

1 row created.

SQL> /
Enter value for eno: 1
Enter value for ename: hi
old 1: insert into conn values('&eno','&ename')
new 1: insert into conn values('1','hi')
insert into conn values('1','hi')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0010285) violated

4. PRIMARY KEY:
Primary key is a constraint for both unique and not null.

Syntax:
Create table tablename(Fieldname1 datatype(constraint)unique,fieldname2 datatype,
…………….Fieldname3 datatype);

Example:
SQL> create table con(empid varchar2(10),empname varchar2(20) primary key);

Table created.

SQL> insert into con values('&empid','&empname');


Enter value for empid: 1
Enter value for empname: kumar
old 1: insert into con values('&empid','&empname')
new 1: insert into con values('1','kumar')

1 row created.

SQL> /
Enter value for empid: 2
Enter value for empname: kumar
old 1: insert into con values('&empid','&empname')
new 1: insert into con values('2','kumar')
insert into con values('2','kumar')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0010286) violated

5. ADDING CONSTRIANT:

Used to set any constraint to the specified column at the last by specifying the
constraint type and field name.

Syntax:
Create table tablename(Fieldname1 datatype(constraint)unique,fieldname2 datatype,
constraint constraintname constrainttype(fieldname));

Example:
SQL> create table con(empid varchar2(10),empname varchar2(10),constraint c1
primary key(empid));

Table created.

SQL> insert into con values('&empid','&empname');


Enter value for empid: 1
Enter value for empname: anand
old 1: insert into con values('&empid','&empname')
new 1: insert into con values('1','anand')

1 row created.

SQL> /
Enter value for empid: 1
Enter value for empname: vijay
old 1: insert into con values('&empid','&empname')
new 1: insert into con values('1','vijay')
insert into con values('1','vijay')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.C1) violated

6. ADD CONSTRAINT(ALTER)
Used to set the constraint for the table already created by using alter command.

Syntax:
Alter table tablename add constraint constraintname (fieldname)datatype,primary key.

Example:
SQL> create table con(empid varchar2(10),empname varchar2(10));

Table created.

SQL> alter table con add constraint c1 primary key(empid);

Table altered.

SQL> desc con;


Name Null? Type
----------------------------------------- -------- -------------------

EMPID NOT NULL VARCHAR2(10)


EMPNAME VARCHAR2(10)

7. DROP CONSTRAINT:
Used to drop the constraint.

Syntax:
Alter table tablename drop constraint constraintname.

Example:
SQL> alter table con drop constraint c1;

Table altered.

SQL> desc con;


Name Null? Type
----------------------------------------- -------- ---------------------

EMPID VARCHAR2(10)
EMPNAME VARCHAR2(10)

8. REFERENTIAL INTEGRITY:
Used to refer the primary key of the parent table from the childtable.
Syntax:
a)Create table tablename(Fieldname1 datatype primary key,fieldname2 datatype,
…………….Fieldname3 datatype);
b) Create table tablename(Fieldname1 datatype references,Parent
tablename(fieldname)
…………….Fieldname n datatype);

Example:
SQL> create table parent(eno varchar2(10),ename varchar2(10) primary key);

Table created.

SQL> insert into parent values('&eno','&ename');


Enter value for eno: 1
Enter value for ename: ajay
old 1: insert into parent values('&eno','&ename')
new 1: insert into parent values('1','ajay')

1 row created.

SQL> /
Enter value for eno: 2
Enter value for ename: bala
old 1: insert into parent values('&eno','&ename')
new 1: insert into parent values('2','bala')

1 row created.

SQL> create table child (eno varchar2(10),ename varchar2(10) references


parent(ename));

Table created.

SQL> insert into child values('&eno','&ename');


Enter value for eno: 1
Enter value for ename: ajay
old 1: insert into child values('&eno','&ename')
new 1: insert into child values('1','ajay')

1 row created.

SQL> /
Enter value for eno: 2
Enter value for ename: balaji
old 1: insert into child values('&eno','&ename')
new 1: insert into child values('2','balaji')
insert into child values('2','balaji')
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.SYS_C0010290) violated - parent key not
Found

9. ON DELETE CASCADE:
The changes done in parent table is reflected in the child table when references are
made .

Syntax:
Create table tablename(Fieldname1 datatype references,Parent tablename(fieldname),
On delete cascade);

Example:

SQL> create table parent(eno varchar2(10),ename varchar2(10) primary key);


Table created.
SQL> insert into parent values('&eno','&ename');
Enter value for eno: 1
Enter value for ename: a
old 1: insert into parent values('&eno','&ename')
new 1: insert into parent values('1','a')

1 row created.

SQL> create table child


2 (
3 eno varchar2(10),
4 ename varchar2(10) references parent(ename) on delete cascade
5 );

Table created.

SQL> insert into child values('&eno','&ename');


Enter value for eno: 2
Enter value for ename: a
old 1: insert into child values('&eno','&ename')
new 1: insert into child values('2','a')
1 row created.

SQL> select * from parent;


ENO ENAME
---------- ----------
1 a

SQL> select * from child;

ENO ENAME
---------- ----------
2 a
SQL> delete from parent where eno=1;
1 row deleted.
SQL> select * from parent;
no rows selected
SQL> select * from child;
no rows selected

ii) SUBQUERY:

SINGLE ROW SUB-QUERY:

Syntax:

Select <fieldname> from <tablename1> where <fieldname>=


(select<fieldname>from<fieldname2>where (condition);

1. UNION:
Syntax:
Select <fieldlist> from <tablename1> where (condition) union select<fieldlist>
from<tablename2> where (condition);

2. INTERSECT:
Syntax:
Select <fieldlist> from <tablename1> where (condition) intersect select<fieldlist>
from<tablename2> where (condition);

3. IN:
Syntax:
Select <fieldlist> from <tablename1> where (condition) in select<fieldlist>
from<tablename2> where (condition);

4. BETWEEN:
Syntax:
Select <fieldlist> from <tablename1> where (condition) between select<fieldlist>
from<tablename2> where (condition);

5. LIKE:
Syntax:
Select <fieldlist> from <tablename> where <fieldname> like <expression>;

6. NOT LIKE:
Syntax:
Select <fieldlist> from <tablename> where <fieldname> not like <expression>;

7. ALL:
Syntax:
Select <fieldlist> from <tablename1>where <fieldname> all
Select <fieldlist> from <tablename2> where (condition);

8. ANY:
Syntax:
Select <fieldlist> from <tablename1> where (condition) any
Select <fieldlist> from <tablename2> where(condition);

OUTPUT / EXAMPLE (NESTED QUERIES):

SQL> create table employee(empno varchar2(10),empname varchar2(10),empsalary


number(10));

Table created.

SQL> drop table employee1;

Table dropped.

SQL> create table employee1(empno varchar2(10),empname varchar2(10),empsalary


number(10));

Table created.

SQL> insert into employee values('&empno','&empname','&empsalry');


Enter value for empno: 1
Enter value for empname: arun
Enter value for empsalry: 10000
old 1: insert into employee values('&empno','&empname','&empsalry')
new 1: insert into employee values('1','arun','10000')
1 row created.

SQL> /
Enter value for empno: 2
Enter value for empname: bala
Enter value for empsalry: 20000
old 1: insert into employee values('&empno','&empname','&empsalry')
new 1: insert into employee values('2','bala','20000')

1 row created.

SQL> insert into employee1 values('&empno','&empname','&empsalry');


Enter value for empno: 1
Enter value for empname: arun
Enter value for empsalry: 10000
old 1: insert into employee1 values('&empno','&empname','&empsalry')
new 1: insert into employee1 values('1','arun','10000')

1 row created.

SQL> /
Enter value for empno: 3
Enter value for empname: chitra
Enter value for empsalry: 40000
old 1: insert into employee1 values('&empno','&empname','&empsalry')
new 1: insert into employee1 values('3','chitra','40000')

1 row created.

SQL> select * from employee;

EMPNO EMPNAME EMPSALARY


---------- ---------- ----------
1 arun 10000
2 bala 20000

SQL> select * from employee1;

EMPNO EMPNAME EMPSALARY


---------- ---------- ----------
1 arun 10000
3 chitra 40000
Output for Subquery:

SQL> select * from employee where empsalary=(select min(empsalary)from employee1


);

EMPNO EMPNAME EMPSALARY


---------- ---------- ----------
1 arun 10000
UNION:
SQL> select empname,empno from employee where(empsalary>10000) union select
empname,empno from employee1 where(empsalary>10000);

EMPNAME EMPNO
---------- ----------
bala 2
chitra 3

INTERSECT:
SQL> select empname,empno from employee where(empsalary>9000) intersect select
empname,empno from employee1 where(empsalary>9000);

EMPNAME EMPNO
---------- ----------
arun 1

IN:
SQL> select empname from employee where empsalary in(select empsalary from
employee1);

EMPNAME
----------
arun

SQL> select empno,empname from employee where empno in(select empsalary from
employee1);

no rows selected

SQL> select empno,empname from employee where empno in(select empno from
employee1);

EMPNO EMPNAME
---------- ----------
1 Arun
BETWEEN:

SQL> select empno,empname from employee where empsalary between 10000 and
30000;

EMPNO EMPNAME
---------- ----------
1 arun
2 bala

LIKE:
SQL> select empname,empno from employee where empname like 'b%';

EMPNAME EMPNO
---------- ----------
bala 2

NOT LIKE:
SQL> select empname,empno from employee where empname not like 'b%';

EMPNAME EMPNO
---------- ----------
arun 1

ALL:
SQL> select empname,empsalary from employee1 where empsalary > all (select
empsalary from employee where empsalary>10000);

EMPNAME EMPSALARY
---------- ----------
chitra 40000

ANY:
SQL> select empname,empsalary from employee1 where empsalary>any (select
min(empsalary) from employee);

EMPNAME EMPSALARY
---------- ----------
chitra 40000
iii) AGGREGATE FUNCTION:

1. COUNT:
Syntax:
Select count(field list) from table_name where condition;

2. SUM:
Syntax:
Select sum(field name) from table_name;

3. MINIMUM:
Syntax:
Select min(field name) from table_name;

4. MAXIMUM:
Syntax:
Select max(field name) from table_name;

5. AVERAGE:
Syntax:
Select avg(field name) from table_name;

iv) NUMBER FUNCTIONS:

1. ABSOLUTE:
Syntax:
Select abs(value) from dual;

2. MODULE:
Syntax:
Select mod(value1,value2) from dual;

3. POWER:
Syntax:
Select power(value1,value2) from dual;

4. SQUARE ROOT:
Syntax:
Select sqrt(value) from dual;

5. ROUND:
Syntax:
Select round(value) from dual;
v) ORDER BY AND GROUP BY CLAUSE :

1. ORDERBY CLAUSE:
Syntax:
select field name from table_name where condition order by field name asc/desc;

2. GROUPBY CLAUSE:
Syntax:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

3. HAVING CLAUSE:
Syntax:
SELECT column_name, aggregate_function(column_name)FROM table_name
WHERE column_name operator value GROUP BY column_name HAVING
aggregate_function(column_name) operator value;

OUTPUT / EXAMPLE (AGGREGATE FUNCTIONS):

SQL> select * from employee;

EMPNO EMPNAME EMPSALARY


---------- ---------- ----------
1 arun 10000
3 chitra 30000
2 bala 20000
4 dhinesh 50000

COUNT:

SQL> select count (empno) from employee;

COUNT (EMPNO)
------------
4

SUM:

SQL> select sum(empsalary) from employee;

SUM(EMPSALARY)
--------------
110000

MINIMUM:

SQL> select min(empsalary) from employee;

MIN(EMPSALARY)
--------------
10000

MAXIMUM:

SQL> select max(empsalary) from employee;

MAX(EMPSALARY)
--------------
50000

AVERAGE:

SQL> select avg(empsalary) from employee;

AVG(EMPSALARY)
--------------
27500

NUMBER FUNCTIONS:

SQL> select abs(-75.7) from dual;

ABS(-75.7)
----------
75.7

SQL> select mod(15,35) from dual;

MOD(15,35)
----------
15

SQL> select power(5,5) from dual;

POWER(5,5)
----------
3125
SQL> select sqrt(64) from dual;

SQRT(64)
----------
8

SQL> select round(10.9) from dual;

ROUND(10.9)
-----------
11

CLAUSE :

SQL> select * from employee;

ENO ENAME ESALARY


---------- ---------- ----------
1 aravind 10000
3 chitra 20000
4 dhinesh 4000
2 bala 30000

ORDERBY CLAUSE :

SQL> select eno from employee order by eno;

ENO
----------
1
2
3
4

GROUPBY CLAUSE:

SELECT dept, SUM (salary)


FROM employee
GROUP BY dept;

dept salary
---------------- --------------
Electrical 25000
Electronics 55000
Aeronautics 35000
InfoTech 30000

HAVING CLAUSE:

SELECT dept, SUM (salary)


FROM employee
GROUP BY dept
HAVING SUM (salary) > 25000

dept salary
------------- -------------
Electronics 55000
Aeronautics 35000
InfoTech 30000

RESULT:

Thus the simple and nested query commands were studied and practiced
successfully.
Ex. No. 2. B JOINS

AIM:
To execute the SQL statements with joins.

PROCEDURE:

Step 1: Start
Step 2: Create the table with its essential attributes.
Step 3: Insert attribute values into the table
Step 4: Execute different join commands and extract information from the table.
Step 5: Stop

1. EQUI JOIN:
It retrieves rows from two tables having a common column using ‘=’ operators.

Syntax:
Select <tablename1.fieldlist,tablename2.fieldlist> from <tablename1><tablename2>
where <tablename1.keyfield>=<tablename2.keyfield>;

2. INNER JOIN :
Inner join returns the matching rows from the tables that are being joined.

Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 inner join tablename2
on tablename1.keyfield = tablename2.keyfield;

3. OUTER JOIN :
Outer join returns the both matching and non-matching rows for the tables that are
being joined. An outer join is an extended form of the inner join. The rows in one table
having no matching rows in the other table will also appear in the result table with nulls.

LEFT OUTER JOIN :


Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 left outer join
tablename2 on tablename1.keyfield = tablename2.keyfield;

RIGHT OUTER JOIN :


Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 right outer join
tablename2 on tablename1.keyfield = tablename2.keyfield;

4. FULL OUTER JOIN :


Syntax:
Select tablename1.fieldlist,tablename2.fieldlist from tablename1 full outer join
tablename2 on tablename1.keyfield = tablename2.keyfield;

OUTPUT / EXAMPLE(JOINS):

SQL> create table employee (empid varchar2(10),empname varchar2(10),deptid


varchar2(10) primary key);

Table created.

SQL> insert into employee values('&empid','&empname','&deptid');


Enter value for empid: 10cse01
Enter value for empname: anand
Enter value for deptid: cse
old 1: insert into employee values('&empid','&empname','&deptid')
new 1: insert into employee values('10cse01','anand','cse')

1 row created.

SQL> /
Enter value for empid: 10ece02
Enter value for empname: bala
Enter value for deptid: ece
old 1: insert into employee values('&empid','&empname','&deptid')
new 1: insert into employee values('10ece02','bala','ece')

1 row created.

SQL> /
Enter value for empid: 10mech03
Enter value for empname: karthi
Enter value for deptid: MECH
old 1: insert into employee values('&empid','&empname','&deptid')
new 1: insert into employee values('10mech03','karthi','MECH')

1 row created.

SQL> create table department(deptid varchar2(10) primary key,deptname


varchar2(20));

Table created.

SQL> insert into department values('&deptid','&deptname');


Enter value for deptid: cse
Enter value for deptname: computerscience
old 1: insert into department values('&deptid','&deptname')
new 1: insert into department values('cse','computerscience')

1 row created.

SQL> /
Enter value for deptid: ece
Enter value for deptname: electronics
old 1: insert into department values('&deptid','&deptname')
new 1: insert into department values('ece','electronics')

1 row created.

SQL> /
Enter value for deptid: mech
Enter value for deptname: mechanical
old 1: insert into department values('&deptid','&deptname')
new 1: insert into department values('mech','mechanical')

1 row created.

SQL> select * from employee;

EMPID EMPNAME DEPTID


---------- ---------- ----------
10cse01 anand cse
10ece02 bala ece
10mech03 karthi MECH

SQL> select * from department;

DEPTID DEPTNAME
---------- --------------------
ece electronics
cse computerscience
mech mechanical

EQUI JOIN:

SQL> select
employee.empid,employee.empname,department.deptid,department.deptname from
employee,department where employee.deptid = department.deptid;
EMPID EMPNAME DEPTID DEPTNAME
---------- ---------- ---------- --------------------
10ece02 bala ece electronics
10cse01 anand cse computerscience

INNER JOIN:

SQL> select employee.empid,department.deptname from employee inner join


department on employee.deptid = department.deptid;

EMPID DEPTNAME
---------- --------------------
10ece02 electronics
10cse01 computerscience

SQL> select * from employee inner join department on


employee.deptid=department.deptid;

EMPID EMPNAME DEPTID DEPTID DEPTNAME


---------- ---------- ---------- ---------- --------------------
10ece02 bala ece ece electronics
10cse01 anand cse cse computerscience

LEFT OUTER JOIN:

SQL> select employee.empname,department.deptname from employee left outer join


department on employee.deptid=department.deptid;

EMPNAME DEPTNAME
---------- --------------------
anand computerscience
bala electronics
karthi

RIGHT OUTER JOIN:

SQL> select employee.empname,department.deptname from employee right outer join


department on employee.deptid=department.deptid;
EMPNAME DEPTNAME
---------- --------------------
bala electronics
anand computerscience
mechanical

FULL OUTER JOIN:

SQL> select employee.empname,department.deptname from employee full outer join


department on employee.deptid=department.deptid;

EMPNAME DEPTNAME
---------- --------------------
anand computerscience
bala electronics
karthi mechanical

RESULT:

Thus the join queries were executed successfully.


Ex. No: 3. A VIEWS

AIM:

To write queries on views for display the particular tables.

PROCEDURE:
Step 1: Create table with some attributes.
Step 2: Create view from single table with all attributes.
Step 3: Create view from single table with selected attributes.
Step 4: Create View from two tables with all attributes.
Step 5: Create View from two tables with selected attributes.
Step 6: Use DML commands in views.
Step 7: Drop the View.

COMMAND EXECUTION

1. Create a view from single table containing all columns from the base table.

SQL> desc emp1;

Name Null? Type


----------------------------------------- -------- ----------------------------
EID NUMBER(3)
ENAME VARCHAR2(10)
AGE NUMBER(3)
SALARY NUMBER(6)
DOB DATE

SQL> create view viiew as select * from emp1;

View created.

SQL> desc viiew;


Name Null? Type
----------------------------------------- -------- ----------------------------
EID NUMBER(3)
ENAME VARCHAR2(10)
AGE NUMBER(3)
SALARY NUMBER(6)
DOB DATE

2. Create view from single table with selected columns.

SQL> create view viiew1 as select eid,ename,salary from emp1;


View created.

SQL> desc viiew1;

Name Null? Type


----------------------------------------- -------- ----------------------------
EID NUMBER(3)
ENAME VARCHAR2(10)
SALARY NUMBER(6)

3. Create view from two table containing all columns from the base table.

SQL> create view viiew2 as select * from emp1 full natural join emp2;
View created.

SQL> desc viiew2;


Name Null? Type
----------------------------------------- -------- ----------------------------
SALARY NUMBER(6)
EID NUMBER(3)
ENAME VARCHAR2(10)
AGE NUMBER(3)
DOB DATE
EMPID NUMBER(3)
EMPNAME VARCHAR2(10)

4. Create view from two table with selected columns.

SQL> create view viiew3 as select ename, age, empid from emp1,emp2;

View created.

SQL> desc viiew3;


Name Null? Type
----------------------------------------- -------- ----------------------------
ENAME VARCHAR2(10)
AGE NUMBER(3)
EMPID NUMBER(3)

DML Commands in Views:

1. INSERT:
SQL> insert into viiew1 values('&eid','&ename','&salary');
Enter value for eid: 1
Enter value for ename: anu
Enter value for salary: 25000
old 1: insert into viiew1 values('&eid','&ename','&salary')
new 1: insert into viiew1 values('1','anu','25000')

1 row created.

SQL> /
Enter value for eid: 2
Enter value for ename: abi
Enter value for salary: 20000
old 1: insert into viiew1 values('&eid','&ename','&salary')
new 1: insert into viiew1 values('2','abi','20000')

1 row created.

SQL> /
Enter value for eid: 3
Enter value for ename: cibi
Enter value for salary: 37000
old 1: insert into viiew1 values('&eid','&ename','&salary')
new 1: insert into viiew1 values('3','cibi','37000')

1 row created.

SQL> /
Enter value for eid: 4
Enter value for ename: babu
Enter value for salary: 24000
old 1: insert into viiew1 values('&eid','&ename','&salary')
new 1: insert into viiew1 values('4','babu','24000')

1 row created.

SQL> /
Enter value for eid: 5
Enter value for ename: soman
Enter value for salary: 27000
old 1: insert into viiew1 values('&eid','&ename','&salary')
new 1: insert into viiew1 values('5','soman','27000')

1 row created.

SQL> /
Enter value for eid: 6
Enter value for ename: billu
Enter value for salary: 28000
old 1: insert into viiew1 values('&eid','&ename','&salary')
new 1: insert into viiew1 values('6','billu','28000')

1 row created.

SQL> select * from viiew1;

EID ENAME SALARY


---------- ---------- -----------------------------

1 anu 25000
2 abi 20000
3 cibi 37000
4 babu 24000
5 soman 27000
6 billu 28000

6 rows selected.

2. UPDATE:

SQL>update viiew1 set eid='8' where ename=’abi’;

1 row updated

EID ENAME SALARY


---------- ---------- -----------------------------

1 anu 25000
8 abi 20000
3 cibi 37000
4 babu 24000
5 soman 27000
6 billu 28000

SQL> select * from viiew1;

EID ENAME SALARY


---------- ---------- --------------------------------

1 anu 25000
2 abi 20000
3 cibi 37000
4 babu 24000
6 billu 28000

3. DELETE

SQL> delete from viiew1 where eid='5';

1 row deleted.

SQL> select * from viiew1;

EID ENAME SALARY


---------- ---------- --------------------------------

1 anu 25000
2 abi 20000
3 cibi 37000
4 babu 24000
6 billu 28000

5 rows selected.

DROP VIEW:

SQL> drop view viiew2;

View dropped.

SQL> desc viiew2;


ERROR:
ORA-04043: object viiew2 does not exist

RESULT:

Thus the views were created and manipulated using insert, delete and update
commands.
Ex: No: 3. B SYNONYMS

AIM:
To write query for synonyms to minimize the impact of moving or renaming the
database objects.

PROCEDURE:
Step 1: Create the database which contains several tables using DDL and DML
command.
Step 2: Select particular table from the database provide permission using grand
command.
Step 3: Create temporary table name for that particular table.
Step 4: Display the table using select command.

EXPLANATION
A synonym is an alias for a database object. Synonyms may be used to reference
the original object in SQL as well as PL/SQL.
They can be used to hide ownership and location of the database objects they refer
to and minimize the impact of moving or renaming the database objects.

COMMAND EXECUTION
EXAMPLE

In your database you have a schema called pacet. This schema contains a table
called EMPLOYEES.

To query the table you use the following SELECT statement:


SELECT * FROM PACET.EMPLOYEES;

Grant select privileges to all database users


GRANT SELECT ON PACET.EMPLOYEES TO PUBLIC;

Create a public synonym for your table


GRANT SELECT ON PACET.EMPLOYEES TO PRIVATE;

Create a public synonym for your table


CREATE PUBLIC SYNONYM EMPLOYEE_DATA FOR PACET.EMPLOYEES;

From now on, anyone can query your table using the synonym
SELECT * FROM EMPLOYEE_DATA;

RESULT:

Thus the synonyms were created using Data Control Language commands.
Ex: No: 3. C SEQUENCES

AIM:
To write query for sequence to database object from which multiple users may
generate unique integers.

PROCEDURE:
Step 1: Create the sequence using create sequence command.
Step 2: Delete the sequence using drop sequence command.
Step 3: Save the current point transaction using savepoint command.
Step 4: Perform undo operation using rollback command.

COMMAND EXECUTION

1. Create

Syntax:
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;

Example:
CREATE SEQUENCE employee
MINVALUE 1
MAXVALUE 888888888888888888888
START WITH 1
INCREMENT BY 1
CACHE 20;

2. Drop

Syntax :
DROP SEQUENCE sequence_name;

Example:
DROP SEQUENCE employee;

3. SAVEPOINT

Syntax:
SAVEPOINT identifier
RELEASE SAVEPOINT identifier

Example:
UPDATE employees
SET salary = 7000
WHERE last_name = 'SURESH';
SAVEPOINT sureshsave;

UPDATE employees
SET salary = 12000
WHERE last_name = 'SUNDAR';
SAVEPOINT sundarsave;

4. ROLLBACK

Syntax:
ROLLBACK [WORK] TO [SAVEPOINT] identifier

Example:
SELECT SUM (salary) FROM employees;

ROLLBACK TO SAVEPOINT sureshsave;

UPDATE employees
SET salary = 11000
WHERE last_name = 'SUNDAR';

COMMIT;

RESULT:

Thus the sequences were created using Transaction Control Language


commands.
Ex. No: 4 CURSOR

AIM
To write a Cursor Procedure to calculate Payroll process of an Employee.

PROCEDURE
Step 1: Start
Step 2: Initialize the Cursor Procedure based on the table attributes to which the actual
operation has to be carried out.
Step 3: Develop the procedure with the essential operational parameters.
Step 4: Specify the Individual operation to be each attribute.
Step 5: Execute the Cursor procedure.
Step 6: Stop

COMMAND EXECUTION

SQL> create table salary (emp_no number(4) primary key,emp_name


varchar2(30),designation varchar2(25),department varchar2(30),basic
number(5),da_percent number(6,2), ma number(6,2),other_allowances
number(6,2),deduction number(6,2));

Table created.

SQL> insert into Sal values (1,'vijay','manager','Accounts',6000,45,200,250,1500.75);

1 row created.

SQL> insert into sal values (2,'vasanth','Asst.manager','Accounts',4000,45,200,200,1200);

1 row created.

SQL> insert into Sal values(3,'priya','Steno','sales',2000,45,100,50,200);

1 row created.

SQL> select * from sal;


Emp_no Emp_nameDesignation Department Basic da_percent MA other_allowance
Deduction

1 vijay manager Accounts 6000 45 200 250


1500.75
2 vasanth Asstmanager Accounts 4000 45 200 200
1200
3 priya Steno sales 2000 45 100 50
200

Example:

SQL> declare
e_no number(6);
e_name varchar2(25);
net_salary number(8,2);
cursor cur_salary is select emp_no,
emp_name,basic+da_percent*basic/100+ma+other_allowance-deduction from sal;
begin
dbms_output.put_line('emp no '||' Name '||' Net salary');
dbms_output.put_line('--------------------');
open cur_salary;
loop
fetch cur_salary into e_no,e_name,net_salary;
exit when cur_salary%notfound;
dbms_output.put_line(rpad(e_no,10,' ')||rpad(e_name,25,' ')||net_salary);
end loop;
close cur_salary;
end;
/

OUTPUT:
emp no Name Net salary

1 vijay 7649.25
2 vasanth 5000
3 priya 2850

PL/SQL procedure successfully completed.

RESULT:

Thus the Cursor Procedure for calculating the Payroll process has been executed
successfully.
Ex: No: 5. A PROCEDURES

AIM:

To write a PL/SQL block for the given problem.

PROCEDURE:
Step 1: Declare the necessary variables.
Step 2: Begin the program.
Step 3: Assign Values to the variables.
Step 4: Write the looping condition
Step 5: Perform calculation.
Step 6: End loop.
Step 7: Execute the procedure

1. Create

Syntax:
CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] IS
Declaration section
BEGIN
Execution section
EXCEPTION
Exception section
END;

Example:
SQL> create or replace procedure sum(n number) is
i number;
sum1 number;
begin
sum1:=0;
i:=0;
while i<=n
loop
sum1:=sum1+i;
i:=i+1;
end loop;
dbms_output.put_line(sum1);
end;
/

Procedure created.
Execution of procedure:

EXECUTE [or EXEC] procedure_name;

SQL> EXEC SUM(4);


10

PL/SQL procedure successfully completed.

2. Drop:

Syntax:
Drop Procedure procedure_name;

Example:
Drop Procedure Sum;

RESULT:

Thus the Procedure has been executed successfully.


Ex. No: 5. B FUNCTIONS

i) Implementation of Factorial Using Function

Aim :

To create a function for Factorial.

Procedure:
Step 1: Declare the necessary variables.
Step 2: Begin the program.
Step 3: Assign Values to the variables.
Step 4: Write the looping condition
Step 5: Perform calculation.
Step 6: End loop.
Step 7: Create the function
Step 8: Create the main procedure and call the function
Step 9:Execute the procedure.

COMMAND EXECUTION

SQL> create function fnfact(n number)


2 return number is
3 b number;
4 begin
5 b:=1;
6 for i in 1..n
7 loop
8 b:=b*i;
9 end loop;
10 return b;
11 end;
12 /

Function created.

SQL> Declare
2 n number:=&n;
3 y number;
4 begin
5 y:=fnfact(n);
6 dbms_output.put_line(y);
7 end;
8 /
Enter value for n: 5
old 2: n number:=&n;
new 2: n number:=5;
120

PL/SQL procedure successfully completed.

ii) Accessing Database Using Function:


Aim:
To write a Functional procedure to search an address from the given database.
Procedure:
Step 1: start
Step 2: create the table with essential attributes.
Step 3: initialize the function to carry out the searching procedure..
Step 4: frame the searching procedure for both positive and negative searching.
Step 5: execute the function for both positive and negative result .
Step 6: stop

COMMAND EXECUTION

SQL> create table phonebook(phone_no number(6) primary key,username


varchar2(30),doorno varchar2(10), street varchar2(30),place varchar2(30),pincode
char(6));

Table created.

SQL> insert into phonebook values(20312,'vijay','120/5D','bharathi street','NGO


colony','629002');

1 row created.

SQL> insert into phonebook values(29467,'vasanth','39D4','RK bhavan','sarakkal


vilai','629002');

1 row created.

SQL> select * from phonebook;

PHONE_NO USERNAME DOORNO STREET PLACE PINCODE


------------------------------- ------------- ---------------- --------------------
20312 vijay 120/5D bharathi street NGO colony 629002

29467 vasanth 39D4 RK bhavan sarakkal vilai 629002


SQL> create or replace function findAddress(phone in number) return varchar2 as
address varchar2(100);
begin
select username||','||doorno ||','||street ||','||place||','||pincode into address from
phonebook
where phone_no=phone;
return address;
exception
when no_data_found then return 'address not found';
end;
/

Function created.

SQL>declare
address varchar2(100);
begin
address:=findaddress(20312);
dbms_output.put_line(address);
end;
/

OUTPUT 1:
vijay,120/5D,bharathi street,NGO colony,629002

PL/SQL procedure successfully completed.

SQL> declare
2 address varchar2(100);
3 begin
4 address:=findaddress(23556);
5 dbms_output.put_line(address);
6 end;
7 /

OUTPUT2:
address not found
PL/SQL procedure successfully completed.

RESULT:

Thus the Function for searching process has been executed successfully
Ex. No: 6 TRIGGERS

AIM:

To write triggers for insert, delete and update of table

PROCEDURE:

Step 1: Start
Step 2: Initialize the trigger with specific table id.
Step 3:Specify the operations (update, delete, insert) for which the trigger has to be
executed.
Step 4: Execute the Trigger procedure for both Before and After sequences
Step 5: Carryout the operation on the table to check for Trigger execution.
Step 6: Stop

Database Triggers:

A database trigger is a stored procedure. It is fired implicitly when insert, update or


delete statements are issued against the table on which the trigger is defined.

COMMAND EXECUTION
Syntax:
Create or replace trigger <Trigger_name> before/after insert/delete/update on
<Table_name> for each row/statement

DELCARE
<Declaration statements>
BEGIN
<Executable Statements>
END:

Example
SQL> create table emply101(eid varchar(20),ename char(25), age number(10), salary
number(10));

Table created.

SQL> desc emply101;

Name Null? Type


------------------------------------------ -------- ----------------------------
EID VARCHAR2(20)
ENAME CHAR(25)
AGE NUMBER(10)
SALARY NUMBER(10)
SQL> insert into emply101 values('&eid','&ename','&age','&salary');
Enter value for eid: e1
Enter value for ename: anu
Enter value for age: 25
Enter value for salary: 20000
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e1','anu','25','20000')

1 row created.

SQL> /
Enter value for eid: e2
Enter value for ename: abi
Enter value for age: 24
Enter value for salary: 25700
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e2','abi','24','25700')
1 row created.

SQL> /
Enter value for eid: e3
Enter value for ename: babu
Enter value for age: 30
Enter value for salary: 34000
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e3','babu','30','34000')

1 row created.

SQL> /
Enter value for eid: e4
Enter value for ename: cibi
Enter value for age: 32
Enter value for salary: 32000
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e4','cibi','32','32000')

1 row created.

SQL> /
Enter value for eid: e5
Enter value for ename: brindha
Enter value for age: 28
Enter value for salary: 31000
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e5','brindha','28','31000')

1 row created.

SQL> select * from emply101;

EID ENAME AGE SALARY


-------------------- ------------------------- ---------- ----------
e1 anu 25 20000
e2 abi 24 25700
e3 babu 30 34000
e4 cibi 32 32000
e5 brindha 23 31000

SQL> create table emply102(eid varchar(20),ename char(25),age number(10),salary


number(10));

Table created.

SQL> desc emply102;


Name Null? Type
----------------------------------------- -------- ----------------------------
EID VARCHAR2(20)
ENAME CHAR(25)
AGE NUMBER(10)
SALARY NUMBER(10)

SQL> create or replace trigger tfg1 before insert on emply101 for each row
2 begin
3 insert into emply102 values(:new.eid,:new.ename,:new.age,:new.salary);
4 end;
5 /

Trigger created.

SQL> insert into emply101 values('&eid','&ename','&age','&salary');


Enter value for eid: e7
Enter value for ename: thara
Enter value for age: 28
Enter value for salary: 30000
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e7','thara','28','30000')
1 row created.

SQL> select * from emply102;

EID ENAME AGE SALARY


-------------------- ------------------------- ---------- ----------
e7 thara 28 30000

SQL> insert into emply101 values('&eid','&ename','&age','&salary');


Enter value for eid: e8
Enter value for ename: siju
Enter value for age: 29
Enter value for salary: 36000
old 1: insert into emply101 values('&eid','&ename','&age','&salary')
new 1: insert into emply101 values('e8','siju','29','36000')

1 row created.

SQL> select * from emply102;

EID ENAME AGE SALARY


-------------------- ------------------------- ---------- ----------
e7 thara 28 30000
e8 siju 29 36000

SQL> create or replace trigger trgrr15 after delete or update on emply101 for each row
2 begin
3 if deleting then
4 insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
5 elsif updating then
6 insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
7 end if;
8 end;
9 /

Trigger created.

SQL> select * from emply101;

EID ENAME AGE SALARY


-------------------- ------------------------- ---------- ----------
e1 anu 25 20000
e2 abi 24 25700
e3 babu 30 34000
e4 cibi 32 32000
e5 brindha 23 31000
e7 thara 28 30000
e8 siju 29 s36000

7 rows selected.

SQL> delete from emply101 where ename='babu';

1 row deleted.

SQL> select * from emply102;

EID ENAME AGE SALARY


-------------------- ------------------------- ---------- ----------
e7 thara 28 30000
e8 siju 29 36000
e3 babu 30 34000

SQL> update emply101 set ename='sanju' where eid='e5';

1 row updated.

SQL> select * from emply102;

EID ENAME AGE SALARY


-------------------- ------------------------- ---------- ----------
e7 thara 28 30000
e8 siju 29 36000
e3 babu 30 34000
e5 brindha 23 31000

RESULT:

Thus the trigger for insert, delete and update are created.
Ex. No: 7 EXCEPTION HANDLING

AIM:
To perform exception handling for customer details.

PROCEDURE:
Step 1: start
Step 2: create the table with essential attributes.
Step 3: declare the user defined exception
Step 4: Raise the exception and handle it.
Step 5: Execute the result.
Step 6: stop

COMMAND EXECUTION

DECLARE c_id customers.id%type := &cc_id;


c_name customers.name%type;
c_addr customers.address%type;
ex_invalid_id EXCEPTION;
BEGIN IF c_id <= 0 THEN
RAISE ex_invalid_id; ELSE
SELECT name, address INTO c_name, c_addr
FROM customers WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
END IF; EXCEPTION
WHEN ex_invalid_id THEN
dbms_output.put_line('ID must be greater than zero!');
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN dbms_output.put_line('Error!');
END;
/

RESULT

Enter value for cc_id: -6 (let's enter a value -6)


old 2: c_id customers.id%type := &cc_id;
new 2: c_id customers.id%type := -6;
ID must be greater than zero!

RESULT:

Thus the exception handling for customer details are executed.


Ex. No:8 DATABASE DESIGN USING ER MODELING AND NORMALIZATION

A. DATABASE DESIGN USING ER MODELING

AIM:
To create ER diagram for a software company with the following details,
1. Registered Students Details.
2. Courses Offered.
3. Students and their enrolled courses.
4. Registered Faculty Details.
5. Each student enrollment details like (Fee paid,Join date,Course Completion Date)
needs to be recorded.
6. Faculty and their handled courses during various durations.
Constraints:
 One student can join in many courses .e.g A student can join in both c and cpp
courses.
 A course should be taught by only one faculty during particular duration(start
date ,end date)
 A faculty can teach many courses.
 Few students may get discount on a course fee.

ER Diagram

Conceptual Database Design Deals with modeling concepts as entities, relationships,


attributes. Its final output is ER diagram.

The following steps are followed,


 Identify the Entities
 Find relationships
 Identify the key attributes for every Entity
 Identify other relevant attributes
 Identify the Primary Key

Identified Entity Sets:


1. Student
2. Course
3. Faculty

Identified Relation Ship Sets:


1. Enroll
2. Teach

Describe entity sets with attributes:


Student: Sid,Sname,City,Pin,Phno,EmaiIId.
Course: Cid,Cname,Credits.
Faculty: Fid,FName, City,Pin,Phno,EmaiIId

Describe Relation ship sets with required attributes:


Enroll: JDate, EDate, Fees

Fees is associated enrollment as Same course can be offered at different fees for
different student.
Teach:SDate, EDate
B. DATABASE DESIGN USING NORMALIZATION

Problem
Consider any University Examination System, which consists of the enrolled students
result details. Suppose the university wants to maintain not only the results, but also
performance of the faculty, then it enhances the existing system by adding the details of
the Instructors.

The Table contains:


Results (S#, Sname#, DOB, C#, Cname,subject, Semester, Year, FID, Fname, CC,
ColName, DateOfExam, Marks, Grade)

Solution

To eliminate the anomalies like redundancy, insertion anomaly, updation anomaly and
deletion anomaly from any table, it should be decomposed into smaller relations using
Normalization techniques.

Introduction:
Basically normalization eliminates the duplicate data and makes insert, update and
delete operations much more efficient in terms of performance and space requirement
to store the data.

Hence we need to refine our design so that we make an efficient database in terms of
storage space and inserts, updates and deletes operations. The refining technique is
called as Normalization

Decomposition of a large relation in to smaller relations is called as normalization


To solve the above problem, the relation can be refined upto 3 NF.
First Normal Form (I NF):
A relation R is said to be in the first normal form (1 NF) if and only if all the attributes of
the relation are atomic in nature.
o There are no repeated groups in the table.
o All the attributes are defined.

In the above table, all the attributes are atomic, meaning they are not further
decomposable. So the above table satisfies the I NF.

Second Normal Form (2 NF):


A relation R is said to be in the second normal form (2 NF) if and only if
 it is in the first normal form.
 no partial dependency exists between non-key attributes and key attributes.

Note - 1: A key attribute a candidate key, which can uniquely defines the other
attributes.
Note – 2: We can define functional dependency as,in a given relation R, X and Y are
attributes. Attribute Y is functionally dependent on attribute X if each value of X
determines EXACTLY ONE value of Y. This is represented as: X -> Y.
Note – 3: We can define partial functional dependency as, in a given relation R, X and Y
are attributes. Attribute Y is partially functionally dependent on attribute X only if it is
functionally dependent on sub-set of X.

In the above table,


 S# is a key attribute.
 C# is a key attribute.
 S#, Subject is a composite key.
 FID is a key attribute.
 CC is a key attribute.
 S#, Subject is composite key
 other attributes like Sname#, DOB, Cname,subject, Semester, Year, Fname,
ColName, DateOfExam, Marks, Grade are non-key attributes.

To make this table 2NF, we have to remove all the partial dependencies.
 Sname and DOB depends only on S#.
 Cname, sem, Year and DOE depends only on C# Subject.
 Marks and Grade depends only on S# Subject CC.
 Fname depends only on FID CC.
 Cname depends only on C#.
 ColName depends only on CC.
In the above tables, No partial dependency exists between the key attributes and non-
key attributes. Hence the tables are in Second Normal Form (2 NF).
Third Normal Form (3 NF)
A relation R is said to be in the third normal form (3 NF) if and only if
 it is in the second normal form.
 no transitive dependency exists between non-key attributes and key attributes.

Note – 4: if A -> B and B -> C then A -> C is Transitive.

There is no transitive dependency in the above sub tables except Grade Table. The
GRADE is depends on MARKS and in turn MARKS depends on S# Subject CC. To bring
this table to third normal form we need to take off this Transitive Dependence. To
eliminate transitive dependency, we can decompose the RESULT table into MARKS and
GRADE.

Final Results:
 Student (S#, Sname, DOB)
 Corse (C#, Cname)
 Sub_Date(C#, Subject, Semester, Year, DOE)
 Faculty(FID, Fname, CC)
 Marks(S#, Subject, CC, Marks)
 Grade(lowaebound, upperbound, grade)

All the anomalies are removed and all dependencies are preserved.

RESULT:
Thus the database design usind ER diagram and normalization was performed for the
given problem.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy