0% found this document useful (0 votes)
198 views63 pages

MCA-I ADBMS Practical File

The document discusses various SQL commands like DDL, DML, DCL to manage database tables. It also discusses implementing primary and foreign keys. Further, it demonstrates different select queries using WHERE, DISTINCT, IN, BETWEEN, LIKE, IS NULL, GROUP BY, HAVING, ORDER BY clauses and formatting result columns.

Uploaded by

hemkashyap628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views63 pages

MCA-I ADBMS Practical File

The document discusses various SQL commands like DDL, DML, DCL to manage database tables. It also discusses implementing primary and foreign keys. Further, it demonstrates different select queries using WHERE, DISTINCT, IN, BETWEEN, LIKE, IS NULL, GROUP BY, HAVING, ORDER BY clauses and formatting result columns.

Uploaded by

hemkashyap628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 63

Advance Database Management SESSION 2023-

INDEX
S. No Title Page No. Remarks
Design and manage table using DML, DDL and DCL
1. commands. Perform select query to work on EMP and DEPT
3
table of scott/tiger log in.

2. Query to implement primary and foreign key concept. 15

Retrieval of database – select: where, distinct, in, between-and


like, is null, group by-having,
3.
order by, column: (format, heading, justify, wrap trunc), nested
20
queries: (any, all, in, not in, exists).

4. Query to demonstrate inner outer and full join. 31

Views (create, update, drop), sequences (create, alter, drop),


5.
synonyms (create, drop), index (create, drop). 36

6. Transaction control (commit, rollback, save point). 43

7. Date control (grant, revoke). 48

Write a PL/SQL code to find whether the entered number is even


8.
odd. 53

9. Write a PL/SQL code to generate multiplication table. 54

10. Write a PL/SQL code to find largest of 3 number. 56

Write a PL/SQL code to computer the factorial & a given of


11.
number. 58

Write procedure to display any word and display it n number of


12.
times. 59

Create a salary table with attributions EmPno, Ename, Grade,


13 Gross salary &write a explicit cursor to display emp number &
60
grade of salary table.

Raipur Institute of Technology [RITEE] Page|1


Advance Database Management SESSION 2023-

Program 1: - Design and manage table using DML, DDL and DCL
commands. Perform select query to work on EMP and DEPT
table of scott/tiger log in.

DDL Commands

1. Create command

mysql> create table Emp(


-> Empno int,
-> Empname varchar(20),
-> Des varchar(15),
-> Sal int,
-> Address varchar(30),
-> Contact int
-> );

Output :-

Raipur Institute of Technology [RITEE] Page|2


Advance Database Management SESSION 2023-

mysql> create table Dept(


-> Deptno int,
-> Dname varchar(20),
-> loc varchar(20)
-> );

Output :-

2. Alter Command

i) If we want to add a column name “Dob” in the “Emp” table,


the following SQL Statement is used:
mysql> alter table Emp
-> add Dob date;

Raipur Institute of Technology [RITEE] Page|3


Advance Database Management SESSION 2023-

Output :-

ii) If we want to change the data type of the column named


“Dob” in the “Emp” table, the following SQL Statement is
used:

mysql> alter table Emp


-> modify column Dob year;

Raipur Institute of Technology [RITEE] Page|4


Advance Database Management SESSION 2023-

Raipur Institute of Technology [RITEE] Page|5


Advance Database Management SESSION 2023-

Output :-

iii) If want to delete the column named “Dob” in the “Emp”


table, the following SQL Statement is used:

mysql> alter table Emp


-> drop column Dob;

Raipur Institute of Technology [RITEE] Page|6


Advance Database Management SESSION 2023-

Output :-

3. Truncate Command

mysql> truncate table Dept;

Raipur Institute of Technology [RITEE] Page|7


Advance Database Management SESSION 2023-

Output :-

4. Rename Command

mysql> alter table Dept


-> rename to Department;

Output :-

5. Drop Command

mysql> drop table Department;

Raipur Institute of Technology [RITEE] Page|8


Advance Database Management SESSION 2023-

Output :-

DML Commands

1. Insert Command

i) Inserting a single row into a

table: mysql> insert into Emp

values(
-> 1,
-> "Rishabh",
-> "Ass. Prog.",
-> 35000,
-> "Raipur",
-> 12345
-> );

Raipur Institute of Technology [RITEE] Page|9


Advance Database Management SESSION 2023-

Raipur Institute of Technology [RITEE] P a g e | 10


