07i409 - Dbms Lab

46
BANNARI AMMAN INSTITUTE OF TECHNOLOGY SATHYAMANGALAM DEPARTMENT OF INFORMATION TECHNOLOGY LAB MANUAL 07I409 - DBMS LABORATORY 1

Transcript of 07i409 - Dbms Lab

BANNARI AMMAN INSTITUTE OF TECHNOLOGY

SATHYAMANGALAM

DEPARTMENT OF INFORMATION TECHNOLOGY

LAB MANUAL

07I409 - DBMS LABORATORY

1

BANNARI AMMAN INSTITUTE OF TECHNOLOGY

SATHYAMANGALAM

DEPARTMENT OF INFORMATION TECHNOLOGY

LAB MANUAL

07I409 - DBMS LABORATORY

Faculty In -Charge HOD - IT

2

INDEX

S.No. Name PageNo.

1 Syllabus 3

2 Hardware and software Requirements 4

3 List of Experiments 5 - 35

3

07I409 DBMS LABORATORY

0 0 3 2

1. Creating a database, simple queries.

2. Uses of select statement for queries using.

(i) AND, OR, NOT operation

(ii) Union, Intersection, Projection and Join operations

(iii) Sorting and grouping

3. Nested queries using SQL.

4. Built-in functions of SQL.

5. Update operations using SQL.

6. Use of indexes, creating views and querying in views.

7. Embedding SQL with C.

8. Library information system.

9. Student evaluation system.

10. Computerized quiz.

11. Computer center reservation of computing machines.

12. Income tax calculation.

13. Pay roll system.

14. Election processing system.

4

Hardware and software Requirements

Operating System : Windows XP

Primary Memory Capacity : 4 GB RAM

Secondary Memory Capacity: 500 GB HDD

Front End : Visual Basic 6.0

Data Base : Oracle 10G

5

EX.NO:1

DDL COMMANDS USING ORACLE

AIM:

To execute queries in the Data Definition Language (DDL) commands using oracle.

QUERIES:

1. CREATE:

PURPOSE: To create table in the database.

SYNTAX: CREATE TABLE "tablename"("column 1"

"data_type_for_column_1","column 2" "data_type_for_column_2",…);

2. ALTER:

PURPOSE: To change the table definition.

SYNTAX: ALTER TABLE "tablename"[alter specification];

A) ADD:

PURPOSE: To add a column definition to a table.

SYNTAX: ALTER table "tablename" ADD "column name" datatype;

B) DROP:

PURPOSE: To drop a column from a table.

SYNTAX: ALTER TABLE "tablename" DROP “column name”;

C) CHANGE:PURPOSE:

To change the default value for a column.

6

SYNTAX: ALTER TABLE tablename CHANGE “new column name” “old Column name” data type;

D) MODIFY:

PURPOSE:

To change the data type of the fieldname.SYNTAX:

ALTER TABLE "tablename" MODIFY “column name” data type;

3) DROP:

PURPOSE: To drop the table.

SYNTAX: DROP table "tablename";

4) RENAME:

PURPOSE: To change the table name.

SYNTAX: RENAME “old table name” to “new table name”;

5) TRUNCATE:

PURPOSE: To get rid of the data but not the table itself.

SYNTAX:

TRUNCATE TABLE "tablename";

7

EX.NO:2

DDL COMMANDS USING ORACLE

AIM:

To execute queries in the data manipulation language (DML). And data control language (DCL)

commands using oracle.

QUEIRES:

DML COMMANDS:

1. INSERT:

PURPOSE:

For adding data to the database.

A) SINGLE-ROW ‘INSERT’ STATEMENT:

PURPOSE:

Adds a new row to the table.

SYNTAX:

Insert into table name

( “columnname1”,”columnname2”…) values

(“value1”,”value2”…);

B) MULTI-ROW ‘INSERT’ STATEMENT:

PURPOSE:

Adds multiple rows of data

SYNTAX:

insert into tablename1

(“columnname1”,”columnname2”…) select

(“columnname1”,”columnname2”…) from tablename2

Where “columnname” condition;

2. SELECT:

PURPOSE:

It lists the data items from the database.

A) FROM:

PURPOSE:

It lists the tables that contain the data to be retrieved by

the query.

8

SYNTAX:

select * from tablename;

select “columnname” from “tablename”;

B) WHERE:

PURPOSE:

It is used to specify the desired rows.

SYNTAX:

select “columnname” from “tablename” where

“columnname”condition;

3. UPDATE:

PURPOSE:

Modifies existing data in the database.

SYNTAX:

A) SINGLE-ROW:

update “tablename” set “columnname”= [value1],”

columnname2”= [value2] where {condition};

B) MULTI-ROW:

update “tablename” set”columnname”= [new value]

where {condition};

4. DELETE:

PURPOSE:

To get rid of records from a table.

SYNTAX:

Delete from tablename where {condition};

DCL/TCL COMMANDS:

1. COMMIT:

PURPOSE:

Commit (make persistent) all changes for the current

transaction, commit may cause a concurrent transaction

conflict exception to be thrown. It is used to commit the

database.9

SYNTAX:

commit;

2. ROLLBACK:

PURPOSE:

Rollback (rescind) all changes for the current

transaction. Rollback option is used to rollback any work

since the last commit.

SYNTAX:

A) rollback;

B) rollback to savepoint [savepoint_name];

3. SAVEPOINT:

PURPOSE:

The savepoint statement sets a named transaction savepoint

with a name of identifier. If the current transaction has a

savepoint with the same name, the old savepoint is deleted.

SYNTAX:

savepoint savepoint_name;

4.GRANT:

PURPOSE:

The grant statement enables system administrators to

create my SQL user accounts and grant rights to

accounts. It gives privileges to users.

SYNTAX:

grant privilege on tablename to username;

(For particular field)

grant all on tablename to username; (For all)

5. REVOKE:

PURPOSE:

The revoke statement is to remove privilege from the user.

SYNTAX:

revoke privilege on tablename from username;

(For particular field)

revoke all on tablename from username;

(For all)

10

CREATE USER:

PURPOSE:

To create user accounts SQL.

SYNTAX:

Create user username identified by password;

11

EX.NO : 3

SORTING AND GROUPING

AIM:

To perform the Grouping and Sorting(Ascending and Decending) using SQL commands.

SORTING:

1. ASCENDING

Oracle is the basically display the values on Ascending order which is used to display the values.

Syntax:

select select-list from from-list where qualification group by col order by col;

2. DECENDING

It display the attributes in descending order

Syntax:

select select-list from from-list where qualification group by col order by col desc;

GROUPING:

It display the attributes by group the colomn based on the group functions like Sum() , Avg(),

Max(), Min(), Count().

Syntax:

select select-list from from-list where qualification group by col having qualification.

Example 1:

SQL> select s.rating,min(s.age) as minimumage from sailors s where s.age >= 18 group by s.rating

having count(*)>1;

Example 2:

SQL> select b.bid,COUNT(b.bid) as noofreservations from boats b,reserves r where b.color='red' AND

b.bid=r.bid group by b.bid having count(*)>0;

12

Example 3:

SQL> select s.rating,AVG(s.age) as averageage from sailors s where s.age>0 group by s.rating having

count(*)>0;

Example 4:SQL> select s.rating,AVG(s.age) as averageage from sailors s where s.age>0 group by s.rating having

count(*) > 1;

Example 5:

SQL> select s.rating,avg(age) from sailors s group by s.rating having avg(age)>35;

CONSTRAINTS

AIM:

To perform types of constraints like Domain constraints, Entity integrity constraints, Referential

integrity constraints using SQL commands.

CONSTRAINTS:

Constraints ensure that changes made to the database by authorised user do not result in a loss of data

consistency. There are 3 types of integrity constraints:

Domain integrity constraint

Entity integrity constraint

Referential integrity constraint

1. DOMAIN INTEGRITY CONSTRAINT: It verifies whether the data entered is in a proper/correct from is also set the range foe input data. There are 2 types of domain integrity constraints

Not null constraint Check constraint

1 a.NOT NULL CONSTRAINT: DEFINITION: The not null specification prohibits the insertion of a null value for this attribute. Any database

