0% found this document useful (0 votes)
9 views

Database Management lab manual

The document is a lab manual for a Database Management Systems course, detailing objectives, course outcomes, and a list of experiments. It covers Data Definition Language (DDL), Data Manipulation Language (DML), and Data Query Language (DQL) commands, providing syntax and examples for each. The manual aims to equip students with practical skills in database modeling, design, and implementation using SQL.

Uploaded by

darshanjain1905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Database Management lab manual

The document is a lab manual for a Database Management Systems course, detailing objectives, course outcomes, and a list of experiments. It covers Data Definition Language (DDL), Data Manipulation Language (DML), and Data Query Language (DQL) commands, providing syntax and examples for each. The manual aims to equip students with practical skills in database modeling, design, and implementation using SQL.

Uploaded by

darshanjain1905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

LAB MANUAL

SUBJECT NAME:
DATABASE MANAGEMENT SYSTEMS LAB

Prepared By:
Sameep N.Sinha
DBMS LABORATORY L T P C
Total Contact Hours - 30 0 0 3 2
Prerequisite –Database Management System
Lab Manual Designed by – Dept. of Computer Science and Engineering
OBJECTIVES: The main objective is students gain knowledge about databases for storing the data
and to share the data among different kinds of users for their business operations.
COURSE OUTCOMES (COs)
CO1 Develop database modeling for a problem.
CO2 Design a database using normalization.
CO3 Implement a data base query language.
CO4 Develop GUI using front end tool.
CO5 Develop a connection between frontend and database.
CO6 Implement a Data Manipulation Language.
LIST OF EXPERIMENTS

CONTENT

S.NO NAME OF THE EXPERIMENT

1 Data definition languages (ddl) commands of base tables and views

2 Data manipulation language (dml) of base tables and views

3 Data query language (dql)

4 Using Constraints in SQL

5 Forms- triggers- menu design.


EX.NO:1
DATA DEFINITION LANGUAGES (DDL) COMMANDS Of Base Tables and Views

A Data Definition Language (DDL) statement is used to define the database


structure or schema.

Aim:

To study and execute the DDL commands in RDBMS.

DDL commands:

• CREATE
• ALTER
• DROP
• RENAME
• TRUNCATE

SYNTAX’S OF COMMANDS
CREATE TABLE:
To make a new database, table, index, or stored query. A create statement in SQL
creates an object inside of a relational database management system (RDBMS).

CREATE TABLE <table_name>


(
Column_name1 data_type ([size]),
Column_name2 data_type ([size]),
.
.
.
Column_name-n data_type ([size])
);

1
ALTER A TABLE:
To modify an existing database object. Alter the structure of the database.
To add a column in a table
ALTER TABLE table_name ADD column_name datatype;
To delete a column in a table
ALTER TABLE table_name DROP column column_name;

DROP TABLE:
Delete Objects from the Database

DROP TABLE table_name;

TRUNCATE TABLE:
Remove all records from a table, including all spaces allocated for the records are
removed.

TRUNCATE TABLE table_name;

EXERCISE:
Create Table
SQL> create table employee
2(
3 empid varchar(10) primary key,
4 empname varchar2(20) not null,
5 gender varchar2(7) not null,
6 age number(3) not null,
7 dept varchar2(15) not null,
8 dob date not null,

9 doj date not null


10);
Table created.

2
SQL> create table salary
2(
3 empid varchar(10) references employee(empid),
4 salary number(10) not null,
5 dept varchar(15) not null,

6 branch varchar2(20) not null


7 );
Table created.

SQL> create table branchtable


2(
3 branch varchar2(20) not null,
4 city varchar2(20) not null
5 );
Table created.

DESCRIBE TABLE
SQL> desc employee;
Name Null? Type

EMPID NOT NULL VARCHAR2(10)


EMPNAME NOT NULL VARCHAR2(20)

GENDER NOT NULL VARCHAR2(7)


AGE NOT NULL NUMBER(3)
DEPT NOT NULL VARCHAR2(15)
DOB NOT NULL DATE
DOJ NOT NULL DATE

3
SQL> desc salary;
Name Null? Type

EMPID VARCHAR2 (10)

SALARY NOT NULL NUMBER (10)

DEPT NOT NULL VARCHAR2 (15)


BRANCH NOT NULL VARCHAR2 (20)

SQL> desc branchtable;


Name Null? Type

BRANCH NOT NULL VARCHAR2 (20)

CITY NOT NULL VARCHAR2 (20)

ALTER TABLE

I. ADD:
SQL> alter table employee add(designation varchar2(15));
Table altered.

SQL> alter table salary add(constraint nithi unique(empid));


Table altered.

II. MODIFY
SQL> alter table employee modify (designation varchar2(20));
Table altered.

4
RENAME TABLE
SQL> create table emp

2(
3 empid varchar2(10),

4 empname varchar2(20),
5 age number(3),
6 sex char
7 );
Table created.

SQL> rename emp to empl;


Table renamed.

SQL> desc empl;


Name Null? Type

EMPID VARCHAR2(10)
EMPNAME VARCHAR2(20)
AGE NUMBER(3)
SEX CHAR(1)

SQL> desc emp;