Advance Database Management SESSION 2023-

Output :-

ii) Inserting more than one record using a single insert command:

mysql> insert into Emp values(


-> 2,
-> "Ravi",
-> "DEO",
-> 25000,
-> "Durg",
-> 24578
-> );

Raipur Institute of Technology [RITEE] P a g e | 11


Advance Database Management SESSION 2023-

Raipur Institute of Technology [RITEE] P a g e | 12


Advance Database Management SESSION 2023-

Output :-

iii) Skipping the fields while inserting:

mysql> insert into Emp (Empname,Sal,Contact) values(


-> "Hemant",
-> 30000,
-> 45783
-> );

Raipur Institute of Technology [RITEE] P a g e | 13


Advance Database Management SESSION 2023-

Output :-

2. Update Command

mysql> update Emp


-> set Address = "Bilaspur"
-> where Empno = 2;

Raipur Institute of Technology [RITEE] P a g e | 14


Advance Database Management SESSION 2023-

Output :-

3. Delete Command

mysql> delete from Emp


-> where Empname = "Hemant";

Output :-

Raipur Institute of Technology [RITEE] P a g e | 15


Advance Database Management SESSION 2023-

DCL Commands

1. Grant Command

mysql> grant select on emp to 'ravis'@'localhost';

Output :-

2. Revoke Command

mysql> revoke select on emp from 'ravis'@'localhost';

Output :-

Raipur Institute of Technology [RITEE] P a g e | 16


Advance Database Management SESSION 2023-

Program 2: - Query to implement primary and foreign key concept.

Implementation of Primary key & Foreign Key

1. Creation of Parent Table Emp

mysql> create table Emp(


-> Emp_Id int not null,
-> Name varchar(30),
-> Loc varchar(50),
-> Contact int,
-> primary key(Emp_id)
-> );

Output :-

Inserting the data in Parent Table Emp :-


mysql> insert into Emp values(
-> 01,
-> "Rishabh",
-> "Raipur",
-> 12345
-> );
mysql> insert into Emp values(
Raipur Institute of Technology [RITEE] P a g e | 17
Advance Database Management SESSION 2023-

-> 02,
-> "Neeraj",
-> "Durg",
-> 36978
-> );
mysql> insert into Emp values(
-> 03,
-> "Bikalp",
-> "Kanker",
-> 45796
-> );
mysql> insert into Emp values(
-> 04,
-> "Fanish",
-> "Bilaspur",
-> 58971
-> );
mysql> insert into Emp values(
-> 05,
-> "Bhavesh",
-> "Gariyaband",
-> 67489
-> );
mysql> insert into Emp values(
-> 06,
-> "Mohnish",
-> "Durg",
-> 32198
-> );

Raipur Institute of Technology [RITEE] P a g e | 18


Advance Database Management SESSION 2023-

Output :-

2. Creation of Child Table Dept

mysql> create table Dept(


-> Dep_Id int not null,
-> Loc varchar(50),
-> Emp_Id int not null,
-> primary key(Dep_Id),
-> foreign key(Emp_Id) references Emp(Emp_Id)
-> );

Raipur Institute of Technology [RITEE] P a g e | 19


Advance Database Management SESSION 2023-

Output :-

Inserting the data in Child Table Dept :-

mysql> insert into Dept values(


-> 1,
-> "Kanker",
-> 3
-> );
mysql> insert into Dept values(
-> 2,
-> "Raipur",
-> 1
-> );
mysql> insert into Dept values(
-> 3,
-> "Bilaspur",
-> 4
-> );
mysql> insert into Dept values(
-> 4,
-> "Durg",
-> 2
-> );

Raipur Institute of Technology [RITEE] P a g e | 20


Advance Database Management SESSION 2023-

Output :-

Raipur Institute of Technology [RITEE] P a g e | 21


Advance Database Management SESSION 2023-

Program 3: - Retrieval of Database - select: where, distinct, in,


between-and, like, is null, group by-having, order by, column:
(format, heading, justify, wrap trunc), nested queries: (any, all,
in, not in, exists).

Consider a Table:- Student

SELECT

1. Where
mysql> select * from student
-> where Roll_No = 1;

Raipur Institute of Technology [RITEE] P a g e | 22


Advance Database Management SESSION 2023-

Output :-

2. Distinct
mysql> select distinct Age from student;

Output :-

3. In
mysql> select * from student
-> where Address in ('Lalpur', 'Mahavir Nagar');

Raipur Institute of Technology [RITEE] P a g e | 23


