DBMS Lab Manuals CS and IT
DBMS Lab Manuals CS and IT
Department of CS and IT
Subject: DBMS Lab
List of Experiments:
Sr. No. List of Experiments:
1 Design a Database and create required tables. For e.g. Bank, College
Faculty Name
(Dhanroop Mal Nagar)
EXPERIMENT – 1
AIM:Design a Database and create required tables. For e.g. Bank, College Database
SOLUTION-
CREATE DATABASE Bank;
BEGIN TRANSACTION;
/* Create a table called NAMES */
CREATE TABLE STUDENT(S_Id integer PRIMARY KEY, S_Name text, S_CITY);
/* Create few records in this table */
INSERT INTO STUDENT VALUES(1,'Tom','AJMER');
INSERT INTO STUDENT VALUES(2,'Lucy','KOTA');
INSERT INTO STUDENT VALUES(3,'Frank','JAIPUR');
StudentCourse
The simplest Join is INNER JOIN.
INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as
thecondition satisfies. This keyword will create the result-set by combining all rows
from both the tables where the condition satisfies i.e value of the common field will be
same.
Syntax:
SELECT table1.column1, table1.column2, table2.column1,
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
This query will show the names and age of students enrolled in different courses.
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
OUTPUT:
EXPERIMENT – 5
AIM:Write the query for implementing the following functions: MAX (), MIN (), AVG ()
SOLUTION-
Students-Table
AVG(): It returns average value after calculating from values in a numeric column.
Syntax:
SELECT AVG(column_name) FROM table_name;
Queries:
1. Computing average marks of students.
2. SELECT AVG(MARKS) AS AvgMarks FROM Students;
Output:
AvgMarks
80
MAX(): The MAX() function returns the maximum value of the selected column.
Syntax:
SELECT MAX(column_name) FROM table_name;
Queries:
1. Fetching maximum marks among students from the Students table.
2. SELECT MAX(MARKS) AS MaxMarks FROM Students;
Output:
MaxMarks
95
MIN(): The MIN() function returns the minimum value of the selected column.
Syntax:
SELECT MIN(column_name) FROM table_name;
Queries:
1. Fetching minimum marks among students from the Students table.
2. SELECT MIN(MARKS) AS MinMarks FROM Students;
Output:
MinMarks
50
EXPERIMENT – 5
AIM : Write the query to implement the concept of Integrity constrains.
EXPERIMENT – 7
AIM:Write the query to create the views.
SOLUTION-
CREATE VIEW Syntax
CREATE VIEWview_nameAS
SELECT column1, column2, ...
FROMtable_name
WHERE condition;
The following SQL creates a view that shows all customers from Brazil:
CREATE VIEW [Brazil Customers] AS
SELECTCustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';
We can query the view above as follows:
SELECT * FROM [Brazil Customers];
EXPERIMENT – 8
AIM:Perform the queries for triggers.
Trigger: A trigger is a stored procedure in database which automatically invokes whenever a
special event in the database occurs. For example, a trigger can be invoked when a row is
inserted into a specified table or when certain table columns are being updated.
SOLUTION-
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Given Student Report Database, in which student marks assessment is recorded. In
such schema,
create a trigger so that the total and average of specified marks is automatically
inserted
whenever a record is insert.
Here, as trigger will invoke before record is inserted so, BEFORE Tag can be used.
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| tid | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| subj1 | int(2) | YES | | NULL | |
| subj2 | int(2) | YES | | NULL | |
| subj3 | int(2) | YES | | NULL | |
| total | int(3) | YES | | NULL | |
| per | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
SQL Trigger to problem statement.
CREATE trigger stud_marks
before INSERT
on
Student
for each row
setStudent.total = Student.subj1 + Student.subj2 + Student.subj3, Student.per;
Structure trigger
CREATE OR REPLACE TRIGGER trigger_name
[BEFORE | AFTER] [INSERT | UPDATE | DELETE]
ON table_name
FOR EACH ROW
BEGIN
-- SQL statements
END;
EXPERIMENT – 9
AIM:Perform the following operation for demonstrating the insertion, updation and deletion.
SOLUTION-
We will insert 2 rows into the students table, one for each student:
INSERT INTO Students(StudentId, StudentName, DepartmentId, DateOfBirth)
VALUES(11, 'Ahmad', 4, '1997-10-12');
INSERT INTO Students VALUES(12, 'Aly', 4, '1996-10-12');
In the following UPDATE statement, we will update the DepartmentId for the Student with
StudentId = 6 to be 3:
UPDATE Students
SET DepartmentId = 3
WHERE StudentId = 6;
In the following statement, we will delete two students with StudentId 11 and 12:
DELETE FROM Students WHERE StudentId = 11 OR StudentId = 12;
EXPERIMENT –10
AIM:Using the referential integrity constraints.
Another example of Referential Integrity is Employee and Department relationship. If wehave
dept_id as foreign key in Employee table than by using referential integrity constraints wecan
avoid creating Employee without department or non-existing department.
In short Referential Integrity makes primary key foreign key relationship viable. Let's first create
Employee and Department table with primary key, foreign key and referential Integrity
constraints.
SOLUTION-
CREATE TABLE Department (dept_id INT NOT NULL,
dept_name VARCHAR(256),
PRIMARY KEY (dept_id)) ENGINE=INNODB;
Above SQL statements will create both Department and Employee table. dept_id is now
foreignkey in Employee table.
In this SQL, while creating foreign key we have specified ON DELETE clause which tells,
what needs to done when a record from parent table is deleted. CASCADE referential
action allows to delete or update all matching rows from child table, after deleting a
record in parent table. This way Refrential Integrity preserve data integrity of
relationship.
EXPERIMENT –11
AIM:Write the query for creating the users and their role.
SOLUTION-
Standard SQL syntax for creating how to create users,
CREATE USER username IDENTIFIED BY password
IDENTIFIEDWITHauth_plugin
CREATE ROLE creates a set of privileges which may be assigned to users of a database.
Once arole is assigned to a user, (s)he gets all the Privileges of that role. By creating and
granting roles,best means of database security can be practiced.
SQL Syntax:
CREATE ROLErole_name[WITH ADMIN {CURRENT_USER | CURRENT_ROLE}]
Parameters:
role_name:A name to identify the role.
Explanation:
With the above syntax, a role with role_name is created and immediately assigned to the
currentuser or the currently active role is passed on to other users. The default usage is
WITH ADMINCURRENT_USER.
EXPERIMENT –11
AIM: Design ERD for Blow statements