ACHINTYA SHARMA 2821302
PANIPAT INSTITUTE OF
ENGINEERING AND TECHNOLOGY
Department of Emerging Technologies
DBMS FILE
(PC-CS-AIML-216A)
Submitted to: Submitted by:
Dr. Shally Nagpal ACHINTYA SHARMA
Asst. Professor B. Tech CSE - AIML
4th Semester (2821302)
Affiliated to:
KURUKSHETRA UNIVERSITY, KURUKSHETRA, INDIA
1|Page
ACHINTYA SHARMA 2821302
INDEX
S.NO PRACTICAL DATE SIGNATURE
1 Create a database and write the programs to carry out
the following operation:
a. Add, Delete, and modify a record in the
database.
b. Generate queries
c. Data Operations
d. List all the records of the database in ascending
order.
2 To perform various integrity constraints on relational
databases.
3 Create a database and perform the following
operations: -
a. Arithmetic and Relational Operations
b. Group By and Having clause
c. Like predicate for pattern matching in the
database
4 Using two tables creates a view that shall perform
natural join, equi join, outer joins
5 Write SQL Query using Character, Number, and Date.
6 Write SQL Query for extracting Data from more than
one table.
7 Write a procedure for computing the amount telephone
bill on the basis of the following conditions: -
a. telephone rent Rs. 205 including the first 105 free
units.
b. if extra units are>0 but <500 then the rate is 80 paise
per unit
c. if extra units are>500 then the rate is Rs. 1.20 per
unit.
For this purpose, create a table with name, Phone No.,
No. of units consumed, bill amount of a customer.
8 Write a trigger for before and after updation, deletion,
or insertion.
9 Create a view to display details of employees working
on more than one project.
10 Create a view to display details of employees not
working on any project.
2|Page
ACHINTYA SHARMA 2821302
PRACTICAL - 1
Aim: Create a database and write the programs to carry out thefollowing operation:
1. Add, Delete, and modify a record in the database.
2. Generate queries
3. Data Operations
4. List all the records of the database in ascending order
Table Used:
create table Student01 (Name varchar (15), Roll_No number (4), Branch
varchar(6))insert into Student01 values('Sabhyata ', '101', 'Btech')
insert into Student01 values('Peter', '102', 'Btech')
insert into Student01 values('Sam', '103', 'Mtech')
insert into Student01 values('John', '104', 'Bsc')
insert into Student01 values('Mury', '105', 'IIT')
insert into Student01 values('Dan', '109', 'MBA')
3|Page
ACHINTYA SHARMA 2821302
ADD, DELETE, AND MODIFY A RECORD IN THE DATABASE.
Command: INSERT
Syntax-
insert into <table name> values (<expression1>,<expression2>)
Example-
insert into Student01 values ('Sri', '111', 'IIT')
OUTPUT
4|Page
ACHINTYA SHARMA 2821302
Command: UPDATE
Syntax-
update tablename set field=values where the condition
Example-
update Student01 set Branch='MBA' where Roll_No='111'
OUTPUT-
AFTER UPDATING-
5|Page
ACHINTYA SHARMA 2821302
COMMAND: DELETE
Syntax-
Delete from <table-name>
Example-
delete from Student01 where Branch='Bsc'
6|Page
ACHINTYA SHARMA 2821302
AFTER DELETION-
Generate queries
select * from Student01
OUTPUT
7|Page
ACHINTYA SHARMA 2821302
select Name, Branch from Student01 where Roll_No Between 105 and 111
Output-
Data Operations
Command- AND
select Name from Student01 where Roll_No =101 and Branch='Btech'
Output-
Command - OR
select S_Name from College where C_ID = 1000 or Age = 19
Output-
8|Page
ACHINTYA SHARMA 2821302
Command – NOT
select Name, Branch from Student01 where not Roll_No =101
Output-
List all the records of database in ascending order
Command – ORDER BY
select * from Student01 order by Roll_No
Output-
9|Page
ACHINTYA SHARMA 2821302
PRACTICAL - 2
Aim: To perform various integrity constraints on relational databases.
Primary Key Constraints:
create table emp(E_ID varchar(5) primary key, Name varchar(10), Age number(2), check(Age>20))
select * from emp
Output:
insert into emp values('', 'Inosuke', '19')Output:
insert into emp values('E256', 'Inosuke', '19')
Output:
10 | P a g e
ACHINTYA SHARMA 2821302
Domain Constraints:
create table emp(E_ID varchar(5) primary key, Name varchar(10), Age number(2), check(Age>20))
select * from emp
Output:
insert into emp values('E25', 'InU', '17')
Output:
11 | P a g e
ACHINTYA SHARMA 2821302
Entity integrity constraints-
The entity integrity constraint states that the primary key value can't be null. This is because the primary key
value is used to identify individual rows in relation and if the primary key has a null value, then we can't identify
those rows. A table can contain a null value other than the primary key field.
Example-
Insert into emp values (‘’, ’Alex’,’20’)
Output-
The error occurs because we try to insert a null value in the primary key attribute which is not possible.
Referential Integrity Constraints-
A referential integrity constraint is specified between two tables. In the Referential integrity constraints, if a foreign key
in Table 1 refers to the Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be null or be
available in Table 2.
Example-
Table 1-
Create table Dept(D_No number(2) primary key, D_Name char(25))
Insert into Dept values(‘1’, ‘CSE’)
Select * form Dept
Output-
12 | P a g e
ACHINTYA SHARMA 2821302
Create table Student(S_Id number(5) primary key, Name char(20), Age number(2), D_No number(2) references
Dept(D_No))
Insert into Student values('1000','Diksha','20','1')
Select*from student
Output-
Insert into Student values(‘1003’,’James’,’M’,’25’,’5’)
Output-
The error occurs because we try to insert D_No-5 in table 2 but the value Is not present in table 1 andviolate
Foreign Key Constraint.
13 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 3
Aim: Create a database and perform the following operations: -
1. Arithmetic and Relational Operations
2. Group By and Having clause
3. Like predicate for pattern matching in the database
Table creation:
create table Employee112(E_ID number(5) primary key, Name char(20), Age number(2), Salary
number(6))
insert into Employee112 values('251', 'Sabhyata ', '19', '45000')
insert into Employee112 values('123', 'Alex', '19', '40000')
insert into Employee112 values('321', 'James', '20', '35000')
insert into Employee112 values('456', 'Lucy', '21', '37000')
insert into Employee112 values('654', 'Peter', '20', '30000')
insert into Employee112 values('789', 'Parle', '21', '33000')
select * from Employee112
Output-
14 | P a g e
ACHINTYA SHARMA 2821302
Arithmetic Operations-
Add Operator (+)
Example-
update Employee112 set Salary = Salary + 500
select * from Employee112
Output-
Subtract Operator (-)
Example-
update Employee112 set Salary = Salary - 400
select * from Employee112
Output-
15 | P a g e
ACHINTYA SHARMA 2821302
Multiply Operator (*)
Example-
update Employee112 set Salary = Salary * 3
select * from Employee112
Output-
Divide Operator (/)
Example-
update Employee112 set Salary = Salary / 2
select * from Employee112
Output-
16 | P a g e
ACHINTYA SHARMA 2821302
Relational Operations-
Equal Operator (=)
Example-
select * from Employee112 where Salary = 45150
Output-
Less than Operator (<)
Example-
select * from Employee112 where Salary < 55000
Output-
Greater than Operator (>)
Example-
select * from Employee112 where Salary > 55000
17 | P a g e
ACHINTYA SHARMA 2821302
Output-
Greater than or equal to (>=)
Example-
select * from Employee112 where Salary >= 50000
Output-
Less than or equal to (<=)
Example-
select * from Employee112 where Salary <= 60000
Output-
18 | P a g e
ACHINTYA SHARMA 2821302
Not equal to (<>)
Example-
select * from Employee112 where Salary <> 49650
Output-
Group By Clause-
Example-
select count(Name), Age from Employee112 group by Age
Output-
Having Clause-
Example-
select count(Name), Age from Employee112 group by Age having count(E_ID)>=2
19 | P a g e
ACHINTYA SHARMA 2821302
Output-
Like predicate for pattern matching in database-
Example-
select Name from Employee112 where Name like 'P%'
Output-
20 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 4
Aim: Using two tables create a view that shall perform natural join,equi join, and outer joins.
Commands:
select * from emp1
Output:
select * from dept1
Output:
Full Join:
Select emp1.EID , emp1.Name , emp1.Salary , emp1.DID, dept1.DID , dept1.D_Name from emp1 full join
dept1 on emp1.DID = dept1.DID
21 | P a g e
ACHINTYA SHARMA 2821302
Inner Join:
Select emp.eid , emp.Name ,emp.Salary, emp.DID, Dept.DID ,Dept.D_Name from emp inner join Dept on
emp.DID = Dept.DID Select emp1.EID , emp1.Name , emp1.Salary , emp1.DID, dept1.DID , dept1.D_Name
from emp1 inner join dept1 on emp1.DID = dept1.DID
Output:
Left Join:
Select emp1.EID , emp1.Name , emp1.Salary , emp1.DID, dept1.DID , dept1.D_Name from emp1 left join
dept1 on emp1.DID = dept1.DID
22 | P a g e
ACHINTYA SHARMA 2821302
Right Join:
Select emp.eid , emp.Name ,emp.Salary, emp.DID, Dept.DID ,Dept.D_Name from emp right join Dept on
emp.DID = Dept.DID
Output:
23 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 5
Aim: Write SQL queries using Character, Number, Date.
Example-
create table company1(e_id number(4) primary key,e_name char(10),e_salary
number(5),date_of_joining DATE NOT NULL)
insert into company1 values('101','Diksha','70000' to_date(‘2019-12-26’,’yyyy-mm-dd’))
select*from company1
Output-
Command- AVERAGE(AVG)
Select Avg(E_Salary) as Avg_Salary from Company1
Output-
Command : COUNT
select Count(E_Name) as Count_Name from Company1
Output-
24 | P a g e
ACHINTYA SHARMA 2821302
Command - MIN
select Min(E_Salary) as Min_Salary from Company1
Output-
Command - MAX
select Max(E_Salary) as Max_Salary from Company1
Output-
25 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 6
Aim: Write SQL queries for extracting data from more than one table.
create table employee1(e_id number(4) primary key,name char(10),salary number(5))
insert into employee1 values('101','Diksha','70000')
select * from Employee1
Output-
create table student11(s_id number(4) primary key,name char(10),fees number(5))
insert into student11 values('101','Diksha','7000')
select * from student11
Output-
Command- RIGHT JOIN
select Employee1.E_ID, Employee1.Name, Employee1.Salary, Student11.S_ID, Student11. Name,
Student11.Fees from Employee1 Right Join Student11 on Employee1.Name= Student11. Name
26 | P a g e
ACHINTYA SHARMA 2821302
Output-
Command- LEFT JOIN
select Employee1.E_ID, Employee1.Name, Employee1.Salary, Student11.S_ID, Student11.Name,
Student11.Fees from Employee1 left Join Student11 on Employee1.Name
= Student11.Name
Output-
Command- FULL JOIN
select Employee1.E_ID, Employee1.Name, Employee1.Salary, Student11.S_ID, Student11.Name,
Student11.Fees from Employee1 full Join Student11 on Employee1.Name
= Student11.Name
27 | P a g e
ACHINTYA SHARMA 2821302
Output-
Command- INNER JOIN
select Employee1.E_ID, Employee1.Name, Employee1.Salary, Student11.S_ID, Student11.Name,
Student11.Fees from Employee1 inner Join Student11 on Employee1.Name
= Student11.Name
Output-
28 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 7
Aim: Write a procedure for computing the amount of the telephone bill on the basis of the following
conditions.
1. telephone rent Rs. 205 including the first 105 free units.
2. if extra units>0 but<500 then the rate is 80 paise per unit.
3. If extra units>500 then the rate is Rs. 1.20 per unit.
For this purpose, create a table with name, Phone No., No. of units consumed, bill amount of a customer.
Commands:
create table telephone(
name text not null,
phone integer not null,
units integer not null,
bill integer
);
insert into telephone values('Chiranjevee', 8854695663, 97, 205);
insert into telephone values('Arjun', 5469566388, 300, 205);
insert into telephone values('Kal Bharav', 8854695773, 225, 205);
insert into telephone values('Doryodhan', 8854697893, 108, 205);
insert into telephone values('Krishna', 8858995663, 543, 205);
Output:
29 | P a g e
ACHINTYA SHARMA 2821302
select units, bill,
case
when units < 106
then 205
when units > 105 and units<501
then 205+(units-105)*0.80
else 521+(units-500)*1.20
end
from telephone;
Output:
30 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 8
Aim: Write a trigger for before and after updation, deletion or insertion.
Commands:
create table student00 (Roll_No number(5) primary key, Name char(10), Fees number(5))insert into student00
values('111', 'Sandy', '5200')
insert into student00 values('112', 'Sam', '5000') insert
into student00 values('113', 'Mandy', '200')insert into
student00 values('114', 'Saby', '8200') insert into
student00 values('115', 'Niti', '2200') select * from
student00
Output:
create table security(Roll_No number(5), Name char(10), Fees number(5), operator char(10),user_info
char(10), D_T_info timestamp)
Output:
31 | P a g e
ACHINTYA SHARMA 2821302
After Trigger:
create or replace trigger trig after inserting or delete or updating on
student00 for each row
declare
operator char(10);
user_info char(10);
D_T_info timestamp;
begin
if inserting then
operator:='insert';user_info:=
user;
32 | P a g e
ACHINTYA SHARMA 2821302
D_T_info:= SYSTIMESTAMP;
end if;
if updating then
operator:='update';
user_info:= user;
D_T_info:= SYSTIMESTAMP;
end if;
if deleting then
operator:='delete';
user_info:= user;
D_T_info:= SYSTIMESTAMP;
end if;
insert into security values(:old.Roll_No, :old.Name, :old.Fees, operator, user_info,D_T_info);
end
Output:
update student00 set Name='Sandeep' where Roll_No='111'insert into
student00 values('118', 'Diljit', '10000')
delete from student00 where Roll_No='112'select *
from security
Output:
33 | P a g e
ACHINTYA SHARMA 2821302
Before Trigger:
create table students1234(name varchar2(20),roll_no varchar2(9) not null primarykey,balance
varchar2(10));
insert into students1234 values('Sabhyata ','1207932','156');insert into
students1234 values('Aditya','1207934','250'); insert into students1234
values('Tafseer','1207931','195'); select * from students1234;
Output:
create or replace trigger tri1234 before insert or update or delete on students1234 for eachrow
begin
if :new.balance<=0 then
raise_application_error(-20003,'salary can not be less than zero'); end if;end;
Output:
34 | P a g e
ACHINTYA SHARMA 2821302
insert into students1234 values('Divanshi','1207933','256');
Output:
select * from students1234;
35 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 9
Aim: Create a view to display details of employees working on morethan one project.
Table Creation:
select * from employee000
select * from project001
Command:
create view V002 as select employee000.Name, project001.Project_Name from employee000 inner
join project001 on employee000.E_ID = project001.E_ID where project001.E_ID in ( select E_ID from
project001 group by E_ID having Count(E_ID)>1)
36 | P a g e
ACHINTYA SHARMA 2821302
Output:
select * from MultiProj
Output:
37 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 10
Aim: Create a view to display details of employees not working on any project.
Commands:
select * from employee000
select * from project001
38 | P a g e
ACHINTYA SHARMA 2821302
Command:
create view NoProject as select employee000.E_ID, employee000.Name from employee000 left join
project001 on employee000.E_ID = project001.E_ID where project001.E_ID is null order by
employee000.E_ID
Output:
Command:
select * from NoProj
39 | P a g e