You are on page 1of 51

Database Systems Lab Manual

CSE 304
Department offering the LAB course
Computer Science and Engineering

Course Pre-requisites
CSE 103, CSE 201

Course Description
This course will familiar the students with the concepts of Database Management Systems and they
will learn how to use this concept in application level. At the end of the class, we expect the students
to be able to design database structure and implement the structure in computer program.

LAB Course Objectives:


1. To explain secure basic and complex SQL queries.
2. To emphasis on implementing relational database and learn to manage DBMS product

Intended learning outcomes of the course (ILOs)


1. Learn secured basic and complex data manipulative SQL statements.
2. Design, analyze, and implement a database for a real-world system

Mapping of Course LO and PLO


Program Learning Outcome (PLO)
1 2 3 4 5 6 7 8 9 10 11 12
Learning Outcome
(LO) of the Course
ILO1 MJ MJ MN MN MN
ILO2 MJ MJ MJ MJ MJ MN MJ MJ MN

Database Systems Lab (CSE 304) Page 1 of 52


Lab Instructions:

 Strictly observe the instructions given by the Faculty / Lab. Instructor.


 NO FOOD, DRINK, IN ANY FORM is allowed in the lab.
 TURN OFF CELL PHONES! If you need to use it, please keep it in bags.
 Avoid all horseplay in the laboratory. Do not misbehave in the computer laboratory. Work
quietly.
 Save often and keep your files organized.
 Don’t change settings and surf safely.
 Do not reboot, turn off, or move any workstation or PC.
 Do not load any software on any lab computer (without prior permission of Faculty and
Technical Support Personnel). Only Lab Operators and Technical Support Personnel are
authorized to carry out these tasks. 
 Do not reconfigure the cabling/equipment without prior permission.
 Do not play games on systems. 
 Violation of the above rules and etiquette guidelines will result in disciplinary action. 

Database Systems Lab (CSE 304) Page 2 of 52


Continuous Assessment Practical

Exp
NAME OF EXPERIMENT Date Sign Remark
No

1 Study of DBMS, RDBMS, and MySQL database

2 Database Connectivity
Implement Data Definition Language (Create, Alter, Drop,
3 Truncate, and Rename)

Implement Data Manipulation Language (Insert, Update, and


4 Delete).

5 SUB QUERIES AND JOIN

6 Views

7 Procedure

8 Cursor

9 Trigger

10 Concepts of Normalization

Checking Normalization of a database table (Third normal


11
form)

CONTENTS

Database Systems Lab (CSE 304) Page 3 of 52


Exp PAGE
NAME OF EXPERIMENT
No NO.

1 Study of DBMS, RDBMS, and MySQL database 5

2 Database Connectivity 11

3 Implement Data Definition Language (Create, Alter, Drop, Truncate, and Rename) 20

4 Implement Data Manipulation Language (Insert, Update, and Delete). 24

5 SUB QUERIES AND JOIN 28

6 Views 32

7 Procedure 35

8 Cursor 38

9 Trigger 40

10 Concepts of Normalization 42

11 Checking Normalization of a database table (Third normal form) 44

Database Systems Lab (CSE 304) Page 4 of 52


Experiment 1
Study of DBMS, RDBMS, and MySQL database

AIM: STUDY OF DBMS, RDBMS, ORDBMS WITH ADVANTAGES AND DISADVANTAGE

Database Systems Lab (CSE 304) Page 5 of 52


DBMS (DATABASE MANAGEMENT SYSTEM)
A DBMS is software that allows creation, definition and manipulation of database, allowing
users to store, process and analyze data easily. DBMS provides us with an interface or a tool, to
perform various operations like creating database, storing data in it, updating data, creating tables in
the database and a lot more.
DBMS also provides protection and security to the databases. It also maintains data
consistency in case of multiple users.
Here are some examples of popular DBMS used these days:
 MySql
 Oracle
 SQL Server
 IBM DB2
 PostgreSQL
 Amazon SimpleDB (cloud based) etc.

CHARACTERISTICS OF DATABASE MANAGEMENT SYSTEM


