Achintya+Dbms+Sem+4.PDF
Achintya+Dbms+Sem+4.PDF
PANIPAT INSTITUTE OF
ENGINEERING AND TECHNOLOGY
Affiliated to:
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:
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
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
Output-
8|Page
ACHINTYA SHARMA 2821302
Command – NOT
select Name, Branch from Student01 where not Roll_No =101
Output-
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.
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:
Output:
11 | P a g e
ACHINTYA SHARMA 2821302
Example-
Output-
The error occurs because we try to insert a null value in the primary key attribute which is not possible.
Example-
Table 1-
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))
Select*from student
Output-
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
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')
Output-
14 | P a g e
ACHINTYA SHARMA 2821302
Arithmetic Operations-
Output-
Example-
update Employee112 set Salary = Salary - 400
select * from Employee112
Output-
15 | P a g e
ACHINTYA SHARMA 2821302
Example-
update Employee112 set Salary = Salary * 3
select * from Employee112
Output-
Example-
update Employee112 set Salary = Salary / 2
Output-
16 | P a g e
ACHINTYA SHARMA 2821302
Relational Operations-
Equal Operator (=)
Example-
select * from Employee112 where Salary = 45150
Output-
Example-
select * from Employee112 where Salary < 55000
Output-
Example-
select * from Employee112 where Salary > 55000
17 | P a g e
ACHINTYA SHARMA 2821302
Output-
Example-
select * from Employee112 where Salary >= 50000
Output-
Example-
select * from Employee112 where Salary <= 60000
Output-
18 | P a g e
ACHINTYA SHARMA 2821302
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-
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:
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
select*from company1
Output-
Command- AVERAGE(AVG)
Select Avg(E_Salary) as Avg_Salary from Company1
Output-
Command : COUNT
Output-
24 | P a g e
ACHINTYA SHARMA 2821302
Command - MIN
Output-
Command - MAX
Output-
25 | P a g e
ACHINTYA SHARMA 2821302
PRACTICAL - 6
Aim: Write SQL queries for extracting data from more than one table.
Output-
Output-
26 | P a g e
ACHINTYA SHARMA 2821302
Output-
Output-
27 | P a g e
ACHINTYA SHARMA 2821302
Output-
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
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:
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
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:
Command:
36 | P a g e
ACHINTYA SHARMA 2821302
Output:
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
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