Database Management lab manual
Database Management 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
Aim:
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).
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
TRUNCATE TABLE:
Remove all records from a table, including all spaces allocated for the records are
removed.
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,
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,
DESCRIBE TABLE
SQL> desc employee;
Name Null? Type
3
SQL> desc salary;
Name Null? Type
ALTER TABLE
I. ADD:
SQL> alter table employee add(designation varchar2(15));
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.
EMPID VARCHAR2(10)
EMPNAME VARCHAR2(20)
AGE NUMBER(3)
SEX CHAR(1)
ERROR:
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')
1 arun it 22 m
6
2 bala service 26 m
3 chitra sales 25 f
SQL> commit;
Commit complete.
Commit complete.
DROP TABLE
RESULT:
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:
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
Syntax:
To select all columns from the table * is used instead of column names.
Syntax:
The DISTINCT keyword is used to return only different values (i.e. ) this
command does not select the duplicate values from the table.
Syntax:
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:
BETWEEN can be used to get those items that fall within a range.
Syntax:
6. Renaming:
The select statement can be used to rename either a column or the entire
table.
Syntax:
Renaming a column:
Renaming a table:
7. Sorting:
The select statement with the order by Clause is used to sort the contents
Syntax:
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-“;
The SELECT INTO statement is most often used to create backup copies of
tables or for archiving records.
Syntax:
WHERE condition.
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:
Syntax:
12
EXERCISE:
INSERT COMMAND
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.
values('&empid','&empname','&gender',&age,'&dept','&dob','&doj','&desig');
Enter value for empid: acc9001
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
1 row created.
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.
16
Enter value for empname: suresh
Enter value for gender: male
Enter value for age: 40
Enter value for dept: sales
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
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.
19
new 1: insert into salary values('sal9006',17000,'sales ','abt maruthi')
1 row created.
8 rows selected.
DOJ DESIGNATION
20
DOJ DESIGNATION
DOJ DESIGNATION
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.
22
1 row created.
UPDATE COMMAND
SQL> update employee set empname = 'arunprasanth' where empid='it9001';
1 row updated.
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
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
Table created.
IDNO NUMBER
NAME VARCHAR2(10)
BRANCH VARCHAR2(4)
Table altered.
IDNO NUMBER
25
NAME VARCHAR2 (10)
IDNO NUMBER
1 row created.
1 row created.
1 row created.
26
Enter value for idno: 04
1 row created.
SQL> /
1 row created.
1 ASHOK CSE BE
2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT BE
5 HARRY IT BE
2 rows updated.
27
SQL> select * from student;
1 ASHOK CSE BE
2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT B.TECH
5 HARRY IT B.TECH
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.
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.
NOT NULL
1 ASHOK CSE BE
2 BHAVANA CSE BE
3 CAVIN CSE BE
4 DANNY IT B.TEC
H
varchar(10),branch varchar(6)
); Table created.
NAME VARCHAR2(10)
BRANCH VARCHAR2(6)
1 row created.
SQL> /
1 row created.
SQL> /
ERROR at line 1:
UNIQUE
name varchar(10),
salary number
);
Table created.
ROLLNO NUMBER
NAME VARCHAR2(10)
SALARY NUMBER
1 row created.
SQL> /
1 row created.
SQL> /
ERROR at line 1:
PRIMARY KEY
name varchar(10),
32
cost number(6)
);
Table created.
NAME VARCHAR2(10)
COST NUMBER(6)
1 row created.
SQL> /
1 row created.
SQL> /
33
Enter value for name: 'innova'
ERROR at line 1:
CHECK CONSTRAINT:
rno number(5),
name varchar(10),
);
Table created.
RNO NUMBER(5)
NAME VARCHAR2(10)
SALARY NUMBER(10)
34
Enter value for salary: 29000
SQL> /
1 row created.
SQL> /
ERROR at line 1:
FOREIGN KEY
name varchar(10),
35
permit number(6)
);
Table created.
NAME VARCHAR2(10)
PERMIT NUMBER(6)
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
1 ASWIN 80
2 ROHIT 67
4 SANJAY 45
5 KAMALINI 35
branch varchar(6),
sec varchar(2)
);
Table created.
37
SQL> insert into course values(&stuid,'&branch','&sec');
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
38
Enter value for sec: A
ERROR at line 1:
1 row deleted.
ERROR at line 1:
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.
IDNO VARCHAR2(4)
NAME VARCHAR2(10)
DEPT VARCHAR2(4)
DEGREE VARCHAR2(6)
YEAR NUMBER(4)
40
new 1: insert into student values('a01','aaron','cse','BE',2)
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
41
Enter value for year: 1
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
42
SQL> SELECT * FROM STUDENT;
6 rows selected.
DISTINCT
CSE
ECE
IT
MECH
AARON
AKIL
BENNY
COOK
DANNY
ELAN
6 rows selected.
43
IN
SQL> select * from student where name BETWEEN 'AARON' and 'COOK';
AS
A01
A02
A03
B01
B02
B03
6 rows selected.
SORT
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.
LIKE
45
IS NULL
no rows selected
LOGICAL OR
RESULT:
46
EX.NO:5
FORMS-TRIGGERS-MENU DESIGN
Aim:
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.
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)
Using a combination of the above options, we can assign 12 types of triggers to a database
table.
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'
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.
RESULT:
50