0% found this document useful (0 votes)
39 views26 pages

DBMS Ii

Database practical

Uploaded by

Yogendra Kshetri
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)
39 views26 pages

DBMS Ii

Database practical

Uploaded by

Yogendra Kshetri
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/ 26

TRIBHUVAN UNIVERSITY

Patan Multiple Campus BIT 3rd semester

REPORT on Database Management System

Submitted by: Submitted To:


Yogendra Chalaune Kshetri Dev Narayan Yadav
Roll:08/79 (Associate Professor)
Symbol:79020070
Table of Contents:
Lab Sheet-1: How to Create a Database and various Tables using CMD in MySQL XAMPP. Write the
syntax and explain

Lab Sheet-2: Use of SQL Data Definition Language (DDL) to create the COMPANY Database and
create following tables in MySQL XAMPP

Lab Sheet-3: Use Draw.io CASE Tool to draw the E-R database of COMPANY database and import in
word document

Lab Sheet-4: Prepare the requirements (miniworld) of Patan Multiple Campus Library and draw E-R
diagrams and their relational database Tables.

Lab Sheet-5: Prepare the requirements (miniworld) of Students database of Patan Multiple Campus
BTI Department. Draw E-R diagrams and their relational database Tables.

Lab Sheet-6: Use of SQL Constraints - The following constraints are commonly used in SQL

Lab Sheet-7: What is Referential Integrity?

Lab Sheet-8: What are domain constraints? Explain the difference between PRIMARY and UNIQUE
constraints. Use the following constraints to create an EMPLOYEE table.

Lab Sheet-9: Create all the COMPANY Database Tables with all various constraints (Primary, Domain,
Foreign and Referential integrity) and insert 5 records in each tables and then use the following SQL
commands

Lab Sheet-9 :1 Use of SELECT and FROM clauses..

Lab Sheet-9_2 Use of SELECT, FROM and WHERE clauses.

Lab Sheet-9_3: Use of GROUP BY clause

Lab Sheet-9_4: Use of HAVING clause

Lab Sheet - 9 - 5 : Use Of ORDER BY clause

2
Lab Sheet-1: How to Create a Database and various Tables using CMD in MySQL XAMPP.
Write the syntax and explain.

1. Starting the MySQL Server in XAMPP

First, ensure that the MySQL server is running in XAMPP.

1. Open XAMPP Control Panel.


2. Start the MySQL service by clicking the "Start" button next to MySQL.

2. Opening CMD and Accessing MySQL

To access MySQL via CMD:

1. Open the Command Prompt.


2. Navigate to the MySQL bin directory. This is typically located at C:\xampp\mysql\bin.
3. Log in to MySQL with the root user (or another user if you have one).
4. Enter the MySQL root password when prompted.

3. Creating a Database

To create a new database, use the following syntax:

CREATE DATABASE database_name;

Example: CREATE DATABASE school;

4. Selecting the Database

Before creating tables, you need to select the database you just created:

USE database_name;

Example: USE school;

5. Creating Tables

To create a table, use the following syntax:

CREATE TABLE table_name (

column1_name column1_datatype constraints,

column2_name column2_datatype constraints,

columnN_name columnN_datatype constraints );

3
Example:
CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
birth_date DATE,
gender ENUM('M', 'F'),
enrollment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

6. Inserting Data into Tables

To insert data into a table, use the following syntax:

INSERT INTO table_name (column1_name, column2_name, ..., columnN_name)

VALUES (value1, value2, ..., valueN);

Example:

INSERT INTO students (first_name, last_name, birth_date, gender)

VALUES ('John', 'Doe', '2000-01-01', 'M');

7. Viewing the Data

To view the data in a table, use the following syntax:

SELECT * FROM table_name;

Example:

SELECT * FROM students;

4
Lab Sheet-2:Use of SQL Data Definition Language (DDL) to create the
COMPANY Database and create following tables in MySQL XAMPP.

EMPLOYEE(Ssn, Fname, Mnit, Lname, Bdate, Address, Sex, Salary, Super_ssn,Dno)


DEPARTMENT(Dname,Dnumber, Mgr_ssn,Mgr_start_date)
PROJECT (Pname,Pnumber, Plocation, Dnum)
DEPENDENT(Essn,Dependent_name,Sex,Bdate,Relationship)
DEPT_LOCATIONS (Dnumber, Dlocation)
WORKS_ON (Essn, Pno, Hours)
-- Create EMPLOYEE Table CREATE TABLE IF NOT EXISTS EMPLOYEE(
Ssn INT PRIMARY KEY,
Fname VARCHAR(50),
Mnit VARCHAR(20),
Lname VARCHAR(50),
Bdate DATE,
Address VARCHAR(100),
Sex CHAR(1),
Salary DECIMAL(10, 2),
Super_ssn INT, Dno INT,
FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber) );