ERROR:

ORA-04043: object emp does not exist


Table altered.

TRUNCATE TABLE DATA


SQL> insert into emp values(&no,'&name','&dept',&age,'&sex');
Enter value for no: 1
Enter value for name: arun

5
Enter value for dept: it
Enter value for age: 22
Enter value for sex: m
old 1: insert into emp values(&no,'&name','&dept',&age,'&sex')

new 1: insert into emp values(1,'arun','it',22,'m')


1 row created.

SQL> insert into emp values(&no,'&name','&dept',&age,'&sex');


Enter value for no: 2
Enter value for name: bala
Enter value for dept: service
Enter value for age: 26
Enter value for sex: m
old 1: insert into emp values(&no,'&name','&dept',&age,'&sex')
new 1: insert into emp values(2,'bala','service',26,'m')
1 row created.

SQL> insert into emp values(&no,'&name','&dept',&age,'&sex');


Enter value for no: 3
Enter value for name: chitra
Enter value for dept: sales
Enter value for age: 25
Enter value for sex: f
old 1: insert into emp values(&no,'&name','&dept',&age,'&sex')
new 1: insert into emp values(3,'chitra','sales',25,'f')
1 row created.
SQL> select * from emp;
EMPID EMPNAME DEPT AGE SEX

1 arun it 22 m

6
2 bala service 26 m
3 chitra sales 25 f

SQL> commit;
Commit complete.

SQL> truncate table emp;


Table truncated.
SQL> select * from emp;
no rows selected
SQL> commit;

Commit complete.

DROP TABLE

SQL> drop table empl;


Table dropped.
SQL> desc empl;
ERROR:
ORA-04043: object empl does not exist

RESULT:

Thus executed the DDL commands in RDBMS

7
EX.NO:2
DATA MANIPULATION LANGUAGE (DML) OF BASE TABLES AND VIEWS

Data manipulation language allows the users to query and manipulate data in existing
schema in object. It allows following data to insert, delete, update and recovery data in
schema object.

Aim:
To study DML commands in RDBMS.

DML COMMANDS:
❖ INSERT
❖ UPDATE
❖ DELETE
❖ SELECT

SYNTAX OF COMMANDS
INSERT:
Values can be inserted into table using insert commands. There are two types of insert
commands. They are multiple value insert commands (using ‘&’ symbol) single value insert
command (without using ‘&’symbol)
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3,…..);
(OR)
INSERT INTO table_name (column1, column2, column3,….) VALUES
(value1,value2,value3,…..);

8
UPDATE:
This allows the user to update the particular column value using the where clause
condition.
Syntax:
UPDATE <table_name> SET <col1=value> WHERE <column=value>;

DELETE:
This allows you to delete the particular column values using where clause condition.

Syntax:

DELETE FROM <table_name> WHERE <condition>;

9
EX.NO:3
DATA QUERY LANGUAGE (DQL) OF BASE TABLES AND VIEWS

QUERY:

Query is a statement in the DML that request the retrieval of data from database.

❖ The portion of the DML used in a Query is called Query language. The SELECT
statement is used to query a database

Aim:
To study DQL commands in RDBMS.

SELECT:

The select statement is used to query a database .This statement is used to retrieve the
information from the database. The SELECT statement can be used in many ways. They are:
1. Selecting some columns :
To select specified number of columns from the table the

Following command is used.

Syntax:

SELECT column_name FROM table_name;

2. Query All Columns:

To select all columns from the table * is used instead of column names.

Syntax:

SELECT * FROM table_name;

3. Select using DISTINCT:

The DISTINCT keyword is used to return only different values (i.e. ) this
command does not select the duplicate values from the table.
Syntax:

SELECT DISTINCT column name(s) FROM table_name;

10
4. Select using IN:

If you want to get the rows which contain certain values, the best way to do it
is to use the IN conditional expression.

Syntax:

SELECT column name(s) FROM table_name WHERE


Column name IN (value1, value2,……,value-n);

5. Select using BETWEEN:

BETWEEN can be used to get those items that fall within a range.

Syntax:

SELECT column name FROM table_name WHERE

Column name BETWEEN value1 AND value2;

6. Renaming:

The select statement can be used to rename either a column or the entire
table.

Syntax:

Renaming a column:

SELECT column name AS new name FROM table_name;

Renaming a table:

SELECT column name FROM table_name AS newname;

7. Sorting:
The select statement with the order by Clause is used to sort the contents

Table either in ascending or descending order.

Syntax:

SELECT column name FROM table_name WHERE

Condition ORDER BY column name ASC/DESC;

11
8. To select by matching some patterns:

The select statement along with like clause I is used to match strings. The like
condition is used to specify a search pattern in a column.

Syntax:

SELECT column name FROM table_name WHERE Column name LIKE “% or-“;

%: Matches any sub string.

- : Matches a single character.

9. SELECT INTO statement:

The SELECT INTO statement is most often used to create backup copies of
tables or for archiving records.

Syntax:

SELECT Column_name(s) INTO variable_name(s) FROM table_name

WHERE condition.

10. To Select NULL values:

We can use the SELECT statement to select the ‘null’ values also.
For retrieving roes where some of the columns have been defined as NULLs there is a special
comparison operator of the form IS [NOT]NULL.
Syntax:

SELECT column name FROM table_name WHERE Column name IS NULL;

11. Select using AND, OR, NOT:

We can combine one or more conditions in a SELECT statement using the


logical operators AND, OR, NOT.

Syntax:

SELECT column name FROM table_name WHERE Condition1 LOGICAL


OPERATOR condition2;

12
EXERCISE:
INSERT COMMAND

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: it9001
Enter value for empname: arunkumar
Enter value for gender: male
Enter value for age: 22
Enter value for dept: it

Enter value for dob: 12-jan-1988


Enter value for doj: 23-oct-2006
Enter value for desig: manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('it9001','arunkumar','male',22,'it','12-jan-1988','23-oct-
2006'
1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: it9001

Enter value for empname: arunkumar


Enter value for gender: male
Enter value for age: 22
Enter value for dept: it
Enter value for dob: 12-jan-1988
Enter value for doj: 23-oct-2006
Enter value for desig: manager

13
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('it9001','arunkumar','male',22,'it','12-jan-1988','23-oct-
2006'
1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: it9002

Enter value for empname: balakrishnan


Enter value for gender: male
Enter value for age: 27
Enter value for dept: it
Enter value for dob: 27-mar-1983
Enter value for doj: 02-dec-2008
Enter value for desig: coordinator
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('it9002','balakrishnan','male',27,'it','27-mar-1983','02-
dec-20
1 row created.

SQL> insert into employee

values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: acc9001

Enter value for empname: kannan


Enter value for gender: male
Enter value for age: 35
Enter value for dept: accounts
Enter value for dob: 28-dec-1975

14
Enter value for doj: 01-jan-1995
Enter value for desig: manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi

new 1: insert into employee values('acc9001','kannan','male',35,'accounts','28-dec-1975','01-


jan-1

1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: acc9002
Enter value for empname: magudeshwaran
Enter value for gender: male
Enter value for age: 27

Enter value for dept: accounts


Enter value for dob: 25-aug-1983
Enter value for doj: 12-apr-2000
Enter value for desig: asst manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('acc9002','magudeshwaran','male',27,'accounts','25-aug-
1983','1
1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: ser9001
Enter value for empname: jagadheesh
Enter value for gender: male
Enter value for age: 33

15
Enter value for dept: service
Enter value for dob: 31-mar-1877
Enter value for doj: 3-jun-1999
Enter value for desig: manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('ser9001','jagadheesh','male',33,'service','31-mar-
1877','3-jun
1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: ser9006
Enter value for empname: muruganandam

Enter value for gender: male

Enter value for age: 35


Enter value for dept: service
Enter value for dob: 09-aug-1975
Enter value for doj: 02-mar-2000
Enter value for desig: painter

old 1: insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('ser9006','muruganandam','male',35,'service','09-aug-
1975','02-
1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
SQL> /
Enter value for empid: sal9001

16
Enter value for empname: suresh
Enter value for gender: male
Enter value for age: 40
Enter value for dept: sales

Enter value for dob: 12-jul-1970


Enter value for doj: 01-apr-1996
Enter value for desig: manager
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('sal9001','suresh','male',40,'sales','12-jul-1970','01-apr-
1996
1 row created.

SQL> insert into employee


values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: sal9006

Enter value for empname: sharmila


Enter value for gender: female
Enter value for age: 27
Enter value for dept: sales

Enter value for dob: 12-jan-1983


Enter value for doj: 09-aug-2007
Enter value for desig: executive
old 1: insert into employee
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desi
new 1: insert into employee values('sal9006','sharmila','female',27,'sales','12-jan-1983','09-
aug-
1 row created.

17
SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);
Enter value for empid: it9002
Enter value for salary: 18000
Enter value for dept: it
Enter value for branch: abt maruthi

old 1: insert into salary values('&empid',&salary,'&dept','&branch')


new 1: insert into salary values('it9002',18000,'it','abt maruthi')
1 row created.

SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);


Enter value for empid: acc9001
Enter value for salary: 35000
Enter value for dept: accounts
Enter value for branch: cars india
old 1: insert into salary values('&empid',&salary,'&dept','&branch')
new 1: insert into salary values('acc9001',35000,'accounts','cars india')
1 row created.

SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);


Enter value for empid: acc9002
Enter value for salary: 26000
Enter value for dept: accounts
Enter value for branch: cars india

old 1: insert into salary values('&empid',&salary,'&dept','&branch')


new 1: insert into salary values('acc9002',26000,'accounts','cars india')
1 row created.

SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);


Enter value for empid: ser9001
Enter value for salary: 35000

18
Enter value for dept: service
Enter value for branch: chennai cars
old 1: insert into salary values('&empid',&salary,'&dept','&branch')
new 1: insert into salary values('ser9001',35000,'service','chennai cars')
1 row created.

SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);


Enter value for empid: ser9006
Enter value for salary: 12000
Enter value for dept: service
Enter value for branch: greenland cars
old 1: insert into salary values('&empid',&salary,'&dept','&branch')
new 1: insert into salary values('ser9006',12000,'service','greenland cars')
1 row created.

SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);


Enter value for empid: sal9001
Enter value for salary: 40000
Enter value for dept: sales
Enter value for branch: abt maruthi

old 1: insert into salary values('&empid',&salary,'&dept','&branch')


new 1: insert into salary values('sal9001',40000,'sales','abt maruthi')
1 row created.

SQL> insert into salary values(‘&empid’,&salary,’&dept’,’&branch’);


Enter value for empid: sal9006
Enter value for salary: 17000
Enter value for dept: sales

Enter value for branch: abt maruthi


old 1: insert into salary values('&empid',&salary,'&dept','&branch')

19
new 1: insert into salary values('sal9006',17000,'sales ','abt maruthi')
1 row created.

SQL> select * from salary;


EMPID SALARY DEPT BRANCH

it9001 35000 it abt maruthi


it9002 18000 it abt maruthi
acc9001 35000 accounts cars india
acc9002 26000 accounts cars india

ser9001 35000 service chennai cars


ser9006 12000 service greenland cars
sal9001 40000 sales abt maruthi
sal9006 17000 sales abt maruthi

8 rows selected.

SQL> select * from employee;


EMPID EMPNAME GENDER AGE DEPT DOB

DOJ DESIGNATION

it9001 arunkumar male 22 it 12-JAN-88


23-OCT-06 manager

it9002 balakrishnan male 27 it 27-MAR-83


02-DEC-08 coordinator
acc9001 kannan male 35 accounts 28-DEC-75
01-JAN-95 manager

EMPID EMPNAME GENDER AGE DEPT DOB

20
DOJ DESIGNATION

acc9002 magudeshwaran male 27 accounts 25-AUG-83

12-APR-00 asst manager

ser9001 jagadheesh male 33 service 31-MAR-77


03-JUN-99 manager
ser9006 muruganandam male 35 service 09-AUG-75
02-MAR-00 painter

EMPID EMPNAME GENDER AGE DEPT DOB

DOJ DESIGNATION

sal9001 suresh male 40 sales 12-JUL-70


01-APR-96 manager

sal9006 sharmila female 27 sales 12-JAN-83


09-AUG-07 executive
8 rows selected.
SQL> insert into branchtable values('&branch','&city');
Enter value for branch: abt maruthi
Enter value for city: chennai

old 1: insert into branchtable values('&branch','&city')

new 1: insert into branchtable values('abt maruthi','chennai')


1 row created.

SQL> select * from salary;


EMPID SALARY DEPT BRANCH

21
it9001 35000 it abt maruthi
it9002 18000 it abt maruthi
acc9001 35000 accounts cars india
acc9002 26000 accounts cars india
ser9001 35000 service chennai cars
ser9006 12000 service greenland cars
sal9001 40000 sales abt maruthi
sal9006 17000 sales abt maruthi

8 rows selected.

SQL> insert into branchtable values('&branch','&city');


Enter value for branch: cars india
Enter value for city: vellore

old 1: insert into branchtable values('&branch','&city')


new 1: insert into branchtable values('cars india','vellore')
1 row created.

SQL> insert into branchtable values('&branch','&city');


Enter value for branch: chennai cars
Enter value for city: thambaram
old 1: insert into branchtable values('&branch','&city')
new 1: insert into branchtable values('chennai cars','thambaram')
1 row created.

SQL> insert into branchtable values('&branch','&city');


Enter value for branch: greenland cars
Enter value for city: kanchipuram

old 1: insert into branchtable values('&branch','&city')


new 1: insert into branchtable values('greenland cars','kanchipuram')

22
1 row created.

SQL> select * from branchtable;


BRANCH CITY

abt maruthi chennai


cars india vellore
chennai cars thambaram
greenland cars kanchipuram

UPDATE COMMAND
SQL> update employee set empname = 'arunprasanth' where empid='it9001';
1 row updated.

SQL> update employee set designation='&designation' where empname='&empname';


Enter value for designation: supervisor
Enter value for empname: muruganandam
old 1: update employee set designation='&designation' where empname='&empname'
new 1: update employee set designation='supervisor' where empname='muruganandam'
1 row updated.

SQL> select empname,designation from employee;

EMPNAME DESIGNATION

arunprasanth manager
balakrishnan coordinator
kannan manager
magudeshwaran asst manager
jagadheesh manager

23
muruganandam supervisor
suresh manager

sharmila executive
8 rows selected.

SELECT COMMAND
To retrieve particular column
SQL> select empname from emp;
EMPNAME

arun
bala
bakyaraj
chitra

To retrieve all columns


SQL> select * from emp;

EMPID EMPNAME DEPT AGE S

1 arun it 22 m
2 bala accounts 26 m

3 bakyaraj stores 30 m
4 chitra sales 24 f

DELETE COMMAND
To delete particular record
SQL> delete emp where empid=1;
1 row deleted.

24
SQL> select * from emp;
EMPID EMPNAME DEPT AGE S

2 bala accounts 26 m
3 bakyaraj stores 30 m
4 chitra sales 24 f

To delete all records


SQL> delete from emp;
3 rows deleted.

SQL> create table student (idno number, name varchar(10),branch varchar(4));

Table created.

SQL> desc student;

NAME NULL? TYPE

IDNO NUMBER

NAME VARCHAR2(10)

BRANCH VARCHAR2(4)

SQL> alter table student add degree varchar(10);

Table altered.

SQL> desc student;

NAME NULL? TYPE

IDNO NUMBER
25
NAME VARCHAR2 (10)

BRANCH VARCHAR2 (4)

DEGREE VARCHAR2 (10)

SQL> alter table student modify degree

varchar(6); Table altered.

SQL> desc student;

NAME NULL? TYPE

IDNO NUMBER

NAME VARCHAR2 (10)

BRANCH VARCHAR2 (4)

DEGREE VARCHAR2 (6)

SQL> insert into student (name, degree, branch, idno) values('ASHOK','BE','CSE',01);

1 row created.

SQL> insert into student values(02,'BHAVANA','CSE','BE');

1 row created.

SQL> insert into student values(&idno, &name, &branch, &degree);

Enter value for idno: 03

Enter value for name: 'CAVIN'

Enter value for branch: 'CSE'

Enter value for degree: 'BE'

old 1: insert into student values(&idno,&name,&branch,&degree)

new 1: insert into student values(03,'CAVIN','CSE','BE')

1 row created.

26
Enter value for idno: 04

Enter value for name: 'DANNY'

Enter value for branch: 'IT'

Enter value for degree: 'BE'

old 1: insert into student values(&idno,&name,&branch,&degree)

new 1: insert into student values(04,'DANNY','IT','BE')

1 row created.

SQL> /

Enter value for idno: 05

Enter value for name: 'HARRY'

Enter value for branch: 'IT'

Enter value for degree: 'BE'

old 1: insert into student values(&idno,&name,&branch,&degree)

new 1: insert into student values(05,'HARRY','IT','BE')

1 row created.

SQL> select * from student;

IDNO NAME BRAN DEGREE

1 ASHOK CSE BE

2 BHAVANA CSE BE

3 CAVIN CSE BE

4 DANNY IT BE

5 HARRY IT BE

SQL> update student set degree='B.TECH' where branch='IT';

2 rows updated.

27
SQL> select * from student;

IDNO NAME BRAN DEGREE

1 ASHOK CSE BE

2 BHAVANA CSE BE

3 CAVIN CSE BE

4 DANNY IT B.TECH

5 HARRY IT B.TECH

SQL> delete from student where idno=5;

1 row deleted.

28
EX.NO:4
USING CONTRAINTS IN SQL

Constraints are the rules that we can apply on the type of data in a table. That is, we can specify the
limit on the type of data that can be stored in a particular column in a table using constraints.

The available constraints in SQL are:

NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a column is
specified as NOT NULL then we will not be able to store null in this particular column any more.
UNIQUE: This constraint when specified with a column, tells that all the values in the column must be
unique. That is, the values in any row of a column must not be repeated.
PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table. And this
constraint is used to specify a field in a table as primary key.
FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a another table.
And this constraint is used to specify a field as Foreign key.
CHECK: This constraint helps to validate the values of a column to meet a particular condition. That
is, it helps to ensure that the value stored in a column meets a specific condition.
DEFAULT: This constraint specifies a default value for the column when no value is specified by the
user.

Aim:
To apply constraints on RDBMS in SQL.

CREATING TABLES WITH CONSTRAINTS:

NOT NULL

SQL> select * from student;

IDNO NAME BRAN DEGREE

1 ASHOK CSE BE

2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT B.TEC
H

SQL> create table staff


29
(

idno number (4) not null,name

varchar(10),branch varchar(6)

); Table created.

SQL> desc staff;

NAME NULL? TYPE

IDNO NOT NULL NUMBER(4)

NAME VARCHAR2(10)

BRANCH VARCHAR2(6)

SQL> insert into staff values (&idno, &name, &branch);

Enter value for idno: 1

Enter value for name: 'ABILASH'

Enter value for branch: 'CSE'

old 1: insert into staff values(&idno, &name, &branch)

new 1: insert into staff values(1,'ABILASH','CSE')

1 row created.

SQL> /

Enter value for idno: 2

Enter value for name: 'ANTON'

Enter value for branch: 'CSE'

old 1: insert into staff values(&idno, &name, &branch)

new 1: insert into staff values(2,'ANTON','CSE')

1 row created.
SQL> /

Enter value for idno:


30
Enter value for name: 'BENNY'

Enter value for branch: 'IT'

old 1: insert into staff values(&idno,&name,&branch)

new 1: insert into staff values(,'BENNY','IT')

insert into staff values(,'BENNY','IT') *

ERROR at line 1:

ORA-00936: missing expression

UNIQUE

SQL> create table employee

rollno number unique,

name varchar(10),

salary number

);

Table created.

SQL> desc employee;

NAME NULL? TYPE

ROLLNO NUMBER

NAME VARCHAR2(10)

SALARY NUMBER

SQL> insert into employee values(&rollno,&name,&salary);

Enter value for rollno: 1


31
Enter value for name: 'anton'

Enter value for salary: 10290

old 1: insert into employee values(&rollno,&name,&salary)

new 1: insert into employee values(1,'anton',10290)

1 row created.

SQL> /

Enter value for rollno: 2

Enter value for name: 'dharun'

Enter value for salary: 23322

old 1: insert into employee values(&rollno,&name,&salary)

new 1: insert into employee values(2,'dharun',23322)

1 row created.

SQL> /

Enter value for rollno: 1

Enter value for name: 'aaron'

Enter value for salary: 32212

old 1: insert into employee values(&rollno,&name,&salary)

new 1: insert into employee values(1,'aaron',32212)

insert into employee values(1,'aaron',32212)

ERROR at line 1:

ORA-00001: unique constraint (SCOTT.SYS_C001265) violated

PRIMARY KEY

SQL> create table cars

(model number primary key,

name varchar(10),

32
cost number(6)

);

Table created.

SQL> desc cars;

NAME NULL? TYPE

MODEL NOT NULL NUMBER

NAME VARCHAR2(10)

COST NUMBER(6)

SQL> insert into cars values(&model,&name,&cost);

Enter value for model: 1098

Enter value for name: 'omni'

Enter value for cost: 200000

old 1: insert into cars values(&model,&name,&cost)

new 1: insert into cars values(1098,'omni',200000)

1 row created.

SQL> /

Enter value for model: 9087

Enter value for name: 'qualis'

Enter value for cost: 500000

old 1: insert into cars values(&model,&name,&cost)

new 1: insert into cars values(9087,'qualis',500000)

1 row created.

SQL> /

Enter value for model: 1098

33
Enter value for name: 'innova'

Enter value for cost: 600000

old 1: insert into cars values(&model,&name,&cost)

insert into cars values(1098,'innova',600000)

ERROR at line 1:

ORA-00001: unique constraint (SCOTT.SYS_C001266) violated

CHECK CONSTRAINT:

SQL> create table employ

rno number(5),

name varchar(10),

salary number(10) constraint no_ck check(salary between 10000 and 30000)

);

Table created.

SQL> desc employ;

NAME NULL? TYPE

RNO NUMBER(5)

NAME VARCHAR2(10)

SALARY NUMBER(10)

SQL> insert into employ values(&rno,&name,&salary);

Enter value for rno: 1

Enter value for name: 'sachin'

34
Enter value for salary: 29000

old 1: insert into employ values(&rno,&name,&salary)

new 1: insert into employ values(1,'sachin',29000)

SQL> /

Enter value for rno: 20

Enter value for name: 'rohit'

Enter value for salary: 10000

old 1: insert into employ values(&rno, &name, &salary)

new 1: insert into employ values(20,'rohit',10000)

1 row created.

SQL> /

Enter value for rno: 15

Enter value for name: 'dhoni'

Enter value for salary: 40000

old 1: insert into employ values(&rno,&name,&salary)

new 1: insert into employ values(15,'dhoni',40000)

insert into employ values(15,'dhoni',40000)

ERROR at line 1:

ORA-02290: check constraint (SCOTT.NO_CK) violated

FOREIGN KEY

SQL> create table admin

stuid number constraint stuid_pk primary key,

name varchar(10),
35
permit number(6)

);