13

modification that would cause a null to be inserted in an attribute declared to be not null generates error diagnostic.

SYNTAX: create table table_name(attribute1 datatype(size)constraint const_name not null,attribute2 datatype(size),…..);EX:SQL> create table erno(rno number(2) not null)SQL> /

Table created.

SQL> desc erno; Name Null? Type ----------------------------------------- -------- ---------------------------- RNO NOT NULL NUMBER(2)

SQL> insert into erno values(&rno);Enter value for rno: 2old 1: insert into erno values(&rno)new 1: insert into erno values(2)

1 row created.

SQL> /Enter value for rno: old 1: insert into erno values(&rno)new 1: insert into erno values() insert into erno values() *ERROR at line 1:ORA-00936: missing expression

1.b.CHECK CONSTRAINT:

DEFINITION:

The check clause in SQL can be applied to relation declarations as well as to domain declaration, the clause check(P) specifies a predicate p that must be specified by every tuple is a relation.SYNTAX: Create table table_name(attribute1 datatype(size)constraint const_name check(condition),attribute2 datatype(size),…..);Ex:SOL>create table st(salary number(5) constraint sa check(salary>5000));

Table created.

SQL> insert into st values(&salary);Enter value for salary: 6000old 1: insert into st values(&salary)new 1: insert into st values(6000)

1 row created.14

SQL> /Enter value for salary: 3000

old 1: insert into st values(&salary)new 1: insert into st values(3000)insert into st values(3000)*ERROR at line 1:ORA-02290: check constraint (IT180.SA) violated

.

2. ENTITY INTEGRITY CONSTRAINT:

It is used to enforce the consistency of our database.There are 2 types entity integrity constraints: Unique constraint Primary Key constraint

2. a. UNIQUE CONSTRAINT: DEFINITION:

The unique specification says that attributes Aj1, Aj2….Ajm form a candidate key that is no two tuples in the relation can be equal on all primary key attributes are permitted to be null unless they have explicitly been declared to be not null.

SYNTAX:

Create table table_name(attribute1 domain(size) constraint cons_name unique attribute2,….);

Ex:SQL> create table stu(sid number(2) constraint s unique);

Table created.

SQL> insert into stu values(&sid);Enter value for sid: 1old 1: insert into stu values(&sid)new 1: insert into stu values(1)

1 row created.

SQL> /Enter value for sid: 2old 1: insert into stu values(&sid)new 1: insert into stu values(2)

1 row created.

SQL> /Enter value for sid: 1

15

old 1: insert into stu values(&sid)new 1: insert into stu values(1)insert into stu values(1)*ERROR at line 1:ORA-00001: unique constraint (IT180.S) violated.2. b.PRIMARY KEY CONSTRAINT:

The primary key specification say the attributes Aj1,Aj2……Ajm form the primary key for the relation.The primary key attributes are required tobe null and unique; that is no tuple can be equal on all the primary key attributes.

CREATE A TABLE WITH PRIMARY KEY CONSTRAIN:

SYNTAX: Create table tablename(attribute1 datatype(size) constraint constraint _name primary key, attribute2 datatype(size),….);

EX: SQL> create table stud(rno number(3),name varchar2(15),constraint cm primary key(rno));

Table created.

SQL> desc stud; Name Null? Type ----------------------------------------- -------- ---------------------------- RNO NOT NULL NUMBER(3) NAME VARCHAR2(15).INSERT THE VALUES INTO THE PARENT TABLE:EX:SQL> insert into stud values(&rno,'&name');Enter value for rno: 1Enter value for name: sandeepold 1: insert into stud values(&rno,'&name')new 1: insert into stud values(1,'sandeep')

1 row created.

SQL> /Enter value for rno: 2Enter value for name: senthilold 1: insert into stud values(&rno,'&name')new 1: insert into stud values(2,'senthil')

1 row created.

SQL> /Enter value for rno: 3Enter value for name: sidu

16

old 1: insert into stud values(&rno,'&name')new 1: insert into stud values(3,'sidu')

1 row created.