A database management system has following characteristics:
1. Data stored into Tables: Data is never directly stored into the database. Data is stored into
tables, created inside the database. DBMS also allows having relationships between tables
which makes the data more meaningful and connected. You can easily understand what type
of data is stored where by looking at all the tables created in a database.
2. Reduced Redundancy: In the modern world hard drives are very cheap, but earlier when
hard drives were too expensive, unnecessary repetition of data in database was a big problem.
But DBMS follows Normalization which divides the data in such a way that repetition is
minimal.
3. Data Consistency: On Live data, i.e. data that is being continuously updated and added,
maintaining the consistency of data can become a challenge. But DBMS handles it all by
itself.
4. Support Multiple user and Concurrent Access: DBMS allows multiple users to work on it
(update, insert, and delete data) at the same time and still manages to maintain the data
consistency.
5. Query Language: DBMS provides users with a simple Query language, using which data
can be easily fetched, inserted, deleted and updated in a database.

Database Systems Lab (CSE 304) Page 6 of 52


6. Security: The DBMS also takes care of the security of data, protecting the data from un-
authorized access. In a typical DBMS, we can create user accounts with different access
permissions, using which we can easily secure our data by restricting user access.
7. DBMS supports transactions, which allows us to better handle and manage data integrity in
real world applications where multi-threading is extensively used.
ADVANTAGES OF DBMS
 Segregation of application program.
 Minimal data duplicity or data redundancy.
 Easy retrieval of data using the Query Language.
 Reduced development time and maintenance need.
 With Cloud Datacenters, we now have Database Management Systems capable of storing
almost infinite data.
 Seamless integration into the application programming languages which makes it very easier
to add a database to almost any application or website.
DISADVANTAGES OF DBMS
 It's Complexity
 Except MySQL, which is open source, licensed DBMSs are generally costly.
 They are large in size.
RDBMS (RELATIONAL DATABASE MANAGEMENT SYSTEM)
A Relational Database management System (RDBMS) is a database management system
based on the relational model introduced by E.F Codd. In relational model, data is stored in relations
(tables) and is represented in form of tuples (rows).
RDBMS is used to manage Relational database. Relational database is a collection of organized set
of tables related to each other, and from which data can be accessed easily. Relational Database is
the most commonly used database these days.
In relational model in which data is stored in multiple tables where tables are related to each other
using primary keys and foreign keys and indexes. RDBMS uses database normalization techniques to
avoid redundancy in tables. It helps to fetch data faster using SQL query. It is widely used by
enterprises and software developers to store large amount of complex data
Examples:
 SQL server,
 Oracle
 MySQL
 MariaDB

Database Systems Lab (CSE 304) Page 7 of 52


 SQLite
IMPORTANT CONCEPT RELATED TO RDBMS:
TABLE
In Relational database model, a table is a collection of data elements organized in terms of rows and
columns. A table is also considered as a convenient representation of relations. But a table can have
duplicate row of data while a true relation cannot have duplicate data. Table is the simplest form of
data storage. Below is an example of an Employee table.

ID Name Age Salary


1 Adam 34 13000
2 Alex 28 15000
3 Stuart 20 18000
4 Ross 42 19020

TUPLE
A single entry in a table is called a Tuple or Record or Row. A tuple in a table represents a set of
related data. For example, the above Employee table has 4 tuples/records/rows.
Following is an example of single record or tuple.

1 Adam 34 13000
ATTRIBUTE
A table consists of several records (row), each record can be broken down into several smaller parts
of data known as Attributes. The above Employee table consists of four
attributes, ID, Name, Age and Salary.
ATTRIBUTE DOMAIN
When an attribute is defined in a relation (table), it is defined to hold only a certain type of values,
which is known as Attribute Domain. Hence, the attribute Name will hold the name of employee
for every tuple. If we save employee's address there, it will be violation of the Relational database
model.

Name

Adam
ADVANTAGES OF RDBMS
Alex
 It is easy to use.
Stuart - 9/401, OC Street, Amsterdam
 It is secured in nature.
 The data manipulation can be done.
 It limits redundancy and replication of the data.
Database Systems Lab (CSE 304) Page 8 of 52
 It offers better data integrity.
 It provides better physical data independence.
 It offers logical database independence i.e. data can be viewed in different ways by the
different users.
 It provides better backup and recovery procedures.
 It provides multiple interfaces.
 Multiple users can access the database which is not possible in DBMS.
