Final DBMS Lab Record
Final DBMS Lab Record
Name: PinNo:
CERTIFICATE
Mr./Ms.
a Student of with PinNo:
in the Laboratory during the Academic year
Examiner–1 Examiner – 2
DATABASE MANAGEMENT SYSTEMS LAB
INDEX
Exp
Date Name of the Experiment Page Signature Marks
No
No.
06/11/2021 Creation, altering and dropping of tables and
1 inserting rows into a table (use constraints while
creating tables) examples using select command.
13/11/2021 Queries (along with sub queries) using any, all,
2 in, exists, notexists, union, interset, constraints.
20/11/2021 Queries using aggregate functions (count, sum,
3 avg, max and min), group by, having and creation
and dropping of views.
27/11/2021 Queries using conversion functions (to_char,
4 to_number and to_date), string functions
(concatenation, lpad, rpad, ltrim, rtrim, lower,
upper, initcap, length, substr and instr), date
functions (sysdate, next_day, add_months,
last_day, months_between, least, greatest, trunc,
round, to_char, to_date)
11/12/2021 Creation of simple pl/sql program which includes
5 declaration section, executable section and
exception –handling section
08/01/2022 Insert data into student table and use commit,
6 rollback and savepoint in pl/sql block.
AIM: Aim of the experiment is to CREATE, ALTER and DROP the tables and INSERT rows
into a table.
CREATE SYNTAX:
CREATE TABLE TABLE_NAME (column1datatype,column2 datatype,column3 datatype,....);
CREATE EXAMPLE:
CREATE TABLE STUDENTS(pin int not null,lastname varchar(25),firstname varchar(25),
phone_no int not null, email varchar(25) not null);
DROP SYNTAX:
DROP TABLE TABLE_NAME;
DROP EXAMPLE:
DROP TABLE STUDENTS;
CREATE TABLE STUDENTS(pin int not null, lastname varchar(25), firstname varchar(25),
phone_no int not null, email varchar(25) not null);
INSERT SYNTAX:
INSERT INTO TABLE_NAME VALUES (column1 data,column2 data,.....columnN data);
INSERT EXAMPLE:
INSERT INTO STUDENTS VALUES (1001,'xyz','abc',12346789,'google@gmail.com');
INSERT INTO STUDENTS VALUES (1002,'yxz','bac',21346789,'ogogle@gmail.com');
ALTER SYNTAX:
ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE;
ALTER EXAMPLE:
ALTER TABLE STUDENTS ADD GRADE VARCHAR(20);
SELECT SYNTAX:
SELECT * FROM table_name;
SELECT EXAMPLE:
SELECT * FROM STUDENTS;
Every time you can check the data in the database table using the querySELECT * FROM
TABLE_NAME; and the structure/description of the table can be seen using DESC
TABLE_NAME;
AIM: Aim of the experiment is to illustrate ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, Constraints along with Sub-Queries
DESCRIPTION OF THE OPERATORS:
ALL: The ALL operator is used to compare a value to all values in another value set.
AND: The AND operator allows the existence of multiple conditions in ansql statement's where
clause.
ANY: The ANY operator is used to compare a value to any applicable value in the list as per the
condition.
EXISTS:The EXISTS operator is used to search for the presence of a row in a specified table
that meets a certain criterion.
IN: The IN operator is used to compare a value to a list of literal values that have been specified.
UNION: The UNION is a binary set operator in DBMS. It is used to combine the result set of
two select queries.
INTERSECT:INTERSECT is a binary set operator in DBMS. The intersection operation
between two selections returns only the common data sets or rows between them.
COONSTARINTS:CONSTRAINTS enforce limits to the data or type of data that can
beinserted/updated/deleted from a table.
EMPLOYEES TABLE:
CREATE TABLEEMPLOYEES(idint,namevarchar(100),ageint,address varchar(100),basic
int,primary key(id));
Insertion of rows/data in to the created table is as follows:
INSERT INTO EMPLOYEES VALUES (1,'Rakesh',30,'Eluru',15658.00);
INSERT INTO EMPLOYEES VALUES (2,'Krishna',45,'Eluru ',35480.00);
INSERT INTO EMPLOYEES VALUES (3,'Kaushik',23,'Kotak ',8000.00);
INSERT INTO EMPLOYEES VALUES (4,'Chandu',30,'Amalapuram ',33568.00);
INSERT INTO EMPLOYEES VALUES (5,'Harish',27,'Kakinada',48500.00);
INSERT INTO EMPLOYEES VALUES (6,'Satya',22,'Murari',42500.00);
INSERT INTO EMPLOYEES VALUES (7,'Murali',24,'Rajamahendravaram',45845.00);
INSERT INTO EMPLOYEES VALUES (8,'Mastan',48,'Indore',15584.00);
CUSTOMERS TABLE:
CREATE TABLE CUSTOMERS(ID INT,NAME VARCHAR(100),AGE INT,ADDRESS
VARCHAR(100),SALARY INT);
Insertion of rows/data in to the created table is as follows:
INSERT INTO CUSTOMERS VALUES (1,'Ramesh',32,'Ahmedabad',2000.00);
INSERT INTO CUSTOMERS VALUES (2,'Khilan',25,'Delhi ',1500.00);
INSERT INTO CUSTOMERS VALUES (3,'kaushik',23,'Kota ',2000.00);
INSERT INTO CUSTOMERS VALUES (4,'Chaitali',25,'Mumbai ',6500.00);
INSERT INTO CUSTOMERS VALUES (5,'Hardik',27,'Bhopal',8500.00);
INSERT INTO CUSTOMERS VALUES (6,'Komal',22,'MP',4500.00);
INSERT INTO CUSTOMERS VALUES (7,'Muffy',24,'Indore',10000.00);
INSERT INTO CUSTOMERS VALUES (8,'Mastan',28,'Indore',20000.00);
Q:DISPLAY THE NAMES OF THE EMPLOYEES WHO ARE NOT FROM HARISH'S
WORK PLACE;
A:SELECT NAME FROM EMPLOYEES
WHERE ADDRESS! = (SELECT ADDRESS FROM EMPLOYEES WHERE NAME =
'Harish');
II. ALL:
Q:DISPLAY ALL THE DETAILS OF THE EMPLOYEES WHERE AGE OF AN
EMPLOYEE IN EMPLOYEE TABLE IS GREATER THAN AGE OF ALL
CUSTOMERS IN CUSTOMERS TABLE
A:SELECT * FROM EMPLOYEES
WHERE AGE > ALL (SELECT AGE FROM CUSTOMERS);
III. ANY:
Q:DISPLAY ALL THE DETAILS OF THE EMPLOYEES WHERE AGE OF AN
EMPLOYEE IN EMPLOYEE TABLE IS GREATER THAN AGE OF ANY
CUSTOMERS IN CUSTOMERS TABLE AND SALARY OF AN CUSTOMERS
SHOULD BE GREATER THAN 5000;
A:SELECT * FROM EMPLOYEES
WHERE AGE > ANY (SELECT AGE FROM CUSTOMERS WHERE SALARY>5000);
IV. EXISTS:
Q:CHECK WHETHER THE AGE OF AN EMPLOYEE IN EMPLOYEE TABLE IS
PRESENT IN THE AGE COLUMN OF CUSTOMERS TABLE OR NOT, IF YES THEN
DISPLAY THAT EMPLOYEE AGE DETAILS.
A:SELECT AGE FROM EMPLOYEES
WHERE NOT EXISTS (SELECT AGE FROM CUSTOMERS);
NO ROWS SELECTED.
V. IN:
Note that NOT operator can be used as negation to the existing operator like EXISTS, IN,
LIKE, NULL.
VI. UNION:
SQL> select salary from customers union select salary from employees;
SALARY
----------
1500
2000
4500
6500
8000
8500
10000
15584
15658
20000
33568
SALARY
----------
35480
42500
45845
48500
15 rows selected.
VII. INTERSECTION
SQL> select address from customers intersect select address from employees;
ADDRESS
-------------------------
Indore
AIM: Aim of the experiment is to illustrate the Aggregate functions -COUNT, SUM, AVG,
MAX and MIN, GROUP BY, HAVING and Creation and dropping of Views.
CUSTOMERS TABLE :
CREATE TABLE CUSTOMER_S(ID INT,NAME VARCHAR(100),AGE INT,ADDRESS
VARCHAR(100),SALARY INT);
INSERT INTO CUSTOMER_S VALUES (1,'Ramesh',32,'Ahmedabad',2000.00);
INSERT INTO CUSTOMER_S VALUES (2,'Khilan',25,'Delhi ',1500.00);
INSERT INTO CUSTOMER_S VALUES (3,'kaushik',23,'Kota ',2000.00);
INSERT INTO CUSTOMER_S VALUES (4,'Chaitali',25,'Mumbai ',6500.00);
INSERT INTO CUSTOMER_S VALUES (5,'Hardik',27,'Bhopal',8500.00);
INSERT INTO CUSTOMER_S VALUES (6,'Komal',22,'MP',4500.00);
INSERT INTO CUSTOMER_S VALUES (7,'Muffy',24,'Indore',10000.00);
INSERT INTO CUSTOMER_S VALUES (8,'Mastan',28,'Indore',20000.00);
Every time you can check the data in the database table using the query SELECT * FROM
TABLE_NAME; i.e., SELECT * FROM CUSTOMER_S VALUES
NO ROWS SELECTED.
SQL> declare
2 top varchar2(25);
3 begin
4 select name into top from student where marks=10;
5 dbms_output.put_line('The NAMES ARE : '||top);
6 exception
7 whenno_data_found then dbms_output.put_line('NO ONE SCORED FIRST
CLASS');
8 end;
9 /
NO ONE SCORED FIRST CLASS
PROGRAM:
SQL> begin
savepoint g;
insert into stu values('mahi','cse');
exception
when dup_val_on_index then
rollback to g;
commit;
end;
/
SQL> Rollback to h;
Rollback completed
OUTPUT:
NESTED IF:
SQL> declare --NESTED IF(2nd internal - 1st Qn)
2 a number:=&a;
3 b number:=&b;
4 begin
5 if a!=b then
6 if a>b then
7 dbms_output.put_line('A is the greatest');
8 else
9 dbms_output.put_line('B is the greatest');
10 end if;
11 end if;
12 end;
13 /
Enter value for a: 10
old 2: a number:=&a;
new 2: a number:=10;
Enter value for b: 20
old 3: b number:=&b;
new 3: b number:=20;
B is the greatest
CASE EXPRESSION:
SQL> declare --CASE EXPRESSION(2nd internal - 1st Qn)
2 grade varchar2(1):='&grade';
3 begin
4 case grade
5 when 'A' then dbms_output.put_line('Excellent');
6 when 'B' then dbms_output.put_line('GOOD');
7 when 'C' then dbms_output.put_line('Average');
8 when 'F' then dbms_output.put_line('Fail');
9 else
CASE EXPRESSION:
SQL>declare --SEARCHED CASE EXPRESSION(2nd internal - 1st Qn)
2 grade varchar2(1):='&grade';
3 begin
4 case
5 when grade='A' then dbms_output.put_line('Excellent');
6 when grade='B' then dbms_output.put_line('GOOD');
7 when grade='C' then dbms_output.put_line('Average');
8 when grade='F' then dbms_output.put_line('Fail');
9 else
10 dbms_output.put_line('Invalid Grade Entered');
11 end case;
12 end;
13 /
Enter value for grade: A
old 2: grade varchar2(1):='&grade';
new 2: grade varchar2(1):='A';
Excellent
Q1: TO READ A NUMBER FORM KEY BOARD AND DISPLAY THE SAME ON THE
SCREEN
DECLARE
N NUMBER:= :N;
BEGIN
DBMS_OUTPUT.PUT_LINE(N);
END;
/
Output:
Output:
B NUMBER;
C NUMBER;
BEGIN
A:=:A;
B:=:B;
C:=:C;
DBMS_OUTPUT.PUT_LINE('A IS '||A||' B IS '||B||' C IS '||C);
IF(A>B)AND(A>C)THEN
DBMS_OUTPUT.PUT_LINE(' A IS MAXIMUM');
ELSIF(B>A)AND(B>C)THEN
DBMS_OUTPUT.PUT_LINE('B IS MAXIMUM');
ELSE
DBMS_OUTPUT.PUT_LINE('C IS MAXIMUM');
END IF;
END;
/
Output:
Output:
The salary of employ is 50000
PL/SQL procedure successfully completed.
Syntax:
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [,…])]
{IS | AS}
BEGIN
<procedure body>
END procedure_name;
Output:sai
output:sai
Cursor program:
declare
emp_recvarchar(30);
cursor emp_cur is
select ename
from employ where salary > 25000;
Begin
Open emp_cur;
Loop
fetch emp_cur into emp_rec;
exit when emp_cur % notfound;
dbms_output.put_line(emp_rec);
end loop;
close emp_cur;
end;
/
Output:
sindhu
satya
PL/SQL procedure successfully completed.
(1)CREATE A TRIGGER
create or replace trigger trg2
after insert or delete or update
on dept1
for each row
when(new.deptno>0)
begin
dbms_output.put_line('trigger fired');
end;
/
(2)INSERT
->insert into deptvalues('sindhu',30);
*trigger fired*
*1 row created*
(3)UPADTE
->udpate dept1 set deptno=19 where dname='sindhu';
*trigger fired*
*1 row updated*
(4)DELETE
->delete from dept where deptno=30;
*trigger fired*
*1 row deleted*