Dbms Final
Dbms Final
INDEX
9 Implement Triggers
10 Mini Project
Performance Indicators 1 2 3 4 5 6 7 8 9
(Maximum 5 marks per indicator)
Course Outcome
1. Knowledge
(Factual/Conceptual/Procedural/Metacognitive)
2. Describe
(Factual/Conceptual/Procedural/Metacognitive)
3. Demonstration
(Factual/Conceptual/Procedural/Metacognitive)
4. Strategy (Analyse & / or Evaluate)
(Factual/Conceptual/Procedural/Metacognitive)
5. Interpret/ Develop
(Factual/Conceptual/Procedural/Metacognitive)
Total (out of 25)
Source: *Anderson, L.W. (Ed.), Krathwohl, D.R. (Ed.), Airasian, P.W., Cruikshank, K.A., Mayer, R.E., Pintrich, P.R.,
Raths, J., & Wittrock, M.C. (2001). A taxonomy for learning, teaching, and assessing: A revision of Bloom’s
Taxonomy of Educational Objectives (Complete edition). New York: Longman.
DJS22EL406 On completion of the course, learner will be able to: Blooms Level
CO1 Analyze a case study and create ER diagram of the scenario and able to Analyse
create Database schema from this using SQL.
CO2 Write basic SQL queries to apply constraints, insert rows, do basic Create
operations like alter, update and delete, to use basic aggregate functions and
retrieve information from databases.
CO3 Write SQL queries to make joins and views on table. Create
Experiment No-1
DESIGN OF ER DIAGRAM
Aim: Design of ER Diagram
Theory:
Entity Relationship Diagrams are a major data modelling tool and will help organize the
data in your project into entities and define the relationships between the entities. This
process has proved to enable the analyst to produce a good database structure so that the
data can be stored and retrieved in a most efficient manner.
By using a graphical format, it may help communication about the design between the
designer and the user and the designer and the people who will implement it.
Components of an ERD
An ERD typically consists of four different graphical components:
1. Entity.
A data entity is anything real or abstract about which we want to store data. Entity
types fall into five classes: roles, events, locations, tangible things or concepts. E.g.
employee, payment, campus, book. Specific examples of an entity are called
instances. E.g. the employee John Jones, Mary Smith's payment, etc.
2. Relationship.
A data relationship is a natural association that exists between one or more
entities. E.g. Employees process payments.
3. Cardinality.
Defines the number of occurrences of one entity for a single occurrence of the
related entity. E.g. an employee may process many payments but might not
process any payments depending on the nature of her job.
4. Attribute.
A data attribute is a characteristic common to all or most instances of a particular
entity. Synonyms include property, data element, and field. E.g. Name, address,
Employee Number, pay rate are all attributes of the entity employee. An attribute or
combination of attributes that uniquely identifies one and only one instance of an
1
Name: Anees Alwani
SapID: 60002220242
Batch:E1 -1
entity is called a primary key or identifier. E.g. Employee Number is a primary key
for Employee.
Typically, you will start with a case study or perhaps a logical model of the system to be
developed. This document will demonstrate how to use the following process to convert that
information into an ERD.
The process has ten steps:
1. Identify Entities
Identify the roles, events, locations, tangible things or concepts about which the
end-users want to store data.
2. Find Relationships
Find the natural associations between pairs of entities using a relationship matrix.
3. Draw Rough ERD
Put entities in rectangles and relationships on line segments connecting the
entities.
4. Fill in Cardinality
Determine the number of occurrences of one entity for a single occurrence of the
related entity.
5. Define Primary Keys
Identify the data attribute(s) that uniquely identify one and only one occurrence of
each entity.
6. Draw Key-Based ERD
Eliminate Many-to-Many relationships and include primary and foreign keys in
each entity.
7. Identify Attributes
Name the information details (fields) which are essential to the system under
development.
8. Map Attributes
For each attribute, match it with exactly one entity that it describes.
9. Draw fully attributed ERD
Adjust the ERD from step 6 to account for entities or relationships discovered in
step 8.
10. Check Results
Does the final Entity Relationship Diagram accurately depict the system data?
2
Name: Anees Alwani
SapID: 60002220242
Batch:E1 -1
Problem Statement
Conclusion
We were able to visualize the ER diagram for a hospital management system and for a movie.
3
Experiment No-2
Relational Schema
Theory:
An entity type within ER diagram is turned into a table. You may preferably keep the same name for
the entity or give it a sensible name but avoid DBMS reserved words as well as avoid the use of
special characters.
Each attribute turns into a column (attribute) in the table. The key attribute of the entity is the primary
key of the table which is usually underlined. It can be composite if required but can never be null.
If you have a multi-valued attribute, take the attribute and turn it into a new entity or table of its own.
Then make a 1:N relationship between the new entity and the existing one. In simple words. 1. Create
a table for the attribute. 2. Add the primary (id) column of the parent entity as a foreign key within the
new table as shown below:
An entity type within ER diagram is turned into a table. You may preferably keep the same name for
the entity or give it a sensible name but avoid DBMS reserved words as well as avoid the use of
special characters.
1
Each attribute turns into a column (attribute) in the table. The key attribute of the entity is the primary
key of the table which is usually underlined. It can be composite if required but can never be null.
It is highly recommended that every table should start with its primary key attribute conventionally
named as TablenameID.
The initial relational schema is expressed in the following format writing the table names with the
attributes list inside a parentheses as shown below for
Persons and Phones are Tables. name, lastname, are Table Columns (Attributes).
2. Multi-Valued Attributes
If you have a multi-valued attribute, take the attribute and turn it into a new entity or table of its own.
Then make a 1:N relationship between the new entity and the existing one. In simple words. 1. Create
a table for the attribute. 2. Add the primary (id) column of the parent entity as a foreign key within the
new table as shown below:
personid within the table Phones is a foreign key referring to the personid of Persons
2
3. 1:1 Relationships
To keep it simple and even for better performances at data retrieval, I would personally recommend
using attributes to represent such relationship. For instance, let us consider the case where the Person
has or optionally has one wife.
4. 1:N Relationships
This is the tricky part ! For simplicity, use attributes in the same way as 1:1 relationship but we have
only one choice as opposed to two choices. For instance, the Person can have a House from zero to
many , but a House can have only one Person. To represent such relationship the personid as the
Parent node must be placed within the Child table as a foreign key but not the other way around as
shown next:
An entity type within ER diagram is turned into a table. You may preferably keep the same name for
the entity or give it a sensible name but avoid DBMS reserved words as well as avoid the use of
special characters.
3
Each attribute turns into a column (attribute) in the table. The key attribute of the entity is the primary
key of the table which is usually underlined. It can be composite if required but can never be null.
It is highly recommended that every table should start with its primary key attribute conventionally
named as TablenameID.
The initial relational schema is expressed in the following format writing the table names with the
attributes list inside a parentheses as shown below for
Persons and Phones are Tables. name, lastname, are Table Columns (Attributes).
2. Multi-Valued Attributes
If you have a multi-valued attribute, take the attribute and turn it into a new entity or table of its own.
Then make a 1:N relationship between the new entity and the existing one. In simple words. 1. Create
a table for the attribute. 2. Add the primary (id) column of the parent entity as a foreign key within the
new table as shown below:
personid within the table Phones is a foreign key referring to the personid of Persons
4
3. 1:1 Relationships
To keep it simple and even for better performances at data retrieval, I would personally recommend
using attributes to represent such relationship. For instance, let us consider the case where the Person
has or optionally has one wife. You can place the primary key of the wife within the table of the
Persons which we call in this case Foreign key as shown below.
Or vice versa to put the personid as a foreign key within the Wife table as shown below:
Persons( personid , name, lastname, email )
Wife ( wifeid , name , personid)
For cases when the Person is not married i.e. has no wifeID, the attribute can set to NULL
4. 1:N Relationships
This is the tricky part ! For simplicity, use attributes in the same way as 1:1 relationship but we have
only one choice as opposed to two choices. For instance, the Person can have a House from zero to
many , but a House can have only one Person. To represent such relationship the personid as the
Parent node must be placed within the Child table as a foreign key but not the other way around as
shown next:
It should convert to :
Persons( personid , name, lastname, email )
House ( houseid , num , address, personid)
5
5. N:N Relationships
We normally use tables to express such type of relationship. This is the same for N − ary relationship
of ER diagrams. For instance, The Person can live or work in many countries. Also, a country can
have many people. To express this relationship within a relational schema we use a separate table as
shown below:
Problem statement
We were able to visualize a relation schema for the ER diagrams of hospital Management
system and movie database.
6
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-3
Data Definition Language
Theory:
DDL- Data Definition Language (DDL) statements are used to define the database structure or
schema. Data Definition Language understanding with database schemas and describes how
the data should consist in the database, therefore language statements like CREATE TABLE
or ALTER TABLE belongs to the DDL. DDL is about "metadata".
DDL includes commands such as CREATE, ALTER and DROP statements. DDL is used to
CREATE, ALTER OR DROP the database objects (Table, Views, Users).
1) CREATE
a) To create a database
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
The create table command defines each column of the table uniquely. Each column has minimum of three
attributes.
• Name
• Data type
• Size(column width).
Each table column definition is a single clause in the create table syntax. Each table column definition is
separated from the other by a comma. Finally, the SQL statement is terminated with a semicolon.
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
2. ALTER
By The use of ALTER TABLE Command we can modify our exiting table.
Syntax:
ALTER TABLE <table_name>
ADD (<NewColumnName> <Data_Type>(<size>),......n)
Example:
ALTER TABLE Student
ADD (Age int(2), Marks int(3));
Syntax:
Example:
Syntax:
ALTER TABLE <table_name> MODIFY <column_name> <NewDataType>(<NewSize>)
Example:
Example:
3
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
3. RENAME
Syntax:
RENAME TABLE <OldTableName> TO <NewTableName>
Example:
RENAME table Student TO Stud;
4. DROP
Syntax:
DROP TABLE <table_name>
Example:
DROP TABLE Student;
5. TRUNCATE
Syntax:
TRUNCATE TABLE <Table_name>
Example:
TRUNCATE TABLE Student;
6-SHOW
To check available databases and tables
Syntax
SHOW DATABASES;
SHOW TABLES;
7- DESCRIBE
Example-
DESC stud;
CONSTRAINTS
Constraints enforce limits to the data or type of data that can be inserted/updated/deleted from a table. The whole
purpose of constraints is to maintain the data integrity during an update/delete/insert into a table.
4
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Types of constraints
• NOT NULL
• UNIQUE
• DEFAULT
• CHECK
• Key Constraints – PRIMARY KEY, FOREIGN KEY
NOT NULL:
By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL
values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update
a record without adding a value to this field.
PRIMARY KEY:
Primary key uniquely identifies each record in a table. It must have unique values and cannot contain nulls. In
the below example the ROLL_NO field is marked as primary key, that means the ROLL_NO field cannot have
duplicate and null values.
FOREIGN KEY:
Foreign keys are the columns of a table that points to the primary key of another table. They act as a cross-
reference between tables.
PROBLEM STATEMENT:
SOLUTION:
1) CREATE
a) To create a database
5
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
c) To create a Table
6
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
7
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
8
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
9
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
10
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
2) ALTER
11
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
3) RENAME
4) DROP
5) TRUNCATE
12
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
6) SHOW
13
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
7) DESCRIBE
14
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
CONCLUSION:
DDL commands like CREATE, ALTER, RENAME, DROP, and DESC empower efficient database
management. Constraints such as PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL ensure
data integrity. Mastery of these commands is vital for effective schema design, maintaining data
consistency, and enhancing proficiency in MySQL administration through regular practice.
15
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-4
Data Manipulation Language
Aim: Implement DML commands.
A data manipulation language (DML) is a family of computer languages including commands
permitting users to manipulate data in a database. This manipulation involves inserting data
into database tables, retrieving existing data, deleting data from existing tables and modifying
existing data. DML is mostly incorporated in SQL databases.
1) INSERT.
Syntax
Example
2) SELECT
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Syntax
Example-
3) UPDATE
The UPDATE statement is used to update existing records in a table.
Syntax
UPDATE table_name
SET column1=value1, column2=value2,...
WHERE some_column=some_value;
Example-
1) update stud
set Marks='75'
where reg_no=1;
2) update stud
where reg_no=2;
4) DELETE
This command removes one or more records from a table according to specified conditions.
Syntax
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Example-
1) INSERT
3
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
2) SELECT
3) UPDATE
4
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
4) DELETE
CONCLUSION:
The study of DML commands in SQL found that INSERT is good for adding data, UPDATE
can change it, DELETE should be used carefully because it permanently removes data, and
SELECT helps in finding and displaying data. Managing transactions is important for data
safety, and how well commands perform depends on database setup.
5
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-5
SELECT COMMANDS AND AGGREGATE FUNCTIONS
Aim: Implement all select commands.
The most commonly used SQL command is SELECT statement. SQL SELECT statement is
used to query or retrieve data from a table in the database. A query may retrieve information
from specified columns or from all of the columns in the table. To create a simple SQL
SELECT Statement, you must specify the column(s) name and the table name. The whole
query is called SQL SELECT Statement.
Theory
Syntax-
SELECT expressions
FROM tables
[WHERE conditions];
Examples-
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
4. Retrieve employee name and phone numbers of all employees residing at vile
parle.
select ename, phone_no
from employee, department
where location='vile parle' and dnumber=dno;
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
5. Retrieve employee name and phone numbers of all employees residing at vile
parle.
select e.ename, e.phone_no
from employee as e, department as d
where d.location='vile parle' and d.dnumber=e.dno;
3
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
9. Retrieve all employee names whose phone number starts with 99.
select ename
from employee
where phone_no like '99________';
4
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
10. Retrieve all employee names as employee_name for all employees working for
department no.3
select ename as employee_name
from employee
where dno=3;
11. Retrieve all employees in department no.1 whose salary is between 30000 and
60000
select *
from employee
where (salary between 30000 and 60000) and dno=1;
12. Retrieve employee id, name, phone number and department name of all
employees who work for computer department ordered alphabetically in
ascending order by employee name and descending order by department name.
select id, ename, phone_no,dname
from employee,department
5
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
6
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
15. Retrieve department name, department number and average salary for all
departments having average salary greater than 40000
7
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
having avg(salary)>40000;
Results:
In conclusion, through the exploration of SQL SELECT statements in MySQL, this
experiment has empowered us with fundamental querying skills. By mastering the
usage of WHERE clauses, aggregate functions, GROUP BY, ORDER BY, and
HAVING, we are now equipped to proficiently extract and manipulate data from
databases, fostering a deeper understanding of database management.
8
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-6
SQL JOINS
Aim: To implement joins in database
JOINS
INNER JOIN
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
LEFT JOIN
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows
in the right table (table2). The result is NULL in the right side when there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
RIGHT JOIN
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching
rows in the left table (table1). The result is NULL in the left side when there is no match.
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
Examples-
5) List all order numbers whether they are ordered by customers or not.
select cname, order_number
from customer right outer join order1 on order1.id=customer.customer_id;
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
3
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
LEFT JOIN:
4
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
RIGHT JOIN
5
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
INNER JOIN
6
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
EQUI JOIN
7
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
8
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-7
Views
Aim: To implement Views in database
Theory
"views" refer to virtual tables that are derived from the result of a predefined SQL query.
These views do not contain any data themselves; instead, they are dynamic representations of
data from one or more tables in a database. Views are useful for simplifying complex queries,
providing a layer of abstraction, and controlling access to data.
When you create a view in SQL, you define a query that selects data from one or more tables,
possibly applying filters, joins, or calculations. This query is stored as metadata in the
database, and when you query the view, the database engine dynamically executes the
underlying query and returns the result set as if it were a table.
Simplicity: Views can simplify complex queries by encapsulating logic into a single, easy-to-
use object. This can improve readability and maintainability of your SQL code.
Abstraction: Views provide a layer of abstraction over the underlying tables, allowing you to
hide the complexity of the database schema from end users or applications. Changes to the
underlying schema can be managed within the view definition without affecting applications
that use the view.
Security: Views can be used to control access to sensitive data by restricting the columns or
rows that users are allowed to see. By granting permissions to views instead of tables, you
can enforce fine-grained access control.
Performance: In some cases, views can improve performance by precomputing complex joins
or aggregations and storing the results, reducing the overhead of executing the same query
multiple times.
Overall, views are a powerful feature of SQL that enable developers and database
administrators to simplify complex queries, manage data access, and improve performance.
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Syntax
FROM table_name
WHERE condition;
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
3
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
4
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
5
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
6
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
7
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
8
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-8
Nested Queries
Aim: To use nested queries
Theory
A Subquery or Inner query or Nested query is a query within another SQL query and
embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
1) Find the customers who are borrowers from the bank and who appear in the list of
account holders.
SELECT cust_name
from borrower
where cust_name in ( select cust_name
from depositor);
2) Find the customers who are borrowers from the bank but do not hold an account.
SELECT cust_name
from borrower
where cust_name not in ( select cust_name
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
from depositor);
3) Find all customers who have balance greater than at least one customer located at
branch Mumbai.
select cust_name
from depositor
where balance> some ( select balance
from depositor
where branch='mumbai');
4) Find customers who have balance greater than or equal to that of each customer
located at Mumbai branch.
select cust_name
from depositor
where salary>=all ( select salary
from depositor
where branch='mumbai');
5) Find the customers who are borrowers from the bank and who appear in the list of
account holders using EXISTS.
SELECT cust_name
from borrower
where exists ( select *
from depositor
where borrower.cust_name=depositor.cust_name);
6) Find the customers who are borrowers from the bank but do not hold an account
using NOT EXISTS.
SELECT cust_name
from borrower
where not exists ( select *
from depositor
where borrower.cust_name=depositor.cust_name);
7) create a backup for borrower table by copying all records of borrower to a new table
borrower_bkp.
insert into borrower_bkp
select * from borrower;
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Through the exploration of nested queries in SQL, this experiment has provided us with a
deeper understanding of query composition and data manipulation. By comprehending the
syntax and applications of subqueries, we are now adept at leveraging nested queries to refine
and optimize their database interactions effectively.
3
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
Experiment No-8
Triggers
Aim: To use triggers
Theory
A trigger is a set of actions that are run automatically when a specified change operation
(SQL INSERT, UPDATE, or DELETE statement) is performed on a specified table. Triggers
are useful for tasks such as enforcing business rules, validating input data, and keeping an
audit trail.
A trigger is a named database object that is associated with a table, and it activates when a
particular event (e.g. an insert, update or delete) occurs for the table. The statement CREATE
TRIGGER creates a new trigger in MySQL.
Syntax
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
trigger_body
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
Example
1) delimiter //
create trigger depocheck
before insert on depositor
1
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1
2
Name: Anees Alwani
SapID: 60002220242
Batch: E1-1