Advance Database Management SESSION 2023-

Output :-

4. Between-and
mysql> select * from student
-> where Roll_No between 2 and 4;

Output :-

5. Like
mysql> select * from student
-> where Address like 'p%';

Raipur Institute of Technology [RITEE] P a g e | 24


Advance Database Management SESSION 2023-

Output :-

6. Is Null
mysql> select Name,Age,Contact
-> from student
-> where Contact is null;

Output :-

7. Group by – having
mysql> select Roll_No, Name, Age, Address, Contact
-> from student
-> group by Age
-> having count(Age) >= 2;

Raipur Institute of Technology [RITEE] P a g e | 25


Advance Database Management SESSION 2023-

Output :-

8. Order by
mysql> select * from student
-> order by Name;

Output :-

Raipur Institute of Technology [RITEE] P a g e | 26


Advance Database Management SESSION 2023-

COLUMN

1. Format
(Before Formatting)

(After Formatting)
mysql> alter table student
-> modify column Dob year;

Output :-

Raipur Institute of Technology [RITEE] P a g e | 27


Advance Database Management SESSION 2023-

2. Heading
mysql> select Name as Stu_Name
-> from student;

Output :-

3. Justify
mysql> select lpad(Name,70,' ') from student;

Output :-

Raipur Institute of Technology [RITEE] P a g e | 28


Advance Database Management SESSION 2023-

NESTED QUERIES

Consider a Table Emp & Dept

1. Any
mysql> select Name from Emp
-> where Emp_Id = any
-> (select Emp_Id from Dept
-> where Dep_Id > 2);

Raipur Institute of Technology [RITEE] P a g e | 29


Advance Database Management SESSION 2023-

Output :-

2. All
mysql> select Name from Emp
-> where Emp_Id = all
-> (select Emp_Id from Dept
-> where Dep_Id = 3);

Output :-

3. In
mysql> select * from Emp
Raipur Institute of Technology [RITEE] P a g e | 30
Advance Database Management SESSION 2023-

-> where Emp_Id in


-> (select Emp_Id from Emp
-> where Loc > 'c%');

Output :-

4. Not in
mysql> select * from Emp
-> where Emp_Id not in
-> (select Emp_Id from Emp
-> where Loc > 'C%');

Raipur Institute of Technology [RITEE] P a g e | 31


Advance Database Management SESSION 2023-

Output :-

5. Exists
mysql> select Dep_Id from Dept
-> where exists
-> (select Name from Emp
-> where Emp.Loc = Dept.Loc and Emp_Id < 4);

Output :-

Raipur Institute of Technology [RITEE] P a g e | 32


Advance Database Management SESSION 2023-

Program 4: - Query to demonstrate inner outer and full join.

Consider a Table:-

1. Student

2. Student Course

Raipur Institute of Technology [RITEE] P a g e | 33


Advance Database Management SESSION 2023-

1. Inner Join -
mysql> select StudentCourse.Course_Id, Student.Name,
-> Student.Age from Student
-> inner join StudentCourse on
-> Student.Roll_No = StudentCourse.Roll_No;

Output :-

1. Outer Join -

(i) Left Join

mysql> select Student.Name, StudentCourse.Course_Id


-> from Student
-> left join StudentCourse
-> on StudentCourse.Roll_No = Student.Roll_No;

Raipur Institute of Technology [RITEE] P a g e | 34


Advance Database Management SESSION 2023-

Output :-

(ii) Right Join

mysql> select Student.Name, StudentCourse.Course_Id


-> from Student
-> right join StudentCourse
-> on StudentCourse.Roll_No = Student.Roll_No;

Raipur Institute of Technology [RITEE] P a g e | 35


Advance Database Management SESSION 2023-

Output :-

2. Full Join –

mysql> select Student.Name, StudentCourse.Course_Id


-> from Student
-> left join StudentCourse
-> on StudentCourse.Roll_No = Student.Roll_No
-> union all
-> select Student.Name, StudentCourse.Course_Id
-> from Student
-> right join StudentCourse
-> on StudentCourse.Roll_No = Student.Roll_No;

Raipur Institute of Technology [RITEE] P a g e | 36


Advance Database Management SESSION 2023-

Output :-

Raipur Institute of Technology [RITEE] P a g e | 37


Advance Database Management SESSION 2023-

Program 5: - Views (create, update, drop), sequences (create, alter,


drop), synonyms (create, drop), index (create, drop)

Consider a table Student

VIEWS