Table created.

SQL> desc admin;

NAME NULL? TYPE

STUID NOT NULL NUMBER

NAME VARCHAR2(10)

PERMIT NUMBER(6)

SQL> insert into admin values(&stuid, '&name', &permit);

Enter value for stuid: 1

Enter value for name: ASWIN

Enter value for permit: 80

old 1: insert into admin values(&stuid,'&name',&permit)

new 1: insert into admin values(1,'ASWIN',80)

1 row created.

SQL> /

Enter value for stuid: 2

Enter value for name: ROHIT

Enter value for permit: 67

old 1: insert into admin values(&stuid,'&name',&permit)

new 1: insert into admin values(2,'ROHIT',67)

1 row created.

SQL> /

Enter value for stuid: 4


36
Enter value for name: SANJAY

Enter value for permit: 45

old 1: insert into admin values(&stuid,'&name',&permit)

new 1: insert into admin values(4,'SANJAY',45)

1 row created.

SQL> /

Enter value for stuid: 5

Enter value for name: KAMALINI

Enter value for permit: 35

old 1: insert into admin values(&stuid,'&name',&permit)

new 1: insert into admin values(5,'KAMALINI',35)

1 row created.

SQL> select * from admin;

STUID NAME PERMIT

1 ASWIN 80
2 ROHIT 67
4 SANJAY 45
5 KAMALINI 35

