Professional Documents
Culture Documents
NAME :_________________________
RRN :_________________________
PROGRAMME :_________________________
RRN: of
Master of Computer Applications during I semester for the course CAE 6128 -
Database management system Laboratory during the academic year 2023 - 2024.
Date:
Signature of Faculty-In-Charge
4 In Built Functions
6 Set operators
7 Views
11 Exception handling
Ex No: 1
Date :
AIM:
To practice and implement data definition language commands and constraints.
Output:
Table created.
Q2: Add a column experience to the emp table. experience numeric null allowed.
Output:
Table altered.
Q3: Modify the column width of the job field of emp table.
Output:
Table altered.
Output :
Table altered.
Output:
Table created.
1
Q5: create the emp1 table with ename and empno, add constraints to check the
empno value while entering (i.e) empno > 100.
Output:
Table created.
Output:
Table altered.
Q7: Truncate the emp table and drop the dept table
Output:
Table truncated.
Result:
Thus the data definition language commands was performed and executed
successfully.
2
Ex No: 2
Date :
AIM:
To study the various DML commands and implement them on the database.
Q2: Insert more than a record into emp table using a single insert command.
Q3: Update the emp table to set the salary of all employees to Rs15000/- who are
working as ASP.
Output:
-
1 Mathi AP 1 10000
3
2 Arjun ASP 2 12000
3 Gugan ASP 112000
Output:
-
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 115000
Q4: Create a pseudo table employee with the same structure as the table emp and
insert rows into the table using select clauses.
desc employee;
Output:
Name Null? Type
EMPNO NUMBER(6)
ENAME NOT NULL VARCHAR2(20)
JOB NOT NULL VARCHAR2(13)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Output:
ENAME JOB
Mathi AP
Arjun ASP
4
Gugan ASP
Karthik Prof
Akalya AP
suresh lect
6 rows selected.
Q6: Delete only those who are working as lecturer.
Output:
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
6 suresh lect 1 8000
Output:
1 row deleted.
Output:
EMPNO ENAME JOB DEPTNO SAL
10000
1 Mathi AP 1
15000
2 Arjun ASP 2
15000
3 Gugan ASP 1
30000
4 Karthik Prof 2
10000
5 Akalya AP 1
5
Q7: List the records in the emp table orderby salary in ascending order.
Output:
EMPNO ENAME JOB DEPTNO SAL
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q8: List the records in the emp table orderby salary in descending order.
Output:
EMPNO ENAME JOB DEPTNO SAL
Output:
1 Mathi AP 10000
3 Gugan ASP
5 Akalya AP 10000
6
Q10: Display deptno from the table employee avoiding the duplicated values.
DEPTNO
1
2
Result:
Thus the DML commands using from where clause was performed and executed successfully.
7
Ex No: 3
Date :
AIM:
To study the various data language commands (DCL, TCL) and implements them on the
database.
Tables Used:
Consider the following tables namely “DEPARTMENTS” and “EMPLOYEES”
Their schemas are as follows ,
Departments ( dept _no , dept_ name , dept_location );
Employees ( emp_id , emp_name , emp_salary );
Q1: Develop a query to grant all privileges of employees table into departments
table.
Output:
Grant succeeded.
Q2: Develop a query to grant some privileges of employees table into departments
table.
Output:
Grant succeeded.
Q3: Develop a query to revoke all privileges of employees table from departments table
Output:
Revoke succeeded.
8
Q4: Develop a query to revoke some privileges of employees table from departments table
Output:
Revoke succeeded.
SAVEPOINT S1;
Output:
Savepoint created.
Output:
EMPNO ENAME JOB DEPTNO SAL
1 MATHI AP 1 10000
2 ARJUN ASP 2 15000
3 GUGAN ASP 1 15000
4 KARTHIIK PROF 2 30000
Output:
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
9
Q6: Write a query to implement the rollback
rollback s1;
select * from emp;
Output:
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
COMMIT;
Output:
Commit complete.
Result:
The DCL,TCL commands was performed and executed successfully.
10
Ex No: 4
Date :
IN BUILT FUNCTIONS
AIM:
To perform nested Queries and joining Queries using DML command.
Q1: Display all the details of the records whose employee name starts with ‘A’.
Q2: Display all the details of the records whose employee name does not starts with ‘A’.
select * from emp where ename not like 'A%';
Output:
1 Mathi AP 1 10000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q3: Display the rows whose salary ranges from 15000 to 30000.
Output:
-
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
11
Q4: Calculate the total and average salary amount of the emp table.
Output:
SUM(SAL) AVG(SAL)
80000 16000
Output:
EMPNO ENAME JOB DEPTNO SAL
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 115000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
Q6: Determine the max and min salary and rename the column as max_salary and
min_salary.
Output:
MAX_SALARY MIN_SALARY
30000 10000
12
Q8: Display the last day of that month in “05-Oct-09”.
Select last_day ('1-jun-2009') from dual;
Output:
LAST_DAY(
30-JUN-09
Q9: Find how many job titles are available in employee table.
select count(job) from emp;
Output:
COUNT(JOB)
Output:
COUNT(DISTINCTJOB)
Q10: What is the difference between maximum and minimum salaries of employees in the
organization?
Output:
MAX(SAL) MIN(SAL)
20000 10000
Result:
Thus the nested Queries and join Queries was performed and executed successfully.
13
Ex No: 5
Date :
Q1: Display all employee names and salary whose salary is greater than minimum salary of
the company and job title starts with ‘M’.
select ename,sal from emp where sal>(select min(sal) from emp where job like 'A%');
Output:
ENAME SAL
Arjun 12000
Gugan 20000
Karthik 15000
Q2: Issue a query to find all the employees who work in the same job as
Arjun.
Output:
EMPNO ENAME
1 Mathi AP
2 Arjun ASP
3 Gugan ASP
4 Karthik AP
Output:
ENAME
Arjun
Gugan
14
Q3: Issue a query to display information about employees who earn more than any
employee in dept 1.
Ans:
SQL> select * from emp where sal>(select max(sal) from emp where empno=1);
JOINS
Tables used
SQL> select * from emp;
1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 220000
4 Karthik AP 1 15000
EQUI-JOIN
Q4: Display the employee details, departments that the departments are same in both the
emp and dept.
Solution:
1. Use select from clause. 2. Use equi join in select clause to get the result.
Ans:
SQL> select * from emp,dept where emp.deptno=dept.deptno;
15
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
--
-
1000
1 Mathi AP 1 0 1 ACCOUNTING NEW YORK
1200
2 Arjun ASP 2 0 2 RESEARCH DALLAS
2000
3 Gugan ASP 2 0 2 RESEARCH DALLAS
1500
4 Karthik AP 1 0 1 ACCOUNTING NEW YORK
Q5: Display the employee details, departments that the departments are not same in both
the emp and dept.
Solution:
1.Use select from clause. 2. Use non equi join in select clause to get the result.
Ans:
SQL> select * from emp,dept where
emp.deptno!=dept.deptno;
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
LEFTOUT-JOIN
Tables used
SQL> select * from stud1;
Regno Name Mark2 Mark3 Result
- -
101 john 89 80 pass
102 Raja 70 80 pass
103 Sharin 70 90 pass
104 sam 90 95 pass
SQL> select * from stud2;
NAME GRA
john s
raj s
sam a
sharin a
Q6: Display the Student name and grade by implementing a left outer join.
Ans: SQL> select stud1.name,grade from stud1 left outer join stud2 on stud1.name=stud2.name;
Name Gra
john s
raj s
sam a
sharin a
smith null
RIGHTOUTER-JOIN
Q7: Display the Student name, register no, and result by implementing a right outer join.
Ans:
SQL> select stud1.name, regno, result from stud1 right outer join stud2 on stud1.name =
stud2.name;
Name Regno Result
17
FULLOUTER-JOIN
Q8: Display the Student name register no by implementing a full outer join.
Ans:
SQL> select stud1.name, regno from stud1 full outer join stud2 on (stud1.name= stud2.name);
Name Regno
john 101
raj 102
sam 103
sharin 104
SELFJOIN
Q10: Display the details of those who draw the salary greater than the average salary.
Ans:
SQL> select distinct * from emp x where x.sal >= (select avg(sal) from emp);
EMPNO ENAME JOB DEPTNO SAL
Result:
Thus the nested Queries and join Queries was performed and executed successfully.
18
Ex No: 6
Date :
SET OPERATORS
AIM:
To perform set operations using DML Commands
Q1: Display all the dept numbers available with the dept and emp tables
avoiding duplicates.
Solution:
1. Use select from clause. 2. Use union select clause to get the result.
Ans:
SQL> select deptno from emp union select deptno from dept;
DEPTNO
1
2
12
30
40
Q2: Display all the dept numbers available with the dept and emp tables.
Solution:
1. Use select from clause. 2. Use union all in select clause to get the result.
Ans:
SQL> select deptno from emp union all select deptno from dept;
DEPTNO
1
2
2
1
12
1
2
30
40
9 rows selected.
19
Q3: Display all the dept numbers available in emp and not in dept tables and vice versa.
Solution:
1. Use select from clause.
2. Use minus in select clause to get the result.
Ans:
SQL> select deptno from emp minus select deptno from dept;
DEPTNO
12
SQL> select deptno from dept minus select deptno from emp;
DEPTNO
30
40
Result:
Thus the set operations using DML Commands was performed and executed successfully.
20
Ex No: 7
Date :
VIEWS
AIM:
To create and manipulate various database objects of the Table using views
Q1: The organization wants to display only the details of the employees those who are
ASP. (Horizontal portioning)
Solution:
1. Create a view on emp table named managers
2. Use select from clause to do horizontal partioning
Ans:
SQL> create view empview as select * from emp where job='ASP';
View created.
SQL> select * from empview;
EMPNO ENAME JOB DEPTNO SAL
120
2 Arjun ASP 2 00
200
3 Gugan ASP 2 00
Q2: The organization wants to display only the details like empno, empname,
deptno, deptname of the employees. (Vertical portioning)
Solution:
1. Create a view on emp table named general 2. Use select from clause to do vertical
partioning
Ans:
SQL> create view empview1 as select ename,sal from emp; View
created.
Q3: Display all the views generated.
Ans:
SQL> select * from tab;
Q4: Execute the DML commands on the view
created.
Ans:
SQL> select * from empview;
JO SA
EMPNO ENAME B DEPTNO L
21
Q5: Drop a view.
Ans: SQL> drop view empview1;
View dropped.
Result:
Thus the creation and manipulate various database objects of the table using views
was executed successfully.
22
Ex No: 8
Date :
AIM:
Creating a View
PARTITIONING
PROD_ID CUST_ID TIME_ID CHANNEL_ID PROMO_ID QUANTITY_SOLD
AMOUNT_SOLD
23
You create time_range_sales as a partitioned table using the statement in the following
example. The time_id column is the partition key.
Locks
UNLOCK TABLES;
Result:
Thus views, partitioning and locks was performed and executed successfully.
24
Ex No:9
Date :
AIM:
1. DECLARE
2. n3 number(2);
3. BEGIN
4. n3 := adder(11,22);
5. dbms_output.put_line('Addition is: ' || n3);
6. END;
7. /
25
Output:
Addition is: 33
Statement processed.
0.05 seconds
Let's take an example to demonstrate Declaring, Defining and Invoking a simple PL/SQL
function which will compute and return the maximum of two values.
1. DECLARE
2. a number;
3. b number;
4. c number;
5. FUNCTION findMax(x IN number, y IN number)
6. RETURN number
7. IS
8. z number;
9. BEGIN
10. IF x > y THEN
11. z:= x;
12. ELSE
13. Z:= y;
14. END IF;
15.
16. RETURN z;
17. END;
18. BEGIN
19. a:= 23;
20. b:= 45;
21.
22. c := findMax(a, b);
23. dbms_output.put_line(' Maximum of (23,45): ' || c);
24. END;
25. /
Output:
Maximum of (23,45): 45
Statement processed.
0.02 seconds
26
Create Function:
1. CREATE OR REPLACE FUNCTION totalCustomers
2. RETURN number IS
3. total number(2) := 0;
4. BEGIN
5. SELECT count(*) into total
6. FROM customers;
7. RETURN total;
8. END;
9. /
After the execution of above code, you will get the following result.
Function created.
1. DECLARE
2. c number(2);
3. BEGIN
4. c := totalCustomers();
5. dbms_output.put_line('Total no. of Customers: ' || c);
6. END;
7. /
After the execution of above code in SQL prompt, you will get the following result.
Create procedure:
Execute the following program to retrieve the customer name and address.
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
BEGIN
27
OPEN c_customers;
LOOP
END LOOP;
CLOSE c_customers;
END;
Output:
Ramesh Allahabad
Suresh Kanpur
Mahesh Ghaziabad
Chandan Noida
Alex Paris
Sunita Delhi
PL/SQL procedure successfully completed.
Result:
28
Ex No: 10
Date :
CURSOR(IMPICIT AND EXPLICIT)
AIM:
Let's execute the following program to update the table and increase salary of each customer
by 5000. Here, SQL%ROWCOUNT attribute is used to determine the number of rows
affected:
Create procedure:
1. DECLARE
2. total_rows number(2);
3. BEGIN
4. UPDATE customers
5. SET salary = salary + 5000;
6. IF sql%notfound THEN
7. dbms_output.put_line('no customers updated');
8. ELSIF sql%found THEN
9. total_rows := sql%rowcount;
10. dbms_output.put_line( total_rows || ' customers updated ');
11. END IF;
12. END;
13. /
Output:
6 customers updated
PL/SQL procedure successfully completed.
Now, if you check the records in customer table, you will find that the rows are updated.
29
1. select * from customers;
The Explicit cursors are defined by the programmers to gain more control over the context
area. These cursors should be defined in the declaration section of the PL/SQL block. It is
created on a SELECT statement which returns more than one row.
Steps:
You must follow these steps while working with an explicit cursor.
It defines the cursor with a name and the associated SELECT statement.
1. CURSOR name IS
2. SELECT statement;
30
2) Open the cursor:
It is used to allocate memory for the cursor and make it easy to fetch the rows returned by the
SQL statements into it.
1. OPEN cursor_name;
3) Fetch the cursor:
It is used to access one row at a time. You can fetch rows from the above-opened cursor as
follows:
It is used to release the allocated memory. The following syntax is used to close the above-
opened cursors.
1. Close cursor_name;
Explicit cursors are defined by programmers to gain more control over the context area. It is
defined in the declaration section of the PL/SQL block. It is created on a SELECT statement
which returns more than one row.
Let's take an example to demonstrate the use of explicit cursor. In this example, we are using
the already created CUSTOMERS table.
31
Result:
32
Ex No: 11
Date :
EXCEPTION HANDLING(TRIGGERS)
AIM:
1. DECLARE
2. c_id customers.id%type := 8;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;
33
OUTPUT:
No such customer!
PL/SQL procedure successfully completed.
1. DECLARE
2. c_id customers.id%type := 5;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;
OUTPUT:
Name: alex
Address: paris
PL/SQL procedure successfully completed.
Let's take a simple example to demonstrate the trigger. In this example, we are using the
following CUSTOMERS table:
34
Create trigger:
Let's take a program to create a row level trigger for the CUSTOMERS table that would fire
for INSERT or UPDATE or DELETE operations performed on the CUSTOMERS table. This
trigger will display the salary difference between the old values and new values:
After the execution of the above code at SQL Prompt, it produces the following result.
Trigger created.
Result:
Thus triggers using SQL commands are implemented and executed successfully.
35