1. Create View:-

mysql> create view Details_View as


-> select Name, Address
-> from student
-> where Roll_No = 1;

Raipur Institute of Technology [RITEE] P a g e | 38


Advance Database Management SESSION 2023-

Output :-

2. Update View :-
mysql> alter view Details_View as
-> select Name,Address,Contact
-> from student
-> where Roll_No = 1;

Output :-

3. Drop View :-

mysql> drop view Details_View;

Raipur Institute of Technology [RITEE] P a g e | 39


Advance Database Management SESSION 2023-

Output :-

SEQUENCES

1. Create Sequence :-

create sequence student_seq


minvalue 1
start with 1
increment by 1
cache 10;

Output :-

Now create a table named students with column as id and name.

SQL> create table students(


Id int,
Name varchar(20)
);
Raipur Institute of Technology [RITEE] P a g e | 40
Advance Database Management SESSION 2023-

SQL> insert into students values(


student_seq.nextval,'Rishabh');
SQL> insert into students values(
student_seq.nextval,'Ravi');

Output :-

2. Alter Sequence :-

SQL> alter sequence student_seq increment by 2;


SQL> insert into students values(
Student_seq.nextval,'Neeraj');
SQL> insert into students values(
student_seq.nextval,'Hemant');

Output :-

Raipur Institute of Technology [RITEE] P a g e | 41


Advance Database Management SESSION 2023-

3. Drop Sequence :-

SQL> drop sequence student_seq;

Output :-

SYNONYMS

Consider a Table Student2

1. Create Synonyms :-

SQL> create synonym Stud for student2;

Raipur Institute of Technology [RITEE] P a g e | 42


Advance Database Management SESSION 2023-

Output :-

2. Drop Synonym :-

SQL> drop synonym

Stud; Output :-

Raipur Institute of Technology [RITEE] P a g e | 43


Advance Database Management SESSION 2023-

INDEX

1. Create Index :-

SQL> create index idx_Name


on student2(Name);

Output :-

2. Drop Index :-

SQL> drop index idx_Name;

Output :-

Raipur Institute of Technology [RITEE] P a g e | 44


Advance Database Management SESSION 2023-

Program 6: - Transaction control (commit, rollback, save point).

1. Commit
Consider a Table Student :-

mysql> delete from student


-> where Age = 23;
mysql> commit;

Raipur Institute of Technology [RITEE] P a g e | 45


Advance Database Management SESSION 2023-

Output :-

2. Rollback
Consider a Table Student :-

Raipur Institute of Technology [RITEE] P a g e | 46


Advance Database Management SESSION 2023-

mysql> delete from student


-> where Age = 22;
mysql> rollback;
Output :-

3. Save Point
Consider a Table Student :-

Raipur Institute of Technology [RITEE] P a g e | 47


Advance Database Management SESSION 2023-

mysql> savepoint sp1;


mysql> delete from student
-> where Roll_No = 1;
mysql> savepoint sp2;
mysql> delete from student
-> where Roll_No = 2;
mysql> savepoint sp3;
mysql> delete from student
-> where Roll_No = 3;
mysql> rollback to sp2;

Raipur Institute of Technology [RITEE] P a g e | 48


Advance Database Management SESSION 2023-

Output :-

Raipur Institute of Technology [RITEE] P a g e | 49


Advance Database Management SESSION 2023-

Program 7: - Data control (grant, revoke)

1.) Grant the select authority on the Student table to all user.

a) Grant Create Session

Create user Anyone like public can accesses select authority on


Students:

Login by another user:

Raipur Institute of Technology [RITEE] P a g e | 50


Advance Database Management SESSION 2023-

Access select privilege by public (RishabhSahu):

b) Grant the select, delete and update authority on student table


to user ‘System’.

c) Grant privilege select, delete, update authority on students


table to ‘RishabhSahu’’:

*Now first access select privilege on Students table

Raipur Institute of Technology [RITEE] P a g e | 51


Advance Database Management SESSION 2023-

*Now first access delete privilege on Student table

mysql> delete from work2.student


-> where Name = "Poonam";

Output :-

*Now first access Update privilege on Student table

mysql> update work2.student


-> set Name = "Pankaj", Age = 19
-> where Roll_No = 4;

Raipur Institute of Technology [RITEE] P a g e | 52


Advance Database Management SESSION 2023-

Output :-

2.) Revoke the select privilege on students table from RishabhSahu

Now login as user RishabhSahu:

Raipur Institute of Technology [RITEE] P a g e | 53


