Database Management Systems Lab Manual
Database Management Systems Lab Manual
LAB MANUAL
Practice of Views.
12
3. Hostel management
14 [Link] management
[Link] Railways reservation
VEMUIT Page 1
Department of CSE
INPUT:
SQL>
RESULT: Table created;
2. SQL - ALTER TABLE
(a) Add New column
Syntax: ALTER TABLE table_nameADD column_namedatatype;
INPUT:
SQL> ALTER TABLE Persons ADD DateOfBirth date;
VEMUIT Page 2
Department of CSE
RESULT:
RESULT:
Table droped;
4. TRUNCATE:
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.
Syntax
TRUNCATE TABLE table_name;
VEMUIT Page 3
Department of CSE
INPUT:
SQL> TRUNCATE TABLE dept;
RESULT:
Basic SQL DML Commands.
To practice basic SQL DML Commands such as INSERT, DELETE, etc.
Syntax:
The first way specifies both the column names and the values to be inserted:
RESULT:
VEMUIT Page 4
Department of CSE
UPDATE: The UPDATE statement is used to modify the existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE
statement. The WHERE clause specifies which record(s) that should be updated. If you omit the
WHERE clause, all records in the table will be updated!
SQL>
The following SQL statement updates the first customer (CustomerID = 1) with a new contact
person and a new city.
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
RESULT:
You have made changes to the database. Rows affected: 1
DELETE:
Syntax
DELETE FROM table_name
WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) that should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!
VEMUIT Page 5
Department of CSE
The COMMIT command is the transactional command used to save changes invoked by a
transaction to the database.
The COMMIT command is the transactional command used to save changes invoked by a
transaction to the database. The COMMIT command saves all the transactions to the database
since the last COMMIT or ROLLBACK command.
Syntax:
Commit;
INPUT:
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
Following is an example which would delete those records from the table which have age = 25
and then COMMIT the changes in the database.
VEMUIT Page 6
Department of CSE
SQL> COMMIT;
RESULT:
Thus, two rows from the table would be deleted and the SELECT statement would produce the
following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
2. ROLLBACK:
The ROLLBACK command is the transactional command used to undo transactions that have not
already been saved to the database. This command can only be used to undo transactions since the
last COMMIT or ROLLBACK command was issued.
ROLLBACK;
INPUT:
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
VEMUIT Page 7
Department of CSE
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would delete those records from the table which have the age =
25 and then ROLLBACK the changes in the database.
SQL> ROLLBACK;
RESULT:
Thus, the delete operation would not impact the table and the SELECT statement would produce
the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
3. SAVEPOINT:
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain
point without rolling back the entire transaction.
SAVEPOINT SAVEPOINT_NAME;
VEMUIT Page 8
Department of CSE
This command serves only in the creation of a SAVEPOINT among all the transactional
statements. The ROLLBACK command is used to undo a group of transactions.
ROLLBACK TO SAVEPOINT_NAME;
Following is an example where you plan to delete the three different records from the
CUSTOMERS table. You want to create a SAVEPOINT before each delete, so that you can
ROLLBACK to any SAVEPOINT at any time to return the appropriate data to its original state.
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
Savepoint created.
1 row deleted.
VEMUIT Page 9
Department of CSE
Savepoint created.
1 row deleted.
Savepoint created.
1 row deleted.
Now that the three deletions have taken place, let us assume that you have changed your mind and
decided to ROLLBACK to the SAVEPOINT that you identified as SP2. Because SP2 was created
after the first deletion, the last two deletions are undone .
RESULT:
SQL> ROLLBACK TO SP2;Rollback complete.
Notice that only the first deletion took place since you rolled back to SP2.
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
6 rows selected.
VEMUIT Page 10
Department of CSE
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
INPUT:
VEMUIT Page 11
Department of CSE
FROM CUSTOMERS
RESULT:
+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+
3. SELECT with SubQueries:
Syntax:
WHERE ID IN (SELECT ID
FROM CUSTOMERS
+----+----------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+---------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
VEMUIT Page 12
Department of CSE
The SQL LIKE clause is used to compare a value to similar values using wildcard operators.
There are two wildcards used in conjunction with the LIKE operator.
or
or
RESULT:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
+----+----------+-----+-----------+----------+
VEMUIT Page 13
Department of CSE
The HAVING Clause enables you to specify conditions that filter which group results
appear in the results.
The WHERE clause places conditions on the selected columns, whereas the HAVING clause
places conditions on groups created by the GROUP BY clause.
To write queries using clauses such as GROUP BY, ORDER BY, etc. and retrieving
information by joining tables.
Source tables: emp, dept, programmer, software, study.
Order by :The order by clause is used to display the results in sorted order.
The HAVING Clause enables you to specify conditions that filter which group results appear in
the results.
The WHERE clause places conditions on the selected columns, whereas the HAVING clause
places conditions on groups created by the GROUP BY clause.
Syntax
The following code block shows the position of the HAVING Clause in a query.
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
The HAVING clause must follow the GROUP BY clause in a query and must also precede the
ORDER BY clause if used. The following code block has the syntax of the SELECT statement
including the HAVING clause −
VEMUIT Page 14
Department of CSE
HAVING [ conditions ]
ORDER BY column1, column2
Group by : The attribute or attributes given in the clauses are used to form groups. Tupleswith the
same value on all attributes in the group by clause are placed in one group.
GROUP BY NAME;
Having: SQL applies predicates (conditions) in the having clause after groups have beenformed,
so aggregate function be used.
FROM CUSTOMERS
GROUP BY age
VEMUIT Page 15
Department of CSE
Begin
BS: = &bs;
Dt: = 200;
Da=bs *0.15;
Gs:=bs+hra +da;
Ns:= da – dt;
End;
6. Whenever salary is updated and its value becomes less than 5000 a trigger has tobe raised
preventing the operation.
SQL>create trigger emp 5000
Before insert on emp
For each row
begin
If(:[Link]<5000) then
Dbms_output.put_line(‘sal shouldn’t be less than 5000’);
Else
Dbms_output.put_line(‘sal can beentered into employee table ’);
End if;
End;
/
RESULT:
Trigger created;
7. The assertions are: hra should not be less than 10% of basic and da should not beless than
50% of basic.
SQL>create trigger emphrada
Before insert on emp
for each row
Begin
If (:[Link]<((basic*10)/100)) then
VEMUIT Page 17
Department of CSE
RESULT:
Table created;
9. When the da becomes more than 100%, a message has to be generated and with user
permission da has to be merged with basic.
SQL> create trigger dabasic
Before insert on emp
For each row
begin
If(:[Link]>=:[Link])then
Dbms_output.put_line(‘da should be merged with basic’);
End if;
End;
/
RESULT:
Trigger created;
[Link] should be unique and has to be generated automatically.
VEMUIT Page 18
Department of CSE
SQL>
create table empt(empno number(5) unique);
RESULT:
Table created;
[Link] the employee is going to retire in a particular month, automatically a message has to be
generated.
SQL>
declare
Empno number:=7782
Hiredate date:= ’23-jan-1999’;
C varchar(15);
Begin
C:=extract(month from add_months(hiredate,720));
Dbms_output.put_line(‘empno’||empno ||’retiers in c’||c);
End;
/
RESULT:
Empno 7782 retires in c 2
13. When the employees called daily-wagers are to be added the constraint that
salary should be greater than or equal to 5000 should be dropped.
SQL>
Declare
VEMUIT Page 19
Department of CSE
Jobtypevarchar(20);
Sal number(7);
Begin
Jobtype:=’dailywager’;
Sal:=10000;
If(jobtype=’dailywager’) then
If(sal>=5000) then
Dbms_output.put_line(‘employee can’t be added’);
else
Dbms_output.put_line(‘employee can be added’);
End if;
End if;
End;
/
RESULT:
employee can’t be added
14. Display the information of the employees and departments with description of the fields.
SQL>
descempt;
Descdept;
RESULT:
Empt:
Name null? Type
Empno number(5)
Ename varchar2(15)
Job varchar2(15)
Mgr number(5)
Hiredate date
Sal number(7,2)
Comm number(7,2)
Dept:
VEMUIT Page 20
Department of CSE
[Link] the maximum salary of each department and also all departments put together.
SQL>
select max(sal) from empgroupbydeptno union select max(sal) from emp;
RESULT :
max(sal)
2850
3000
5000
18. Commit the changes whenever required and rollback if necessary.
SQL>
Commit;
Rollback to r1;
VEMUIT Page 21
Department of CSE
RESULT:
Commit completed;
Rollback completed;
[Link] some of the employees have given wrong information about date-of birth. Update
the corresponding tables to change the value.
21. Find the employees whose salary is between 5000 and 10000 but not exactly
7500.
SQL>select * from empt where ( sal between 1600 and 3000) and (sal ! =2975);
RESULT
VEMUIT Page 22
Department of CSE
RESULT:
select ename,empno en from empt where empno= 7369
Ename En
Smith 7369
VEMUIT Page 23
Department of CSE
Clark 2450
Blake 2850
Jones 2975
26. List the employees according to ascending order of salary in each department.
SQL>select emptname fromempt group by dept no order by sal ;
Select deptno min (sal) from empt group by deptno order by min (sal);
RESULT:
Dept no min (sal)
20 800
30 950
10 1300
27. Use ‘&&’ wherever necessary
SQL>
RESULT
28. Amount 6000 has to be deducted as CM relief fund in a particular month which has to be
accepted as input from the user. Whenever the salary becomes
negative it has to be maintained as 1000 and the deduction amount for those
employees is reduced appropriately
SQL> declare
Fund number (5): =6000;
Sal number (7): = 10000;
Month vocher (15) = ‘JAN’
Number (5) ;
begin
if ( month = ‘JAN’ ) then
If (sal,< ‘0’ ) then
Sal = 1000
else
x: = sal – 6000;
VEMUIT Page 24
Department of CSE
end if ;
end ;
Result:
29. The retirement age is 60 years. Display the retirement day of all the employees.
SQL>select exetract( day from add – months( hire - date , 720) ) from emp;
RESULT:
Extract (day from add – months ( hire- date , 720) )
17
20
22
2
29
1
9
9
17
8
12
3
3
23
30. If salary of all the employees is increased by 10% every year, what is the salary of all the
employees at retirement time.
SQL>
decare
Sal number (n): =20000
Dob date : = ‘ 12 –MAR – 1980 ‘
C :=extract (year from dob);
Pyearnumber(5):=2017;
X:=pyear-c;
Begin
While(x<=60)
VEMUIT Page 25
Department of CSE
loop
sal:=sal+((sal*10)/100);
x:=x+1;
end loop;
dbms_output.put_line(‘salary is ‘||sal);
end;
/
31. Find the employees who are born in leap year.
SQL>select * from empt where mod(extract(year from dob),4)=0;
33. Find the departments where the salary of atleast one employee is more than
20000.
SQL>select distinct(deptno) from empt where deptno in (select deptno from empt where
sal>20000);
RESULT:
Deptno
30
20
10
34. Find the departments where the salary of all the employees is less than 20000.
SQL>
select distinct(deptno) from empt where deptno in (select deptno from empt where sal<20000);
RESULT:
Deptno
VEMUIT Page 26
Department of CSE
30
20
10
35. On first January of every year a bonus of 10% has to be given to all the employees. The
amount has to be deducted equally in the next 5 months. Write procedures for it.
SQL>
Declare
Basic number(5):=10000;
Hiredate date:=’10-jan-70’;
C:= Extract(month from hiredate);
Begin
Basic:=basic+((basic*10)/100);
C:=c+1;
While(c>7)
Loop
Basic:=basic-((basic*2)/100);
C:=c+1;
End loop;
End;
/
36. As a designer identify the views that may have to be supported and create views.
SQL>
RESULT
VIVA QUESTIONS-1
1. What is DBMS?
VEMUIT Page 27
Department of CSE
2. What is DBA?
3. What is DDL?
4. What is DML?
5. What is Query?
6. What is Atomicity?
7. What is consistency?
8. What is constraints?
9. What is primary key?
[Link] is foreign key?
11. Difference between delete and drop command?
12. .Difference between commit and rollback?
[Link] of DBMS?
[Link] is file systems?
15. What is SQL?
[Link] of DBMS?
17. Difference between primary key and foreign key?
[Link] is a row?
[Link] is a column?
[Link] is the another name of rows and columns?
Practice of SELECT Query with various options
prctice of Views.
Other Practice Queries
VIVA QUESTIONS-2
1. What is the syntax of select ?
2. What is the groupby clause?
3. Why we go for where clause?
4. What is the usage of create?
5. What is the diiference between alter and update?
6. What is the syntax of del?
7. Define join ?
8. Types of joins?
VEMUIT Page 28
Department of CSE
9. Why we go for IN ?
10. Why we go for BETWEEN?
11. Difference between IN and NOTIN?
12. Syntax of DATE?
13. Difference between COUNT and DISTINCT COUNT?
14. What is relational algebra?
15. What is relational Calculus?
16. What is election?
17. What is projection?
18. Difference between DBMS and RDBMS?
19. What is Normalization?
20. What is Multivalued Dependencies?
[Link] understand the DBMS concepts. Students should gather the requiredinformation, draw
ER diagrams, map them to tables, normalize, create tables, triggers,procedures, execute queries,
create user interfaces, and generate reports.
1. Student information system:
2. APSRTC reservation system:
3. Hostel management:
4. Library management:
5. Indian Railways reservation:
[Link] market management:
7. Postal system:
8. Banking system:
9. Courier system:
10. Publishing house system:
VIVA QUESTIONS-3
1. What is the difference between DBMS and RDBMS?
2. What is mean by DDL Queries?
3. What are DML Queries?
4. What are the constrains are available in DBMS?
VEMUIT Page 29
Department of CSE
5. What is Entity?
6. What is relation?
7. What is weak entity?
8. What is strong entity?
9. Difference between weak and strong entity?
10. What is composition?
11. What is the usage of ER-diagram?
12. What is Trigger?
13. What is aggregation?
14. What is generalization?
15. What is dependency?
16. Types of normal forms?
17. What is the usage of integrity constraints?
18. What is Event?
19. What is Optimization?
20. What is Transaction?
21. What is ACID?
22. What is Decomposition?
23. What is Redundancy?
24. Syntax of OrderBy Clause?
25. What is relation? 26. What are the symbols used for E-R diagrams?
Practice of Triggers& Cursors
TRIGGERS
Triggers :
A data base trigger is a named PL/SQL block diagram in a data base and executed implicity
when a triggering event occurs the act of executing a trigger the is called firing the trigger. A
triggering can be one of the following
*A DML statement (such as insert, update, or DELETE) executed against a database table.
Such a trigger can be fire after a triggering exent for example if you have defined a triggering to
VEMUIT Page 30
Department of CSE
fire before an insert statement of the statement the students table this trigger fired each time before
you insert a row in the student table
DBMS They are age specified record various schema changes when they were made, and
by which user.
VEMUIT Page 31
Department of CSE
SQL>
Delare
i number;
j number ;
k number ;
begin
i: = &I;
j: = &j;
k: = i+j;
end;
Output:
Sum of 6 and 7= 12
2. Write a PL /SQL program to insert roll no from 15BF1A1245 to 15BF1A1249 (or) start
number to ending number
s number;
e number;
i number;
For i in s..e
loop
End for ;
VEMUIT Page 32
Department of CSE
end
N= &n;
For I in 1 .. 10 100p
End 100p;
End;
output:
8x1=8
8x2=16
8x3=24
8x4=32
8x5=40
8x6=48
8x7=56
8x8=64
8x9=72
8x10=80
VEMUIT Page 33
Department of CSE
SQL>Dec;are
Bs number;
Hra number;
Da number;
Gs number;
Dt number;
Ns number;
Begin
BS: = &bs;
Dt: = 200:
Da=bs *0.15;
Gs:=bs+hra +da;
Ns: da – dt;
End;
Result:
VEMUIT Page 34