SQL> create table course

stuid number constraint sid_fk references admin(stuid),

branch varchar(6),

sec varchar(2)

);

Table created.
37
SQL> insert into course values(&stuid,'&branch','&sec');

Enter value for stuid: 1

Enter value for branch: CSE

Enter value for sec: A

old 1: insert into course values(&stuid,'&branch','&sec')

new 1: insert into course values(1,'CSE','A')

1 row created.

SQL> /

Enter value for stuid: 2

Enter value for branch: CSE

Enter value for sec: A

old 1: insert into course values(&stuid,'&branch','&sec')

new 1: insert into course values(2,'CSE','A')

1 row created.

SQL> /

Enter value for stuid: 4

Enter value for branch: IT

Enter value for sec: A

old 1: insert into course values(&stuid,'&branch','&sec')

new 1: insert into course values(4,'IT','A')

1 row created.

SQL> /

Enter value for stuid: 6

Enter value for branch: CSE

38
Enter value for sec: A

old 1: insert into course values(&stuid,'&branch','&sec')

new 1: insert into course values(6,'CSE','A')

insert into course values(6,'CSE','A')

ERROR at line 1:

ORA-02291: integrity constraint (SCOTT.SID_FK) violated - parent key not found

SQL> delete from admin where stuid=5;

1 row deleted.