Advance Database Management SESSION 2023-

Now try to check that user RishabhSahu can apply select privilege on
Student table or not:

3.) Drop all the privileges on Student table from user RishabhSahu.

Raipur Institute of Technology [RITEE] P a g e | 54


Advance Database Management SESSION 2023-

Program 8: - Write a PL/SQL code to find whether the


entered number is odd or even.

SQL> set serveroutput on


SQL> declare
num int := &num;
begin
if mod(num,2) = 0 then
dbms_output.put_line('The number '||num||' is even number');
else
dbms_output.put_line('The number '||num||' is odd number');
end if;
end;
/

Output :-

Raipur Institute of Technology [RITEE] P a g e | 55


Advance Database Management SESSION 2023-

Program 9: - Write a PL/SQL code to generate multiplication table.

SQL> set serveroutput on


SQL> declare
a int;
b int;
begin
<<outer>>
for a in 1..2 loop
<<inner>>
for b in 1..10 loop
dbms_output.put_line( a ||' * ' || b || ' = ' || a*b);
end loop inner;
end loop outer;
end;
/

Raipur Institute of Technology [RITEE] P a g e | 56


Advance Database Management SESSION 2023-

Output :-

Raipur Institute of Technology [RITEE] P a g e | 57


Advance Database Management SESSION 2023-

Program 10: - Write a PL/SQL code to find largest of 3 number.

SQL> set serveroutput on


SQL> declare
a int;
b int;
c int;
begin
a := &a;
b := &b;
c := &c;
if a>b and a>c
then
dbms_output.put_line('Largest Number is '||a);
elsif b>a and b>c
then
dbms_output.put_line('Largest Number is '||b);
else
dbms_output.put_line('Largest Number is '||c);
end if;
end;
/

Raipur Institute of Technology [RITEE] P a g e | 58


Advance Database Management SESSION 2023-

Output :-

Raipur Institute of Technology [RITEE] P a g e | 59


Advance Database Management SESSION 2023-

Program 11: - Write a PL/SQL code to compute the factorial & a


given number.

SQL> set serveroutput on


SQL> declare
f int := 1;
n int := &n;
begin
while n>0 loop
f := n*f;
n := n-1;
end loop;
dbms_output.put_line('Factorial of entered number is ' || f);
end;
/

Output :-

Raipur Institute of Technology [RITEE] P a g e | 60


Advance Database Management SESSION 2023-

Program 12: - Write procedure to display any word and display it


n number of times.

DROP PROCEDURE IF EXISTS WordPrints;


DELIMITER $$
CREATE PROCEDURE `WordPrints` (Word VARCHAR(255),n
integer)
BEGIN
DECLARE result VARCHAR(255);
SET result = '';
loop_label: LOOP
IF n <= 0 THEN
LEAVE loop_label;
END IF;
SET n = n - 1;
SET result = CONCAT(result, Word ,',');
END LOOP;
SELECT result;
END;
$$
DELIMITER ;
CALL WordPrints('Rishabh',5);

Output :-

Raipur Institute of Technology [RITEE] P a g e | 61


Advance Database Management SESSION 2023-

Program 13: - Create a salary table with attributions EmPno,


Ename, Grade, Gross Salary & write a explicit cursor to
display emp number & grade of salary table.

DELIMITER $$
DROP PROCEDURE IF EXISTS display_by_cursor$$
CREATE PROCEDURE display_by_cursor()
BEGIN
DECLARE Emp_id integer;
DECLARE Grd varchar(255);
DECLARE finished INTEGER DEFAULT 0;
DECLARE Cust_Record
CURSOR FOR
SELECT `Emp_No`,`Grade` FROM `salary`;
CREATE TEMPORARY TABLE tblResults (Emp_No
int,Grade varchar(255));
BEGIN
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN Cust_Record;
getEmail: LOOP
FETCH Cust_Record into Emp_id, Grd;
IF finished = 1 THEN
LEAVE getEmail;
END IF;
INSERT INTO tblResults VALUES (Emp_id, Grd);
END LOOP getEmail;
CLOSE Cust_Record;
END;
SELECT * FROM tblResults;
DROP TEMPORARY TABLE IF EXISTS tblResults;
END$$
DELIMITER ;
CALL display_by_cursor();

Raipur Institute of Technology [RITEE] P a g e | 62


Advance Database Management SESSION 2023-

Output :-

-------END------

Raipur Institute of Technology [RITEE] P a g e | 63

You might also like