Dbms Lab Manual
Dbms Lab Manual
LABORATORY MANUAL
DBMS LABORATORY
BCS403
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
INDEX
1. Code of Conduct
4. Program Outcomes
6. Syllabus
7. Laboratory Manual
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Code of Conduct
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
1. Code of Conduct
Standards of Conduct
To,
Prof.Namithadevi
NN, Assistant
professor
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
a. Treat each other with fairness and respect, and conduct themselves with dignity and restraint in all
exchanges with colleagues.
b. Be fair and objective when presenting a professional judgment on their colleagues’ work
c. Not use threatening or abusive behavior or language, verbal harassment or intimidation of another
member of the faculty, under any circumstance while on the college campus.
d. Not knowingly misrepresent the views/positions of colleagues to further their own position orpoint
of view.
a. Faculty shall discuss with their Principal/HOD and submit a semester wise Lesson Plan. This will be
followed-up and monitored on a regular basis by the respective Principal/HOD.
b. The HODs/Principals shall make periodic surprise visits to classes to provide feedback on the teaching
methods and materials used. They will also check the diaries the faculty are expected to maintain.
“I have read all the terms and conditions outlined in this document and agree to abide by them”
Prof.Namithadevi NN
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Institute Vision
Institute Mission
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Department Vision
Department Mission
M3: To establish industry institute interaction, to enable students to cater the ever changing
industry demands and to nurture entrepreneurial qualities.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Program Educational
Objectives
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
PEO 1: To provide Graduates of computer science & engineering course with a solid
foundation in the principles and practices of computer science and engineering
enabling them to have successful professional career.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Program Outcome
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Program Specific
Outcomes
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Syllabus
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
7. Syllabus
DATABASE MANAGEMENT
SYSTEM(BCS403)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Introduction to SQL
SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel –
(Structured English Query Language)”. It is a query language used for accessing and modifying
information in the database. IBM first developed SQL in 1970s. Also, it is an ANSI/ISO
standard. It has become a Standard Universal Language used by most of the relational database
management systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft SQL
server, Sybase etc. Most of these have provided their own implementation thus enhancing its
feature and making it a powerful tool. Few of the SQL commands used in SQL programming
are SELECT Statement, UPDATE Statement, INSERT INTO Statement, DELETE Statement,
WHERE Clause, ORDER BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views,
GROUP Functions, Indexes etc.
SQL Commands
SQL commands are instructions used to communicate with the database to perform specific
task that work with data. SQL commands can be used not only for searching the database but
also to perform various other functions like, for example, you can create tables, add data to
tables, or modify data, drop the table, set permissions for users.
The CREATE TABLE Statement is used to create tables to store data. Integrity Constraints like
primary key, unique key and foreign key can be defined for the columns while creating the
table. The integrity constraints can be defined at column level or table level. The
implementation and the syntax of the CREATE Statements differs for different RDBMS.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
number(size)
Number value with a max number of column digits specified in
or int
parenthesis.
number (size,d) Number value with a maximum number of digits of "size" total, with a
Integrity Constraints are used to apply business rules for the database tables. The
constraintsavailable in SQL are Foreign Key, Primary key, Not Null, Unique, Check.
Constraints can be defined in two ways:
1. The constraints can be specified immediately after the column definition. This
is called column-leveldefinition.
2. The constraints can be specified after all the columns are defined. This is called table-
level definition.
1) Primary key:
This constraint defines a column or combination of columns which
uniquely identifies each row inthe table.
Syntax to define a Primary key at column level:
column_name2,..)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
referenced_table_name(column_name)
4) Unique Key:
This constraint ensures that a column or a group of columns in each row have
a distinct value.
A column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
5) Check Constraint:
This constraint defines a business rule on a column. All the rows
must satisfy this rule. Theconstraint can be applied for a single column or a
group of columns.
Syntax to define a Check constraint:
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Commit command
Rollback command
This command restores the database to last commited state. It is also use with
savepoint command tojump to a savepoint in a transaction.
Savepoint command
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
LAB EXPERIMENTS
1. Create a table called Employee & execute the following. Employee (EMPNO, ENAME,
JOB, MANAGER_NO, SAL, COMMISSION).
1. Create a user and grant all permissions to the user.
2. Insert the any three records in the employee table contains attributes EMPNO, ENAME
JOB, MANAGER_NO, SAL, COMMISSION and use rollback. Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.
+ +
| Tables_in_COMPANY |
+ +
| Employee |
+ +
1 row in set (0.00 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
3. Insert any three records in the employee table and use rollback.
Database changed
-- START A TRANSACTION
mysql> COMMIT;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+ + + + + + +
| 2 | Ram Charan | Developer | 1 | 4000.00 | NULL |
| 3 | Honey Singh | Salesperson | 2 | 3000.00 | 500.00 |
+ + + + + + +
mysql> ROLLBACK;
Query OK, 0 rows affected (0.06 sec)
4. Add primary key constraint and not null constraint to the employee table.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | NO | PRI | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MANAGER_NO | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.00 sec)
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (1, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
5. Insert null values into the employee table and verify the result.
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
-> VALUES (4, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
Query OK, 1 row affected (0.16 sec)
mysql>
mysql> SELECT * FROM Employee;
+ + + + + + +
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+ + + + + + +
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
| 4 | Ranjan | Manager | NULL | 5000.00 | 1000.00 |
+ + + + + + +
2 rows in set (0.00 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
2. Create a table called Employee that contain attributes EMPNO, ENAME, JOB,
MGR, SAL & execute the following.
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job.
4. Rename the column of Employ table using alter command.
5. Delete the employee whose Empno is 105.
1. Create a table called Employee with attributes EMPNO, ENAME, JOB, MGR, SAL
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| EMPNO | int | YES | | NULL | |
| ENAME | varchar(255) | YES | | NULL | |
| JOB | varchar(255) | YES | | NULL | |
| MGR | int | YES | | NULL | |
| SAL | decimal(10,2) | YES | | NULL | |
| COMMISSION | decimal(10,2) | YES | | NULL | |
+ + + + + + +
6 rows in set (0.00 sec)
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
-> VALUES
-> (101, 'Radha Bai', 'Manager', NULL, 5000.00, 1000.00),
-> (102, 'Krishna Kumar', 'Developer', 101, 4000.00, NULL),
-> (103, 'Abdul Sattar', 'Salesperson', 102, 3000.00, 500.00),
-> (104, 'Bob Johnson', 'Accountant', 101, 4500.00, NULL),
-> (105, 'Amartya Sen', 'HR Manager', 101, 4800.00, 800.00);
Query OK, 5 rows affected (0.12 sec)
Records: 5 Duplicates: 0 Warnings: 0
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
3. Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), Group by, Order by.
Employee (E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ + + + +
| E_id | E_name | Age | Salary |
+ + + + +
| 1 | Samarth | 30 | 50000.00 |
| 2 | Ramesh Kumar | 25 | 45000.00 |
| 3 | Seema Banu | 35 | 60000.00 |
| 4 | Dennis Anil | 28 | 52000.00 |
| 5 | Rehman Khan | 32 | 58000.00 |
| 6 | Pavan Gowda | 40 | 70000.00 |
| 7 | Shruthi Bhat | 27 | 48000.00 |
| 8 | Sandesh Yadav | 29 | 51000.00 |
| 9 | Vikram Acharya | 33 | 62000.00 |
| 10 | Praveen Bellad | 26 | 46000.00 |
| 11 | Sophia Mary | 31 | 55000.00 |
| 12 | Darshan Desai | 34 | 63000.00 |
+ + + + +
12 rows in set (0.00 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ + +
| E_name | Salary |
+ + +
| Ramesh Kumar | 45000.00 |
| Praveen Bellad | 46000.00 |
| Shruthi Bhat | 48000.00 |
| Samarth | 50000.00 |
| Dennis Anil | 52000.00 |
| Sandesh Yadav | 52000.00 |
| Sophia Mary | 55000.00 |
| Rehman Khan | 58000.00 |
| Seema Banu | 62000.00 |
| Vikram Acharya | 62000.00 |
| Darshan Desai | 63000.00 |
| Pavan Gowda | 70000.00 |
+ + +
12 rows in set (0.00 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
4. 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 & new Salary. CUSTOMERS (ID, NAME, AGE,
ADDRESS, SALARY)
-- INSERT TRIGGER
DELIMITER //
DELIMITER ;
-- UPDATE TRIGGER
DELIMITER //
DELIMITER ;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
-- DELETE TRIGGER
DELIMITER //
DELIMITER ;
mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
+ -------------------------------- +
| SAL_DIFF |
+ -------------------------------- +
| salary inserted is 50000.00 |
+ -------------------------------- +
1 row in set (0.00 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
5. Create cursor for Employee table & extract the values from the table. Declare the variables
,Open the cursor & extrct the values from the cursor. Close the cursor. Employee(E_id,
E_name, Age, Salary)
USE COMPANY05;
DELIMITER //
OPEN emp_cursor;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
SET @finished = 0;
cursor_loop: LOOP
IF @finished = 1 THEN
LEAVE cursor_loop;
END IF;
SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age, ', Salary:
',emp_salary) AS Employee_Info;
END LOOP;
CLOSE emp_cursor;
END//
DELIMITER ;
+ --------------------------------------------------------------------------- +
| Employee_Info |
+ --------------------------------------------------------------------------- +
| Employee ID: 1, Name: Samarth, Age: 30, Salary: 50000.00 |
+ --------------------------------------------------------------------------- +
1 row in set (0.07 sec)
+ ------------------------------------------------------------------------------------ +
| Employee_Info |
+ ------------------------------------------------------------------------------------ +
| Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000.00 |
+ ------------------------------------------------------------------------------------ +
1 row in set (0.07 sec)
+ ------------------------------------------------------------------------------- +
| Employee_Info |
+ ------------------------------------------------------------------------------- +
| Employee ID: 3, Name: Seema Banu, Age: 35, Salary: 62000.00 |
+ ------------------------------------------------------------------------------- +
1 row in set (0.07 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ ------------------------------------------------------------------------------ +
| Employee_Info |
+ ------------------------------------------------------------------------------ +
| Employee ID: 4, Name: Dennis Anil, Age: 28, Salary: 52000.00 |
+ ------------------------------------------------------------------------------ +
1 row in set (0.07 sec)
+ ---------------------------------------------------------------------------------- +
| Employee_Info |
+ ---------------------------------------------------------------------------------- +
| Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000.00 |
+ ---------------------------------------------------------------------------------- +
1 row in set (0.07 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
6. Write a PL/SQL block of code using parameterized Cursor, that will merge the data available
in the newly created table N_RollCall with the data available in the table O_RollCall. If the
data in the first table already exist in the second table then that data should be skipped.
USE ROLLCALL;
DELIMITER //
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
DELIMITER ;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
+ + + +
| student_id | student_name | birth_date |
+ + + +
| 1 | Shivanna | 1995-08-15 |<- Common record,not duplicated
| 2 | Bhadramma | 1998-03-22 |<- New record from N_RollCall
| 3 | Cheluva | 1990-12-10 |<- Common record,not duplicated
| 4 | Devendra | 2000-05-18 |<- New record from N_RollCall
| 5 | Eshwar | 1997-09-03 |<- New record from N_RollCall
+ + + +
5 rows in set (0.00 sec)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
7. Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create, Read,
Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.
Please refer to the link below which contains detailed procedure of installing Open Source NoSQL Data
base MongoDB.
https://moodle.sit.ac.in/blog/setting-up-mongodb-on-ubuntu/
Start MongoDB.
Mongosh
bookDB> db.createCollection("ProgrammingBooks")
INSERT operations
bookDB> db.ProgrammingBooks.insertMany([
{
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
},
{
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
category: "JavaScript",
year: 2008
},
{
title: "Design Patterns: Elements of Reusable Object-Oriented Software",
author: "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides",
category: "Software Design",
year: 1994
},
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
{
title: "Introduction to Algorithms",
author: "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein",
category: "Algorithms",
year: 1990
},
{
title: "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
author: "Eric Matthes",
category: "Python",
year: 2015
}
])
bookDB> db.ProgrammingBooks.insertOne({
title: "The Pragmatic Programmer: Your Journey to Mastery",
author: "David Thomas, Andrew Hunt",
category: "Software Development",
year: 1999
})
bookDB> db.ProgrammingBooks.find().pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Software Design',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Algorithms',
year: 1990
},
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
},
{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Software Development',
year: 1999
}
]
Update Operations
bookDB>db.ProgrammingBooks.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "Robert C. Martin (Uncle Bob)" } }
)
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
}
]
bookDB> db.ProgrammingBooks.updateMany(
{ year: { $lt: 2010 } },
{ $set: { category: "Classic Programming Books" } }
)
//verify the update operation by displaying books published before year 2010
bookDB> db.ProgrammingBooks.find({ year: { $lt: 2010 } }).pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Classic Programming Books',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford
Stein',
category: 'Classic Programming Books',
year: 1990
},
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Classic Programming Books',
year: 1999
}
]
Delete Operations
bookDB> db.ProgrammingBooks.drop()
true
bookDB>
VVIET, Mysuru