-- Create DEPARTMENT Table CREATE TABLE IF NOT EXISTS DEPARTMENT (

Dname VARCHAR(50),
Dnumber INT PRIMARY KEY,
Mgr_ssn INT,
Mgr_start_date DATE,
FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn) );

-- Create PROJECT Table CREATE TABLE IF NOT EXISTS PROJECT (

Pname VARCHAR(50),
Pnumber INT PRIMARY KEY,
Plocation VARCHAR(100),
Dnum INT,
FOREIGN KEY (Dnum) REFERENCES DEPARTMENT(Dnumber) );

-- Create DEPENDENT Table


CREATE TABLE IF NOT EXISTS DEPENDENT (
Essn INT,
Dependent_name VARCHAR(50),
Sex CHAR(1),
Bdate DATE,
Relationship VARCHAR(20),
PRIMARY KEY (Essn, Dependent_name),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE(Ssn) );

-- Create DEPT_LOCATIONS Table

CREATE TABLE IF NOT EXISTS DEPT_LOCATIONS (


Dnumber INT,
Dlocation VARCHAR(100),
PRIMARY KEY (Dnumber, Dlocation),
FOREIGN KEY (Dnumber)
REFERENCES DEPARTMENT(Dnumber) );

5
-- Create WORKS_ON Table

CREATE TABLE IF NOT EXISTS WORKS_ON (


Essn INT,
Pno INT,
Hours DECIMAL(5, 2),
PRIMARY KEY (Essn, Pno),
FOREIGN KEY (Essn) REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Pno) REFERENCES PROJECT(Pnumber) );

Lab Sheet-3: Use Draw.io CASE Tool to draw the E-R database of COMPANY database and
import in word document.

6
Lab Sheet-4: Prepare the requirements (miniworld) of Patan Multiple Campus Library and
draw E-R diagrams and their relational database Tables.

Mini-World Scenario: Library Management System

Student Table: Contains a list of ISBNs (Books_Borrowed) indicating books borrowed by


each student, identified uniquely by Roll_Number, Year_of_Enrollment, and
Department_Name.

Department Table: Contains a list of ISBNs (Books_Assigned) indicating books assigned to


each course.

Books Table : Would connect Student_name and Book_ISBN. It shows the details of the
books.

Data Model Mapping (Logical Design)

1.BOOK

7
ISBN Title NO_copies Author Publication

2345 DSA 100 Ram Saha Vidyarthi

2.Student

Dpet_name ROll Name Enroll Phone Semester

BIT 04 Ram 2079 1234567 3rd

CSIT 04 Sita 2078 7654321 5th

3.Department

Dept_name Dept_location Dept_head

BIT Patan

CSIT Patan

8
Lab Sheet -5: Prepare the requirements (miniworld) of Students database of Patan Multiple

Campus BIT Department. Draw ER-Diagram and their relational database Tables.

⮚ Student Database contains Student. Each Student table contains roll no, name, address,
date of birth, sex and courses they take.
⮚ Course contains id which is course code, name, credit hour.
⮚ Lecturers contain id, name, courses that they teach. Lecturers may teach many students
and teach many courses in every new semester.

Data Model Mapping (Logical Design)

9
1. Teacher
Teacher_ID Teacher_Name Course_ID
101 Ram Lakhan Pal BIT201
102 Kshavraj Hari BIT202
103 Surya Man Singh BIT203

2. STUDENT
Student_ID Student_Name Course_ID
79020032 Ram Thapa BIT201
79020005 Dishant Paneru BIT202
79020001 Utsab Pradhan BIT203
79002008 Prashant Bandari BIT204

3. Department
Course_ID Course_Name
1 BIT
2 Bsc CSIT
3 BCA
4 BBA

4. SUBJECTS
Subject_ID Subject_Name
201 DSA
202 DBMS

10
203 NM
204 OS
205 POM

Lab Sheet -6:Use of SQL Constraints – The following constraints are commonly
used in SQL.
1. NOT NULL – Ensures that a column cannot have a NULL value.

To verify if NOT NULL has been successfully applied we describe the table.

11
As we can clearly see Ssn is not a null therefore this field cannot be null.

1. UNIQUE – Ensures that all values in a column are different.

In the image below, UNI in Key represents that the field is UNIQUE

PRIMARY KEY – A combination of NOT NULL and UNIQUE. Uniquely


identifies each row in a table.

12
PRI represents that the key is PRIMARY KEY in employee table

FOREIGN KEY – Creates a foreign key to reference another table