SQL> delete from admin where stuid=1;

delete from admin where stuid=1

ERROR at line 1:

ORA-02292: integrity constraint (SCOTT.SID_FK) violated - child record found

SQL> select * from admin;


STUID NAME PERMIT

1 ASWIN 80
2 ROHIT 67
4 SANJAY 45
SQL> select * from course;
STUID BRANCH SE

1 CSE A

2 CSE A

4 IT A

39
SQL> create table student

idno varchar(4),

name varchar(10),

dept varchar(4),

degree varchar(6),

year number(4)

);

table created.

SQL> desc student;

NAME NULL? TYPE

IDNO VARCHAR2(4)

NAME VARCHAR2(10)

DEPT VARCHAR2(4)

DEGREE VARCHAR2(6)

YEAR NUMBER(4)

SQL> insert into student values('&idno', '&name', '&dept', '&degree', &year);

Enter value for idno: A01

Enter value for name: AARON

Enter value for dept: CSE

Enter value for degree: BE

Enter value for year: 2

old 1: insert into student values('&idno','&name','&dept','&degree',&year)

40
new 1: insert into student values('a01','aaron','cse','BE',2)

1 row created.

SQL> /

Enter value for idno: A02

Enter value for name: AKIL

Enter value for dept: ECE

Enter value for degree: BE