SQL> /Enter value for rno: 4Enter value for name: ranjiold 1: insert into stud values(&rno,'&name')new 1: insert into stud values(4,'ranji')

1 row created.

SQL> select *from stud;

RNO NAME ---------- --------------- 1 sandeep 2 senthil 3 sidu 4 ranji

3. REFERENTIAL INTEGRITY CONSTRAINT: It is used to enforce relation between the tables.

FOREIGN KEY CONSTRAINTS: The foreign key declaration specific that for each account tuple the branch name specified in the tuple must exist in the branch relation. A relation schema say r1,may include among its attributes the primary key of another relation schema, say r2.This attribute is called a foreign key from r1,referncing r2.

1.CREATE CHILD TABLE:

SYNTAX: Create table tablename(attribute1 datatype(size), attribute datatype(size),….);EX:. SQL> create table studt(sid number(3),course varchar2(5),constraint ff foreign key(sid) references stud(rno));

Table created.

SQL> desc studt; Name Null? Type ----------------------------------------- -------- ---------------------------- SID NUMBER(3) COURSE VARCHAR2(5)

17

2.INSERT VALUES INTO CHILD TABLE:

SYNTAX: Insert into tablename values(‘&attribute1’,’&attribute2’);EX:SQL> insert into studt values(&sid,'&course');Enter value for sid: 1Enter value for course: itold 1: insert into studt values(&sid,'&course')new 1: insert into studt values(1,'it')

1 row created.

SQL> /Enter value for sid: 6Enter value for course: itold 1: insert into studt values(&sid,'&course')new 1: insert into studt values(6,'it')insert into studt values(6,'it')*ERROR at line 1:ORA-02291: integrity constraint (IT180.FF) violated - parent key not found

3.CREATE ANOTHER CHILD TABLE:

EX:. SQL> create table stude(age number(1); Table created.

4.ALTER THE TABLE AS WITH FOREIGN KEY:

EX:

SQL> alter table stude add constraint dp foreign key(age)references stud(sid)on delete cascade;Table altered.

5.INSERT THE VALUES INTO THE CHILD TABLE:

SQL> insert into stude values(&age);Enter value for age: 20old 1: insert into stude values(&age);new 1: insert into stude values(20);1 row created.

Select * from stude

age--------- 20

18

6. DELETE THE VALUES IN 1st CHILD TABLE:

SYNTAX: delete from tablename where condition;EX: SQL> delete from stud where sid=3;1 row deleted.

SQL> select * from stud;

RNO NAME ---------- --------------- 1 sandeep 2 senthil 4 ranji 7. DELETE THE VALUES IN 2nd CHILD TABLE:

SYNTAX: Ex: SQL> delete from stude where age=20; 1 row deleted.

JOINS

AIM:

To perform joins operations Cartesian join, Cross join, Equality join, Non-Equality join, Self join

and Outer join using sql commands.

1. Cartesian join:

It is also cross join or Cartesian product. It creates a join between tables by displaying each possible

record combination and it replicates each row from the first table with second table.

Syntax:

select * from fromlist;

Example:

SQL> select * from sailor,reserve;

SID SNAME RATING AGE SID BID DAY---------- ---------- ---------- ---------- ---------- ----- -------------- 22 ram 7 45 22 101 04-DEC-89 30 jack 8 22.5 22 101 04-DEC-89 53 victor 10 40 22 101 04-DEC-89 22 ram 7 45 53 103 05-FEB-86

19

30 jack 8 22.5 53 103 05-FEB-86 53 victor 10 40 53 103 05-FEB-86 22 ram 7 45 30 103 10-OCT-85 30 jack 8 22.5 30 103 10-OCT-85 53 victor 10 40 30 103 10-OCT-85 22 ram 7 45 22 104 22-DEC-84 30 jack 8 22.5 22 104 22-DEC-84 53 victor 10 40 22 104 22-DEC-84

12 rows selected.

2. Cross join

Syntax:

select selectlist from relation1 CROSS JOIN relation2

Example:

SQL> select s.sid,bid from sailor s cross join reserve;

SID BID---------- ---------- 22 101 30 101 53 101 22 103 30 103 53 103 22 103 30 103 53 103 22 104 30 104 53 104

12 rows selected.

3. EQUALITY JOIN:

It is also known as eqii join. It can be created by two methods using where clause.

a .Traditional Method:

Syntax:

select *from from list where <condition>

Example:

SQL> select * from reserve r,sailor s where s.sid=r.sid;

20

SID BID DAY SID SNAME RATING AGE---------- ---------- -------------- ---------- ---------- ------------- ---------- 22 101 04-DEC-89 22 ram 7 45 22 104 22-DEC-84 22 ram 7 45 30 103 10-OCT-85 30 jack 8 22.5 53 103 05-FEB-86 53 victor 10 40

4 rows selected.

b. Natural Join

Syntax:

select * selectlist from relation1 natural _join relation2

Example:

SQL> select * from sailor natural join reserve;

SID SNAME RATING AGE BID DAY---------- ---------- ------------- ----------- ---------- ---------------- 22 ram 7 45 101 04-DEC-89 22 ram 7 45 104 22-DEC-84 30 jack 8 22.5 03 10-OCT-85 53 victor 10 40 103 05-FEB-86

4 rows selected.

c. join…on

when the table to be joined in using join on and no need of where clause.

Syntax:

select * selectlist from relation1 join relation2 on <condition>

Example:

SQL> create table reser(sailorid,bid,dob) as (select * from reserve);

Table created.

SQL> select * from sailor s join reser r on sid=r.sailorid;

SID SNAME RATING AGE SAILORID BID DOB---------- -------------- ------------- ---------- -------------- ---------- --------- 22 ram 7 45 22 101 04-DEC-89 22 ram 7 45 22 104 22-DEC-84 30 jack 8 22.5 30 103 10-OCT-85 53 victor 10 40 53 03 05-FEB-86

4 rows selected.d. join…using:

21

Syntax:

select selectlist from relation1 join relation2 using(column1);

Example:

SQL> select sid,bid from sailor join reserve using (sid);

SID BID---------- ---------- 22 101 22 104 30 103 53 1034 rows selected.

4. NON-EQUALITY JOIN

join tables when there are no equivalent rows in the tables to be joined.

a. between…and:

Syntax:

select selectlist from fromlist where column_name between val1 to val2

Example:

SQL> select sname,bid from sailor,reserve where age between 20 and 25;

SNAME BID---------- ----------jack 101jack 103jack 103jack 104

4 rows selected.

b. join…on:

when the table to be joined in using join on and no need of where clause.

Syntax:

select selectlist from relation1 join relation2 on <condition>

Example:

SQL> select sname,bid from sailor join reserve on age between 35 and 44;

SNAME BID---------- ----------victor 101victor 103victor 103victor 104

22

4 rows selected.

5. SELF JOIN:

It creates a join between the same tables by displaying each possible record combinations and it replicates each row from the table1 and table2 on the Same table.

Syntax:

select selectlist from relation1 join relation2 on <condition>

Example:

SQL> select * from sailor s join sailor s1 on s.sid=s1.sid;

SID SNAME RATING AGE SID SNAME RATING AGE------ -------------- ------------ ---------- ---------- ------------ ---------- -------- 22 ram 7 45 22 ram 7 45 30 jack 8 22.5 30 jack 8 22.5 53 victor 10 40 53 victor 10 40

3 rows selected.

6. OUTER JOIN:

It includes records of a table in output where there is no matching record in the other table.

Traditional Method

Example:

SQL> select * from sailor s,reserve s1 where s.sid=s1.sid(+);

SID SNAME RATING AGE SID BID DAY------- ---------- ------------ ---------- ---------- ------- --------- 22 ram 7 45 22 101 04-DEC-89 22 ram 7 45 22 104 22-DEC-84 30 jack 8 22.5 30 103 10-OCT-85 53 victor 10 40 53 103 05-FEB-86

4 rows selected.

a. Left Outer join:

Syntax:

select selectlist from relation1 left outer join relation2 on<condition>

Example:

SQL> select * from sailor s left outer join reserve s1 on s.sid=s1.sid;

23

SID SNAME RATING AGE SID BID DAY---------- ------------ ------------ ---------- -------- -------- --------------- 22 ram 7 45 22 101 04-DEC-89 53 victor 10 40 53 103 05-FEB-86 30 jack 8 22.5 30 103 10-OCT-85 22 ram 7 45 22 104 22-DEC-84

4 rows selected.

b.Right Outer join:

Syntax:

select selectlist from relation1 right outer join relation2 on<condition>

Example:

SQL> select * from sailor s right outer join reserve s1 on s.sid=s1.sid;

SID SNAME RATING AGE SID BID DAY---------- ------------- ------------- ---------- ---------- -------- -------------- 22 ram 7 45 22 104 22-DEC-84 22 ram 7 45 22 101 04-DEC-89 30 jack 8 22.5 30 103 10-OCT-85 53 victor 10 40 53 103 05-FEB-86

4 rows selected.

c. Full outer join:

Syntax:

select selectlist from relation1 full outer join relation2 on<condition>

Example:

SQL> select * from sailor s full outer join reserve s1 on s.sid=s1.sid;

SID SNAME RATING AGE SID BID DAY--------- -------------- ------------- -------- ------- -------- -------------- 22 ram 7 45 22 101 04-DEC-89 53 victor 10 40 53 103 05-FEB-86 30 jack 8 22.5 30 103 10-OCT-85 22 ram 7 45 22 104 22-DEC-84

4 rows selected.

24

VIEWS

AIM:To perform a views operation like as insert,delete,update using sql command.

DEFINITION:The view mechanism is a very powerful feature of a relational DBMS. It is used for enhancing

acess control,it provides logical data Independence. A view technology is a table that deriver from another table.

CREATION OF VIEW:

DESCRIPTION:It is used to create a view. A view is a table whose rows not explicitly stored in the database.

Syntax:Create view viewname as select select_list from from_table qualification

Example:Sql>create view qqq as select * from sailor natural full outerjoin reserve;View created.

Sql>select * from qqq;

SID SNAME RATING AGE BID DAY------ --------------------- ------------ ------ ----- -----------------101 amar 12 20 2 10-FEB-09102 ajay 13 21 1 11-FEB-09103 bala 15 25 4 12-feb-09104 rama 13 18 6 12-feb-09106 ashok 13 17105 gowtham 12 21107 guru 15 24

7 rows selected.

Operation on views:The view operations are

1. Insert view2. Select view3. Update view4. Drop view5. Group by6.

1.Insert view:Insert into value for the created view table.

Syntax:Insert into view_name values(col1,col2,…,coln)

25

Example:Sql>insert into qqq values(101,’amar’,12,20,2,’10-feb-09’);1 row created

2. select view:Select the view table and display the content.

Syntax:Select * from view_name

Example:Sql>select * from qqq;

SID SNAME RATING AGE BID DAY------ -------------- ------------ ------ ----- ------------------101 amar 12 20 2 10-feb-09102 ajay 13 21 1 11-feb-09103 bala 15 25 4 12-feb-09104 rama 13 18 6 12-feb-09106 ashok 13 17105 gowtham 12 21107 guru 15 24

7 rows selected

3.Update view:Update the particular value in the particular view table

Syntax:Update view_name set colomn_name=(value) where qualification

Example:Sql>update qqq setage=23 where sname=’amar’;1 row updated.

Sql>select * from qqq;SID SNAME RATING AGE BID DAY------ -------------- ------------ ------ ----- ------------------101 amar 12 23 2 10-feb-0102 ajay 13 21 1 11-feb-09103 bala 15 25 4 12-feb-09104 rama 13 18 6 12-feb-09106 ashok 13 17105 gowtham 12 21107 guru 15 24

7 rows selected

4. Drop view:It is used to delete the view table and contents.

Syntax:Drop view view_name

26

Example:Sql>drop view qqq;View dropped.

5. using group by:It display the attributes by group the colomn based on the group functions like Sum(), Avg(),

Max(), Min(), Count().Syntax:

Select select_list from from_table group by qualificationExample:Sql>select rating,avg(age) from sailor group by rating;

RATING AVG(AGE)------------ ----------------

12 20.513 18.614 24.5

3 rows selected.

SET OPERATIONS

AIM:

To perform set operations Union, Intersection and Except using SQL commands.

1 .UNION

It produces a relation that includes all the tuples both in R1 & R2.

Syntax:

R1 U R2

Example:

SQL> select sname from sailor s,boat b,reserve r where s.sid=r.sid and r.bid=b.bid and b.color='pink'

union select sname from sailor s2,boat b2,reserve r2 where s2.sid=r2.sid and r2.bid=b2.bid and

b2.color='blue';

SNAME----------victorjackram

3 rows selected.

27

2. INTERSECT:

It produces a relation that includes all the tuples both R1 and R2

Syntax:

R1 ∩ R2

Example:

SQL> select sname from sailor s,boat b,reserve r where s.sid=r.sid and r.bid=b.bid and b.color='pink'

intersect select sname from sailor s2,boat b2,reserve r2 where s2.sid=r2.sid and r2.bid=b2.bid and

b2.color='blue';

SNAME----------ram

1 rows selected.

3.EXCEPT:

It will display tuples in R1 that are not in R2

Syntax:

R1 – R2

Example:

SQL> select sname from sailor s,boat b,reserve r where s.sid=r.sid and r.bid=b.bid and b.color='pink'

minus select sname from sailor s2,boat b2,reserve r2 where s2.sid=r2.sid and r2.bid=b2.bid and

b2.color='blue';

SNAME----------victorjack

2 rows selected.

28

LOGICAL OPERATIONS

AIM:

To perform logical operations like AND,OR and NOT using SQL commands.

1. AND operation:

It checks the condition and display the command if it satisfies both the condition.

Syntax:

select selectlist from from list Where qualification1 AND qualification2.

Example:

SQL> select sname from sailor s,boat b,reserve r 2 where s.sid=r.sid and r.bid=b.bid and b.color='pink';

SNAME----------ramjackvictor

3 rows selected.

2. OR-operation:

It checks both the condition atleast if it satifies one and display the Select terms in both fields.

Syntax:

select selectlist from from list Where qualification1 OR qualification2.

Example:

SQL> select sname from sailor s,reserve r where s.sid=r.sid and r.bid=101 or r.bid=106;

SNAME----------ram

1 rows selected.

3. NOT operation:

It checks the condition that must satisfy only one condition and Display the term.

Syntax: select selectlist from from list

Where qualification1 NOT qualification2.

29

Example:

SQL> select sname from sailor s,reserve r,boat b where s.sid=r.sid and r.bid=b.bid and b.color='pink' and

b.color!='blue';

SNAME----------ramjackvictor

3 rows selected

AGGREGATE OPERATORS

Aim: To perform operations of some aggregate operators like COUNT,AVG, SUM, MIN, MAX using SQL commads.

1.Count:

Syntax: select COUNT( column name) from relation1.

Explanation: It displays the no of rows in that particular column

Example:SQL> select count(age) from sailors;

Output:

COUNT(AGE)---------- 9

2. Avg:

Syntax: select AVG( column name) from relation1.

Explanation: It displays the average value of that particular column.

Example:SQL> select avg(age) from sailors;

Output:

AVG(AGE)----------37.1111111

30

3. Sum:

Syntax: select SUM( column name) from relation1.

Explanation: It displays sum of that column.

Example:SQL> select sum(rating) from sailors;

OUTPUT:

SUM(RATING)----------- 57

4. Min:

Syntax: select MIN(column name)from relation2.

Explanation: It displays a row that has the minimum value of that particular column.

Example:

SQL> select min(age) from sailors;

OUTPUT:

MIN(AGE)---------- 16

5 . Max:

Syntax: select MAX(column name)from relation2.

Explanation: It displays a row that has the maximum value of that particular column

Example:

SQL> select max(rating) from sailors;

OUTPUT:

MAX(RATING)----------- 10

31

TRIGGERS

AIM: To start certain tasks automatically when certain conditions are met using trigger in

oracle.

DEFINITION: A trigger is a statement that the system executes automatically as a side effect of a

modification to the database.

PROCEDURE: 1.Create a table item_master with the fields item_id, item_desc

and bal_stock.

2. Create a trigger named as trig1 to perform its action whenever a

value is inserted into the table item_master.

3. Use the “dbms_output.put_line” statement to display the trigger.

4. End the trig1.

5. Now the trig1 is created.

6. Create trigger named as trig2 to perform its action whenever the

values are inserted in to the table item_master by giving a

particular condition.

7. Use the if statement to check the values in the table item_master.

8. Use the error statement to display the error message.

9. End the trigger trig2.

10. Check the working of triggers trig1 and trig2 by inserting the

values

into the table item_master.

11. Create a trigger named as trig3 to perform its action whenevrer

changes are made to the table item_master.

12. If the bal_stock value is less than 10 then the error message is

displayed using the error statement.

13. Create a trigger named as trig5 to perform its action whenever a

value is deleted from the table item_master.

14. If the value of old_item is i005 then show the error message.

15. Check the working of trigger trig5 by deleting the value from the

table item_master.

16. Stop the program.

32

OUTPUT:

1. Create a trigger that will display user has created a row after every insert on item_master table.

SQL> desc item_master

SQL> create or replace trigger tri1 after insert on item for each row declare begin dbms_output.put_line('***USER HAS INSERTD A ROW***'); end;

Trigger created.

SQL> set serveroutput on;SQL> insert into item_master values('05','pencil',10);

***USER HAS INSERTD A ROW***

1 row(s) inserted.

2. Create a trigger that will not insert an item into the item_master. If the item_id is ‘i001’ or the item_desc is ‘laptop’.

SQL> create or replace trigger tri1 after insert on item for each row declare begin if(:new.item_id='i001' or :new.item_desc='laptop')then raise_application_error('-20001','YOU CANT INSERT THIS VALUE'); end if; end;

Trigger created.

SQL> insert into item_master values('i001','laptop',50000);

ORA-20001: ***YOU CANT INSERT THIS VALUE***ORA-06512: at "trig.TRIG2", line 4ORA-04088: error during execution of trigger 'trig.TRIG2'

SQL> insert into item_master values(‘i006’,’cinthol’,222)33

***USER HAS CREATED A ROW***1 row created

3. Create a trigger that will not the item_master if the bal_stock is <10.

SQL> ed Wrote file afiedt.buf

SQL> create or replace trigger tri3 before update on item for each row declare begin

if(:new.bal_stock<10) then raise_application_error('-20002','YOU CANT update THIS VALUE'); end if; end;

Trigger created.

SQL> update item_master set bal_stock = 5 where item_desc=’hamam’

SQL> update item_master set bal_stock-5 where item_desc=’hamam’

ORA-20002: YOU CANT UPDATE THIS VALUEORA-06512: at "trigger.TRI3", line 4ORA-04088: error during execution of trigger 'tirgger.TRI3'

SQL> update item_master set bal_stock=11 where item_desc=’hamam’

1 row updated

4. Create a trigger that will not delete an item if item_id is ‘1005’.

SQL> ed Wrote file afiedt.buf

create or replace trigger tri4 before delete on item for each rowwhen(old.item_id='01')declarebeginraise_application_error('-20003','YOU CANT DELETE THIS VALUE');end;

Trigger created.

SQL> delete from item_ where item_id='01'; SQL> delete from item_master where item_id=’i006’

ORA-20003: YOU CANT DELETE THIS VALUEORA-06512: at " trigger.TRI4", line 3ORA-04088: error during execution of trigger 'trigger.TRI4'

SQL> delete from item_master where item_id=’i006’

34

1 row deleted

DROPING A TRIGGER:

SQL> drop trigger trig4

Trigger dropped

SQL> select * from item_master

Embedded SQL Projects Using VB

Student evaluation system.

Computerized quiz.

Computer center reservation of computing machines.

Income tax calculation.

Pay roll system.

Election processing system.

Library Management System

Payroll Processing System

Banking System

ITEM_ID ITEM_DESC BAL_STOCK1 hamam 169

2 colgate 484

3 powder 260

4 lizol 12

4 lizol 12

35