column value. The FOREIGN KEY constraint prevents invalid data from
being inserted into the foreign key column.
To add a FOREIGN KEY we first create 2 different tables with their
respective PRIMARY KEYS and later ALTER the table to add
FOREIGN KEY.

Employee Table

Department Table with FOREIGN KEY to Employee Table

13
Here MUL represents FOREIGN KEY.

CHECK – Ensures that the values in a column satisfies a specific


condition.

This is how we use CHECK constraints. In this table students with age 18
or greater can only be inserted and if the condition is not met it returns
error.

14
Lab Sheet -7:What is Referential Integrity?

Referential Integrity refers to the concept of maintaining consistency


between related tables when there are foreign key constraints defined. It
ensures that relationships between tables remain valid, meaning that foreign
key values in one table must match primary key values in another table. If a
foreign key references a primary key, the foreign key value must exist in the
referenced table.

1. FOREIGN KEY – Creates a foreign key to reference another table


column value. The FOREIGN KEY constraint prevents invalid data from
being inserted into the foreign key column.
To add a FOREIGN KEY we first create 2 different tables with their
respective PRIMARY KEYS and later ALTER the table to add
FOREIGN KEY.

Employee Table

Department Table with FOREIGN KEY to Employee Table

Here MUL represents FOREIGN KEY.

15
2. CHECK – Ensures that the values in a column satisfies a specific
condition.

This is how we use CHECK constraints. In this table students with age 18
or greater can only be inserted and if the condition is not met it returns
error.

Here Hari’s age is 17 which didn’t meet the condition resulting in an


error.

16
Lab Sheet -8: What are Domain Constraints? Explain the difference between
PRIMARY KEY and UNIQUE constraints. Use the constraints to create an
EMPLOYEE table.

Domain constraints refer to constraints that are applied to a column to


enforce data integrity and consistency within a specific domain or set of
values. These constraints ensure that only valid data is inserted or updated in
the column, according to predefined rules.

Differences between UNIQUE and PRIMARY KEY.


PRIMARY KEY is combination of NOT NULL and UNIQUE constraints
such that the fields with PRIMARY KEY are both unique and cannot be null
Whereas UNIQUE constraints only represent that the fields should be
unique but it can be null.
1. NOT NULL – Ensures that a column cannot have a NULL value.

To verify if NOT NULL has been successfully applied we describe the


table.

17
As we can clearly see Ssn is not a null therefore this field cannot be null.

2. UNIQUE – Ensures that all values in a column are different.

In the image below, UNI in Key represents that the field is UNIQUE

3. PRIMARY KEY – A combination of NOT NULL and UNIQUE.


Uniquely identifies each row in a table.

18
PRI represents that the key is PRIMARY KEY in employee table

4. FOREIGN KEY – Creates a foreign key to reference another table


column value. The FOREIGN KEY constraint prevents invalid data from
being inserted into the foreign key column.

19
To add a FOREIGN KEY we first create 2 different tables with their
respective PRIMARY KEYS and later ALTER the table to add
FOREIGN KEY.

Employee Table

Department Table with FOREIGN KEY to Employee Table

Here MUL represents FOREIGN KEY.

20
3. CHECK – Ensures that the values in a column satisfies a specific
condition.

This is how we use CHECK constraints. In this table students with age 18
or greater can only be inserted and if the condition is not met it returns
error.

Here Hari’s age is 17 which didn’t meet the condition resulting in an


error.

Lab Sheet -9:Create all the COMPANY Database Tables with all various
constraints (Primary, Domain, Foreign and Referential Integrity) and Insert 5
records in each table and then use the following SQL commands.

21
Employee Table

Inserting 5 values in Employee Table

Department Table

22
Project Table

Dependent Table

Dept_Location Table

23
Works_On Table

Use of SELECT and FROM clauses.


i. Retrieve the SSN values for all employees

ii. Retrieve all the attribute values of Employee

9.1. Use of SELECT, FROM and WHERE clauses.


i. Retrieve the name and address of all employees who work
for ‘Research’ department.
Research Department has Dno 5

24
ii. For every project located in ‘Kathmandu’, list the project
number, the controlling department number, manager’s last
name, address and birthdate.

9.2. Use of GROUP BY Clause


i. For each department, retrieve the department number, the
number of employees in the department, and their average
salary.

ii. For each project, retrive the project number, project name
and the number of employees who work on that project.

25
9.3. Use of HAVING clause
i. For each project on which more than two employees work,
retrieve the project number, project name and the number of
employees who work on that project.

9.4. Use of ORDER BY Clause


i. Retrieve a list of employees and the projects each works, in
order by the employee’s department and within each
department ordered alphabetically by employee last name.

26

You might also like