Enter value for year: 2

old 1: insert into student values('&idno','&name','&dept','&degree',&year)

new 1: insert into student values('A02','AKIL','ECE','BE',2)

1 row created.

SQL> /

Enter value for idno: A03

Enter value for name: BENNY

Enter value for dept: IT

Enter value for degree: B.TECH

Enter value for year: 2

old 1: insert into student values('&idno','&name','&dept','&degree',&year)

new 1: insert into student values('A03','BENNY','IT','B.TECH',2)

1 row created.

SQL> /

Enter value for idno: B01

Enter value for name: COOK

Enter value for dept: CSE

Enter value for degree: BE

41
Enter value for year: 1

old 1: insert into student values('&idno','&name','&dept','&degree',&year)

new 1: insert into student values('B01','COOK','CSE','BE',1)

1 row created.

SQL> /

Enter value for idno: B02

Enter value for name: DANNY

Enter value for dept: MECH

Enter value for degree: BE

Enter value for year: 1

old 1: insert into student values('&idno','&name','&dept','&degree',&year)

new 1: insert into student values('B02','DANNY','MECH','BE',1)

1 row created.

SQL> /

Enter value for idno: B03

Enter value for name: ELAN

Enter value for dept: IT

Enter value for degree: B.TECH

Enter value for year: 1

old 1: insert into student values('&idno','&name','&dept','&degree',&year)

new 1: insert into student values('B03','ELAN','IT','B.TECH',1)

1 row created.

42
SQL> SELECT * FROM STUDENT;

IDNO NAME DEPT DEGREE YEAR

A01 AARON CSE BE 2

A02 AKIL ECE BE 2

A03 BENNY IT B.TECH 2

B01 COOK CSE BE 1

B02 DANNY MECH BE 1

B03 ELAN IT B.TECH 1

6 rows selected.

DISTINCT

SQL> select distinct dept from student;


DEPT

CSE
ECE
IT
MECH

SQL> select name from student;


NAME

AARON
AKIL
BENNY
COOK
DANNY
ELAN
6 rows selected.
43
IN

SQL> select * from student where year IN 2;

IDNO NAME DEPT DEGREE YEAR

A01 AARON CSE BE 2


A02 AKIL ECE BE 2
A03 BENNY IT B.TECH 2

SQL> select * from student where name BETWEEN 'AARON' and 'COOK';

IDNO NAME DEPT DEGREE YEAR

A01 AARON CSE BE 2


A02 AKIL ECE BE 2
A03 BENNY IT B.TECH 2
B01 COOK CSE BE 1

AS

SQL> select IDNO as rollno from student;


ROLLNO

A01
A02
A03
B01
B02
B03
6 rows selected.

SORT

SQL> select * from student where year<3 order by name desc;


IDNO NAME DEPT DEGREE YEAR

44
B03 ELAN IT B.TECH 1
B02 DANNY MECH BE 1
B01 COOK CSE BE 1
A03 BENNY IT B.TECH 2
A02 AKIL ECE BE 2
A01 AARON CSE BE 2
6 rows selected.

SQL> select * from student where year<3 order by dept asc;

IDNO NAME DEPT DEGREE YEAR

A01 AARON CSE BE 2


