DBMS Lab Manual
DBMS Lab Manual
COURSE OUTCOMES:
After successful completion of the course, students will be able to:
INDEX
3 Insertion of Records 4
4 Updation of Records 5
10 Subqueries 16
11 Joins 17-19
17 Cursors 31
18 Triggers 31
Example:
CREATE TABLE student ( sno NUMBER,
Sname VARCHAR2(20), fee NUMBER,
Saddress VARCHAR2(20)
);
Example:
DESCRIBE student;
Example:
1
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ALTER TABLE Student ADD (phno NUMBER);
Describe:
Syntax:
DESCRIBE table_name;
Example:
DESCRIBE student;
Example:
ALTER TABLE Student
MODIFY (Saddress VARCHAR2(30));
Describe:
Syntax:
DESCRIBE table_name;
Example:
DESCRIBE Student;
Describe:
2
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Syntax:
DESCRIBE table_name;
Example:
DESCRIBE Student;
03. Aim: To insert records into a table using Data Manipulation Language.
3
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example:
INSERT INTO Student
VALUES (01, 'Koushik', 25000, 'CTR');
● Session 2:
Enter Sno: 03 Enter Sname: Siddu Enter fee: 20000
Enter saddress: Kadiri
/ (Indicates end of input for this record)
● Session 3:
Enter Sno: 04 Enter Sname: Manju Enter fee: 10000 Enter saddress: MPL
Example:
SELECT * FROM Student;
Program:
Update:
Syntax:
UPDATE table_name
4
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
SET column_name = value
WHERE condition;
Example:
UPDATE Student
SET Saddress = 'KDP'
WHERE Sno = 01;
Example:
SELECT * FROM Student;
05. Aim: To retrieve the data from the database using SELECT statement.
Program:
1. Selecting all columns:
Syntax:
SELECT * FROM table_name;
Example:
SELECT * FROM Student;
5
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Example:
SELECT Sno, Sname, Saddress FROM Student;
Example:
SELECT * FROM Student
WHERE Fee > 15000;
Example Output for SELECT * FROM Student WHERE Fee > 15000;
Group By Clause:
Syntax:
6
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
SELECT column_list
FROM table_name
WHERE <condition>
[GROUP BY <column>];
Example:
SELECT deptno, SUM(Salary)
FROM employee
GROUP BY deptno;
Output:
Deptno SUM(Salary)
20401 80000
20403 25000
20402 20000
Having Clause:
Syntax:
SELECT column, group_function(column)
FROM table_name
[WHERE <condition>]
[GROUP BY <column>]
[HAVING group_condition];
Example:
SELECT deptno, COUNT(deptno)
FROM employee
GROUP BY deptno
HAVING deptno > 20401;
Output:
Deptno COUNT(deptno)
20403 1
20402 1
07: From Querying the table using Clause like WHERE, ORDER BY, SAL, FWD, OR, NOT, IS
NULL
WHERE Clause:
Output:
ORDER BY Clause:
Syntax: SELECT [column1, column2, ...] from Table name [WHERE condition] [ORDER BY column
name [ASC/DESC]]
Output:
Logical AND:
Syntax: SELECT * from tablename WHERE column1 = value1 AND column2 = value2;
Example: SELECT * from employee WHERE ename = 'Payal' AND salary > 3000;
Output:
Logical OR:
Output:
IS NULL Clause:
Output:
8
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ENO ENAME SALARY COMMISSION
1 Amit 3000
4 Payal 4000
Program:
Table 1:
ID NAME
1 Abhi
2 Adam
Table 2:
ID NAME
2 Adam
3 Chester
Union:
Syntax:
Output:
ID NAME
1 Abhi
2 Adam
3 Chester
Union All:
Syntax:
Select * from tablename
Union All
Select * from tablename;
Example:
Select * from table1
Union All
Select * from table2;
Output:
ID NAME
1 Abhi
2 Adam
2 Adam
3 Chester
Intersect:
Syntax:
Select * from tablename
Intersect
Select * from tablename;
Example:
Select * from table1
Intersect
Select * from table2;
Output:
ID NAME
2 Adam
10
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Minus:
Syntax:
Select * from tablename
Minus
Select * from tablename;
Example:
Select * from table1
Minus
Select * from table2;
Output:
ID NAME
10 Abhi
09 Aim: Working with numbers, characters, dates & aggregate functions with in sql.
Programs on Character functions:
1.Lower()
Syntax: Lower(string);
Example: Select lower('HARI') from dual;
Output: hari
2.Upper()
Syntax: Upper(string);
Example: Select upper('Hari') from dual;
Output: HARI
3.Initcap()
Syntax: Initcap(string);
Example: Select initcap('hari') from dual;
Output: Hari
4.Concat()
Syntax: Concat(string1, string2);
Example: Select concat('Guna', 'shekar') from dual;
Output: Gunashekar
5.Length()
Syntax: Length(string);
Example: Select length('Hari') from dual;
Output: 4
11
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
6.Replace()
Syntax: Replace(string, old-string, new-string);
Example: Select replace('SV Academy', 'SV', 'Hari') from dual;
Output: Hari Academy
7.Translate()
Syntax: Translate(string, characters, translation);
Example: Select translate('cold', 'o', '1') from dual;
Output: cool
Number functions or mathematical functions:
1.Abs()
Syntax: Abs(n);
Example: Select abs(-10.8) from dual;
Output: 10.8
2.Sqrt()
Syntax: Sqrt(n);
Example: Select sqrt(4) from dual;
Output: 2
3.Mod()
Syntax: Mod(m, n);
Example: Select mod(4, 2) from dual;
Output: 0
4.Power()
Syntax: Power(x, y);
Example: Select power(2, 3) from dual;
Output: 8
5.Cos()
Syntax: Cos(n);
Example: Select cos(0) from dual;
Output: 1
6.Sin()
Syntax: Sin(n);
Example: Select sin(4) from dual;
Output: 1
7.Round()
Syntax: Round(n, d);
Example: Select round(16.791, 2) from dual;
Output: 16.79
8.Ceil()
Syntax: Ceil(n);
Example: Select ceil(15.20) from dual;
Output: 16
Date functions:
1.Sysdate:
Syntax: Sysdate;
Example: Select Sysdate from dual;
Output: (Current date and time)
12
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
2.Round (Sysdate):
Syntax: Round(Sysdate);
Example: Select round(Sysdate) from dual;
Output: (Current date rounded to the nearest day)
3.Add_months (Sysdate, n):
Syntax: Add_months(Sysdate, n);
Example: Select add_months(Sysdate, 2) from dual;
Output: (Date after adding n months)
4.Last_day (Sysdate):
Syntax: Last_day(Sysdate);
Example: Select last_day(Sysdate) from dual;
Output: (Last day of the current month)
5.Next_day (Sysdate, day):
Syntax: Next_day(Sysdate, day);
Example: Select next_day(Sysdate, 'Friday') from dual;
Output: (Date of the next specified day of the week)
1. Avg():
Syntax: Select Avg(Columnname) from tablename;
Example: Select avg(salary) from employee;
Output: 26250
2. Count():
Syntax: Select Count(Columnname) from tablename;
Example: Select Count(salary) from employee;
Output: 4
3. Max():
Syntax: Select max(columname) from tablename;
Example: Select max(salary) from employee;
Output: 40000
13
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
4. Min():
Syntax: Select min(columnmame) from tablename;
Example: Select min(salary) from employee;
Output: 15000
5. Sum():
Syntax: Select Sum(columnname) from tablename;
Example: Select Sum(salary) from employee;
Output: 105000
Program:
Subquery Syntax:
Select ... where ... (Select ... where ...);
↓ ↓
Outer Query Inner Query
Employee Table:
SQL Query:
Select dept from employee
where Salary = (select max(salary) from employee);
Output:
DEPT
IT
14
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
11 Aim: Working with joins in SQL
Program:
Employee 1 Table:
Employee 2 Table:
Inner Join:
Output:
15
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ename job salary phno address
Anand Admin 15000 1234 CTR
Madhu Marketing 12000 4321 TPT
Srinivas Production 13000 3421 Bang
Anjan HR 16000 4213 Chennai
Koushik Admin 15000 1342 Mumbai
Prudhvi Marketing 12000 2431 Delhi
Yuva HR 16000 1432 Kerala
Output:
Output:
16
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
ename job salary phno address
Yuva HR 16000 1432 Kerala
Union:
Syntax: Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno(+) =
employee2.eid;
Union
Select ename, job, salary, phno, address from employee1, employee2 where employee1.eno =
employee2.eid(+);
Output:
17
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
12.Aim: Program on Integrity Constraints
1. NOT NULL
Example:
CREATE TABLE employee (
emp_id NUMBER NOT NULL,
emp_name VARCHAR(30) NOT NULL,
caddress VARCHAR(40),
ephone NUMBER UNIQUE
);
2. UNIQUE
Example:
3. PRIMARY KEY
Example:
18
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
4. CHECK
Example:
INSERT INTO Voter VALUES ('Kumar', 'K', 10); -- This will violate the CHECK constraint
5. FOREIGN KEY
Example:
19
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
13.Aim: Implementation of Control Statement Using PL/SQL Decision making Statement.
1. IF...THEN
Syntax:
IF condition THEN
Statement(s);
END IF;
Example:
DECLARE
n NUMBER := 10;
BEGIN
IF n > 0 THEN
DBMS_OUTPUT.PUT_LINE('Positive number');
END IF;
END;
/
Output:
Positive number
PL/SQL procedure successfully executed.
2. IF...THEN...ELSE
Syntax:
IF condition THEN
Statement(s) 1;
ELSE
Statement(s) 2;
END IF;
Example:
DECLARE
n NUMBER := -5;
BEGIN
IF n > 0 THEN
DBMS_OUTPUT.PUT_LINE('Positive number');
ELSE
DBMS_OUTPUT.PUT_LINE('Negative number');
END IF;
END;
/
Output:
Negative number
PL/SQL procedure successfully executed.
20
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Another Example (Checking for even/odd):
DECLARE
n NUMBER := 7;
BEGIN
IF MOD(n, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(n || ' is an even number.');
ELSE
DBMS_OUTPUT.PUT_LINE(n || ' is an odd number.');
END IF;
END;
/
Output:
7 is an odd number.
PL/SQL procedure successfully executed.
3. IF...THEN...ELSE …IF
Syntax:
IF condition 1 THEN
Statement(s) 1;
ELSIF condition 2 THEN
Statement(s) 2;
[ELSIF condition 3 THEN
Statement(s) 3;] -- Optional additional ELSIF clauses
...
ELSE
Statement(s) n; -- Optional ELSE clause
END IF;
Example:
DECLARE
S1 NUMBER;
S2 NUMBER;
S3 NUMBER;
S4 NUMBER;
BEGIN
S1 := &S1;
S2 := &S2;
S3 := &S3;
S4 := &S4;
Output:
Enter value for S1: 45
Enter value for S2: 54
Enter value for S3: 34
Enter value for S4: 85
Promoted
PL/SQL procedure successfully executed.
22
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
14.Aim: Implementation of Control Statement Using PL/SQL Looping Statements.
● Basic loop
● FOR loop
● WHILE loop
1. Basic Loop:
Syntax:
LOOP
Statement(s);
EXIT WHEN condition;
END LOOP;
Example:
DECLARE
A NUMBER;
BEGIN
A := 4;
LOOP
DBMS_OUTPUT.PUT_LINE(A);
A := A + 1;
EXIT WHEN A > 10;
END LOOP;
END;
/
Output:
4
5
6
7
8
9
10
PL/SQL procedure successfully executed.
2. WHILE Loop:
Syntax:
Example:
DECLARE
A NUMBER;
BEGIN
A := 2;
WHILE A <= 20 LOOP
DBMS_OUTPUT.PUT_LINE(A);
A := A + 2;
END LOOP;
END;
/
Output:
2
4
6
8
10
12
14
16
18
20
PL/SQL procedure successfully executed.
.
4. FOR Loop:
Syntax:
Example:
DECLARE
A NUMBER;
BEGIN
A := 2; -- Although A is initialized here, the FOR loop re-declares and uses its own loop variable A.
FOR A IN 2..20 LOOP -- Loop from 2 to 20
DBMS_OUTPUT.PUT_LINE(A);
END LOOP;
END;
/
24
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Output:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PL/SQL procedure successfully executed.
25
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
15.Aim: Program on DDL Commands using PL/SQL
Program:
Insert:
Procedure created.
SQL> execute add_student();
Output:
Sno Sname Saddress
01 Hari Chittoor
Update:
Procedure Created.
SQL> execute updt_student();
Output:
26
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Sno Sname Saddress
01 Hari Tirupathi
Delete:
Procedure Created.
SQL> execute del_student();
Output:
No rows selected.
27
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
16. Aim: Creating a procedure Using parameters.
Program:
Procedure Created.
SQL> execute Updt ('Tirupathi', 01);
Output:
28
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
17 Aim: Working with Cursors in PL/SQL
Program:
Employee Table:
Declare
Cursor C is Select ename, sal from employee;
Vename employee.ename%type;
Vsal employee.sal%type;
begin
Open C;
loop
fetch c into Vename, Vsal;
exit when c%notfound;
dbms_output.put_line(Vename);
dbms_output.put_line(Vsal);
end loop;
Close C;
end;
/
Output:
Joseph
15000
Jenny
10000
George
12000
Francis
13000
29
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
18 Aim: Creating a program on Trigger
Program:
Syntax:
Example:
Student Table:
Declare
-- No declarations needed for this simple trigger example
begin
-- No executable statements in this simple trigger example
end;
/
Trigger created.
SQL> update Student
Set Saddress = ‘Chennai’
where Sno=03;
30
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA
Output:
Record is updated
31
Prepared by: B. Sai Hemanth,
Assistant Professor, Dept. Of MCA