DISADVANTAGES OF RDBMS
 Software is expensive.
 Complex software refers to expensive hardware and hence increases overall cost to avail the
RDBMS service.
 It requires skilled human resources to implement.
 Certain applications are slow in processing.
 It is difficult to recover the lost data.
MySQL DATABASE
MySQL, is one of the most popular Open Source SQL database management systems. MySQL is a
fast, easy-to-use RDBMS being used for many small and big businesses. MySQL is developed,
marketed, and supported by MySQL AB, which is a Swedish company.
MySQL is becoming so popular because of many good reasons:
 MySQL is released under an open-source license. So you have nothing to pay to use it.
 MySQL is a very powerful program in its own right. It handles a large subset of the
functionality of the most expensive and powerful database packages.
 MySQL uses a standard form of the well-known SQL data language.
 MySQL works on many operating systems and with many languages including PHP
 MySQL works very quickly and works well even with large data sets.
 MySQL is very friendly to PHP, the most appreciated language for web development.
 MySQL supports large databases, up to 50 million rows or more in a table.
 MySQL is customizable
CONCLUSION: Thus we, have studied DBMS, and RDBMS with its advantages, disadvantages
and mysql database successfully.
Viva Voce Question
1. Define Database? What are DBMS and RDBMS?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

Database Systems Lab (CSE 304) Page 9 of 52


___________________________________________________________________________
________________
________________________________________________________________
_____________________________________________________________

2. What do you understand by Data Redundancy?


___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
________________
___________________________________________________________________________
__________________________________________________

3. What is the difference between DBMS and RDBMS?


___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
______________________________________________________________________

Signature of Subject Teacher

Database Systems Lab (CSE 304) Page 10 of 52


Experiment 2
Database Connectivity

2.1 Database Connectivity

Database Systems Lab (CSE 304) Page 11 of 52


Database Systems Lab (CSE 304) Page 12 of 52
2.2 Database Connectivity

Database Systems Lab (CSE 304) Page 13 of 52


Database Systems Lab (CSE 304) Page 14 of 52
Database Systems Lab (CSE 304) Page 15 of 52
Database Systems Lab (CSE 304) Page 16 of 52
2.3 Database Connectivity (Java Database Connectivity)
Database Systems Lab (CSE 304) Page 17 of 52
Database Systems Lab (CSE 304) Page 18 of 52
Database Systems Lab (CSE 304) Page 19 of 52
Experiment 3
Implement Data Definition Language (DDL)
(Create, Alter, Drop, Truncate, and Rename)

Database Systems Lab (CSE 304) Page 20 of 52


AIM: Consider the database for an organization. Write the queries for the following
 create the database
 select the current database
 Create the following tables.
a. employee (emp_no,emp_name,DOB, address, doj, mobile_no, dept_no, salary).
b. department (dept_no, dept_name, location).
 Include necessary constraints.
 List all the tables in the current database
 Display the structure of the employee table
 Add a new column Designation to the employee table
 Drop the column location from Dept table
 Drop the tables
 Delete the database

OBJECTIVES
To understand DDL commands.

THEORY
DATABASE QUERIES:
Before creating any tables, MySQL requires you to create a database by executing the
CREATE DATABASE command.
 Create a database
CREATE DATABASE <database name>
 Delete a database
DROP DATABASE <database name>
 Select the database
USE <database name>
 List all databases
SHOW databases;
 Rename a database
ALTER DATABASE <database name> RENAME <new database name>

TABLE QUERIES:
 To Create a table
Database Systems Lab (CSE 304) Page 21 of 52
CREATE TABLE <tablename> (<fieldname>< fieldtype>(<fieldsize>) , …)
 List all tables in the current database
SHOW tables;
 Show table format with column names and data types
DESCRIBE <table name>
 Modify the structure of table
ALTER TABLE <table name> <alter specifications>
ALTER TABLE <table name> DROP COLUMN <column name>
ALTER TABLE<table name> ADD COLUMN <column name> datatype>(<size>)
 Delete the table
DROP TABLE <table name>
CONSTRAINTS:
 Primary key  A PRIMARY KEY constraint for a table enforces the table to accept unique