B01 COOK CSE BE 1
A02 AKIL ECE BE 2
A03 BENNY IT B.TECH 2
B03 ELAN IT B.TECH 1
B02 DANNY MECH BE 1
6 rows selected.

LIKE

SQL> select * from student where name LIKE '%Y';


IDNO NAME DEPT DEGREE YEAR

A03 BENNY IT B.TECH 2


B02 DANNY MECH BE 1

SQL> select * from student where name LIKE 'A%';


IDNO NAME DEPT DEGREE YEAR

A01 AARON CSE BE 2


A02 AKIL ECE BE 2

45
IS NULL

SQL> select * from student where IDNO IS NULL;

no rows selected

LOGICAL OR

SQL> select * from student where IDNO='A01' OR IDNO='B01';

IDNO NAME DEPT DEGREE YEAR

A01 AARON CSE BE 2


B01 COOK CSE BE 1

RESULT:

Successfully implemented Constraints on tables in SQL.

46
EX.NO:5

FORMS-TRIGGERS-MENU DESIGN

Aim:

To study and execute Triggers in RDBMS.

Definition & Syntax: -

TRIGGER:

A database trigger is a stored procedure that is fired when an insert, update or delete
statement is issued against the associated table. Database triggers can be used for the
following purposes.
To generate data automatically.
To enforce complex integrity constraints. (e.g., Checking with sysdate, checking
with data in another table).
To customize complex security authorizations.
To maintain replicate tables.
To audit data modifications.

Syntax for Creating Triggers


The syntax for creating a trigger is given below

CREATE OR REPLACE TRIGGER <trigger_name>


[BEFORE/AFTER] [INSERT/UPDATE/DELETE] ON <table_name>
[FOR EACH statement/FOR EACH row]
[WHEN <condition>]
PL/SQL block;

PARTS OF A TRIGGER
A database trigger has three parts, namely, a trigger statement, a trigger body and a
trigger restriction.

47
TRIGGER STATEMENT:
A trigger statement specifies the DML statements like update, delete and insert and it
fires the trigger body. It also specifies the table to which the trigger is associated.

TRIGGER BODY:
Trigger body is a PL/SQL block that is executed when a triggering statement is
issued.

TRIGGER RESTRICTION:
Restrictions on a trigger can be achieved using the WHEN clause as shown in the
syntax for creating triggers. They can be included in the definition of a row trigger, where in,
the condition in the WHEN clause is evaluated for each row that is affected by the trigger.

TYPES OF TRIGGER:
Triggers are categorized into the following types based on when they are fired:

⮚ Before
⮚ After
⮚ For each row
⮚ For each statement (default)

BEFORE /AFTER OPTIONS:


The before/after options can be used to specify when the trigger body should be fired
with respect to the triggering statement. If the user includes a BEFORE option, then, Oracle
fires the trigger before executing the triggering statement. On the other hand, if AFTER is
used, then, Oracle fires the trigger after executing the triggering statement.

FOR EACH ROW / STATEMENT:


When the for each row / statement option when included in the ‘create trigger’ syntax
specifies that the trigger fires once per row. By default, a database trigger fires for each
statement.

Using a combination of the above options, we can assign 12 types of triggers to a database
table.

Before update row / statement


Before delete row / statement

48
Before insert row / statement
After update row / statement
After delete row / statement
After insert row / statement

EXERCISE:
1. Write a PL/SQL program to create a trigger before the user inserts the data into the table.
2. Write a PL/SQL program to create a trigger before the user deletes the data from the table.
3. Write a PL/SQL program to create a trigger before the user changes the value of the salary
of the employee.

ANSWERS:
SQL>create or replace trigger ins1 before insert on emp begin
raise_application_error (-20001,'you can’t insert a row'); end;

OUTPUT:
SQL>insert into emp
values(&eid,'&name','&dob','&addr','&sex','&desig',&deptno,'&maritsta',&salary);
SQL>insert into emp *
values(&eid,'&name','&dob','&addr','&sex','&desig',&deptno,'&maritsta',&salary);
ERROR at line 1:
ORA-20001: you cant insert a row
ORA-06512: at "CSE382.ins1", line 2
ORA-04088: error during execution of trigger 'CSE382.ins1'

SQL>create or replace trigger del1 before delete on emp


begin
raise_application_error (-20001,'you can’t delete a row');
end;

49
OUTPUT:
SQL>delete from emp where eid=4444;
delete from emp where eid=4444;
*
ORA-20001: you can’t delete a row
ORA-06512: at "CSE382.DEL1", line 2
ORA-04088: error during execution of trigger 'CSE382.DEL1'

SQL> create trigger upd1 before update on emp for each row 2 begin
3 if :new.sal < 1000 then
4 raise_application_error(-20001,'salary can’t be low thanthis'); 5 end if;
6 end;
7/
Trigger created.

SQL> update emp set sal=500 where dno=2;


update emp set sal=500 where dno=2
*
ERROR at line 1:
ORA-20001: salary can’t be low than this
ORA-06512: at "CSE382.UPD1", line 3
ORA-04088: error during execution of trigger 'CSE382.UPD1'

RESULT:

Thus forms-triggers-menu design is executed.

50

You might also like