0% found this document useful (0 votes)
2 views

DBMS Lab Manuals CS and IT

The document outlines a series of experiments for a Database Management System (DBMS) lab at Engineering College Bikaner, focusing on various SQL operations such as creating databases, applying constraints, and implementing functions. It includes detailed SQL examples for tasks like designing a database, using integrity constraints, performing joins, and creating triggers. Additionally, it emphasizes group projects for practical understanding of database design methodologies.

Uploaded by

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

DBMS Lab Manuals CS and IT

The document outlines a series of experiments for a Database Management System (DBMS) lab at Engineering College Bikaner, focusing on various SQL operations such as creating databases, applying constraints, and implementing functions. It includes detailed SQL examples for tasks like designing a database, using integrity constraints, performing joins, and creating triggers. Additionally, it emphasizes group projects for practical understanding of database design methodologies.

Uploaded by

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

Engineering College Bikaner

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

2 Write a SQL statement for implementing ALTER, UPDATE and DELETE.


Perform the following operation for demonstrating the insertion, updation
3 and deletion.

4 Write the query to implement the concept of Integrity constrains.


5 Apply the constraints like Primary Key, Foreign key, NOT NULL to the
tables.
6 Write the query to implement the concept of Integrity constrains.
7 Write the query for implementing the following functions: MAX (), MIN
(), AVG () and COUNT ().
8 Perform the queries for triggers.
9 Using the referential integrity constraints.
10 Write the query to create the views.
11 Write the query for creating the users and their role.
12 Data Base Designing Project:

For better understanding students (group of 3-4 students) should design


data base for any data base project, understand the requirement and design
methodology of project by its own.
Some examples of data base design project like:

College management system, Inventory management system and Hospital


management system.

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');

INSERT INTO STUDENT VALUES(4,'Jane','DELHI');


INSERT INTO STUDENT VALUES(5,'Robert','MUMBAI');
COMMIT;
/* Display all the records from the table */
SELECT * FROM STUDENT;
EXPERIMENT – 2
AIM:Apply the constraints like Primary Key, Foreign key, NOT NULL to the tables.
SOLUTION-
CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9), MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER), UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP(SSN));
EXPERIMENT – 3
AIM: Write a SQL statement for implementing ALTER, UPDATE and DELETE.
SOLUTION-
BEGIN TRANSACTION;
/* Create a table called NAMES */
CREATE TABLE student (
Sid int,
sname varchar (255),
scity varchar (255),
sage int
);
/* insert data into table */
INSERT INTO student VALUES(101,"AMIT","AJMER",26);
insert into student values(102,"manish","kota",35);
insert into student values(103,"ankit","jaipur",40);
/* show the table */
select *
from student;
/* Alter table */
ALTER TABLE student
addsweightint;
/* insert data into table */
INSERT INTO student VALUES(101,"AMIT","AJMER",26,90);
insert into student values(102,"manish","kota",35,100);
insert into student values(103,"ankit","jaipur",40,75);
/* show the table */
select *
from student;
/* UPDATE table */
UPDATE student
setsname='RAM',scity='newyork'
wheresid='101';
/* show the table */
select *
from student;
/*DELETE IS USED delete particular Record*/
DELETE from student
wheresid='102';
/* show the table */
select *
from student;
EXPERIMENT – 4
AIM:Write the queries to implement the joins.
SOLUTION-
A SQL Join statement is used to combine data or rows from two or more tables based
on a
common field between them. Different types of Joins are:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
Consider the two tables below:
Student

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;

CREATE TABLE Employee (emp_id INT NOT NULL,


emp_name VARCHAR(256),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
ON DELETE CASCADE) 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

The entities and attributes are


Student: StuId, StuName, Address. We assume that each
student has a unique ID.

Department: DeptName, Office. We are assuming that each


department has a unique name and that each department has
one office designated as departmental office.

Faculty: FacId, FacName, Rank. We are assuming that


FacId is unique and that every faculty member must belong to
a department, so faculty is a weak entity

Course: Course id, Ctitle, Sched, Room. We are assuming that


the course consists of an id that identifies the course uniquely. Also
assuming that each course is taught by only one faculty member.
The faculty is assigned with a rank as per their performance in the course.

You might also like