data for a specific column and this constraint create a unique index for accessing the table
faster
 UNIQUE  The UNIQUE constraint in Mysql does not allow to insert a duplicate value in a
column.
 NOT NULL In Mysql NOT NULL constraint allows to specify that a column can not
contain any NULL value.
 FOREIGN KEY A FOREIGN KEY in mysql creates a link between two tables by one
specific column of both table. The specified column in one table must be a
 PRIMARY KEY and referred by the column of another table known as FOREIGN KEY.
 CHECK  The CHECK constraint determines whether the value is valid or not from a
logical expression.
 DEFAULT  While inserting data into a table, if no value is supplied to a column, then the
column gets the value set as DEFAULT

PROCEDURE
i. CREATE DATABASE command
ii. USE DATABASE command
iii. CREATE TABLE command
iv. PRIMARY KEY,NOT NULL etc
v. SHOW TABLES command
vi. DESCRIBE TABLE command
vii. ALTER TABLE command
viii. ALTER TABLE command
ix. DROP TABLE command
x. DROP DATABASE command

RESULT:
The DDL commands have been executed successfully.
OUTPUT
PROBLEMS
Database Systems Lab (CSE 304) Page 22 of 52
1. Consider the database for a college and design an ER diagram. Write the query for the
following.
i. Create the tables:
Student (sid, sname, sex, dob,dno)
Department (dno, dname)
Faculty (F_id, fname, designation, salary,dno)
Course (cid, cname, credits,dno)
Register (sid,cid,sem )
Teaching (f_id,cid,sem)
Hostel(hid,hname,seats,)

ii. Include the necessary constraints NOT NULL, DEFAULT, CHECK, and PRIMARY
KEY, UNIQUE.
iii. Create a database college
iv. Use college as the current database
v. Display all the tables in college database
vi. Describe the structure of all tables
vii. Modify the student table to add a new field ‘grade’

2. Consider the database for a banking enterprise. Write the queries for the below
questions.
i. Create the following tables

ii. Include necessary constraints.


iii. Tables are created under the database ‘bank’
iv. Display all the tables in bank database
v. Describe the structure of all tables
vi. Delete tables

Database Systems Lab (CSE 304) Page 23 of 52


Experiment 4
Implement Data Manipulation Language (DML)

Database Systems Lab (CSE 304) Page 24 of 52


Database Systems Lab (CSE 304) Page 25 of 52
Database Systems Lab (CSE 304) Page 26 of 52
Database Systems Lab (CSE 304) Page 27 of 52
Experiment 5
SUB QUERIES AND JOIN

Database Systems Lab (CSE 304) Page 28 of 52


Database Systems Lab (CSE 304) Page 29 of 52
Database Systems Lab (CSE 304) Page 30 of 52
Database Systems Lab (CSE 304) Page 31 of 52
Experiment 6
VIEWS

Database Systems Lab (CSE 304) Page 32 of 52


Database Systems Lab (CSE 304) Page 33 of 52
Database Systems Lab (CSE 304) Page 34 of 52
Experiment 7
PROCEDURE

Database Systems Lab (CSE 304) Page 35 of 52


Database Systems Lab (CSE 304) Page 36 of 52
Database Systems Lab (CSE 304) Page 37 of 52
Experiment 8
CURSOR

Database Systems Lab (CSE 304) Page 38 of 52


Database Systems Lab (CSE 304) Page 39 of 52
Experiment 9
TRIGGER

Database Systems Lab (CSE 304) Page 40 of 52


Database Systems Lab (CSE 304) Page 41 of 52
Experiment 10
CONCEPTS OF NORMALIZATION

Database Systems Lab (CSE 304) Page 42 of 52


Database Systems Lab (CSE 304) Page 43 of 52
Experiment 11
Checking Normalization of a database table (Third normal form)

Database Systems Lab (CSE 304) Page 44 of 52


Database Systems Lab (CSE 304) Page 45 of 52
Database Systems Lab (CSE 304) Page 46 of 52
Database Systems Lab (CSE 304) Page 47 of 52
Database Systems Lab (CSE 304) Page 48 of 52
Database Systems Lab (CSE 304) Page 49 of 52
END

Database Systems Lab (CSE 304) Page 50 of 52

You might also like