Unit Iii Normalization
Unit Iii Normalization
Unit Iii Normalization
History of ER models
Peter Chen proposed ER Diagrams in 1971 to create a uniform convention used as a conceptual modeling tool.
Components of ER Diagram
Entities
An entity can be either a living or non-living component.
It showcases an entity as a rectangle in an ER diagram.
Weak Entity
An entity that makes reliance over another entity is called a weak entity
You showcase the weak entity as a double rectangle in ER Diagram.
In the example below, school is a strong entity because it has a primary key attribute - school number.
Unlike school, the classroom is a weak entity because it does not have any primary key and the room number
here acts only as a discriminator.
Attribute
An attribute exhibits the properties of an entity.
You can illustrate an attribute with an oval shape in an ER diagram.
Key Attribute
Key attribute uniquely identifies an entity from an entity set.
It underlines the text of a key attribute.
For example: For a student entity, the roll number can uniquely identify a student from a set of students.
Composite Attribute
An attribute that is composed of several other attributes is known as a composite attribute.
An oval showcases the composite attribute, and the composite attribute oval is further connected with other
ovals.
Multivalued Attribute
Some attributes can possess over one value, those attributes are called multivalued attributes.
The double oval shape is used to represent a multivalued attribute.
Derived Attribute
An attribute that can be derived from other attributes of the entity is known as a derived attribute.
In the ER diagram, the dashed oval represents the derived attribute.
Relationship
The diamond shape showcases a relationship in the ER diagram.
It depicts the relationship between two entities.
In the example below, both the student and the course are entities, and study is the relationship between them.
One-to-One Relationship
When a single element of an entity is associated with a single element of another entity, it is called a one-to-one
relationship.
For example, a student has only one identification card and an identification card is given to one person.
One-to-Many Relationship
When a single element of an entity is associated with more than one element of another entity, it is called a one-
to-many relationship
For example, a customer can place many orders, but an order cannot be placed by many customers.
Many-to-One Relationship
When more than one element of an entity is related to a single element of another entity, then it is called a
many-to-one relationship.
For example, students have to opt for a single course, but a course can have many students.
Many-to-Many Relationship
When more than one element of an entity is associated with more than one element of another entity, this is
called a many-to-many relationship.
For example, you can assign an employee to many projects and a project can have many employees.
Hospital ER Model
Company ER Model
createtable BRANCH (
branch_name varchar(20) primary key, branch_city varchar(20), assets int );
createtable CUSTOMER (
customer_name varchar(20) primary key, customer_street varchar(20), customer_city
varchar(20) );
-- b) Find all customers who have a loan or an account or both (eliminate duplicates if
exists)
-- c) Find all customers who have a loan but not an account (eliminate duplicates if exists)
===============================================================================
INSERT INTO TEACHERS VALUES (1, 'SELENIUM', 'TOM');
ROLLBACK;
INSERT INTO TEACHERS VALUES (2, 'UFT', 'SAM');
INSERT INTO TEACHERS VALUES (3, 'JMETERE', 'TONK');
COMMIT;
===============================================================================
===============================================================================
We can do configuration such that a COMMIT statement gets executed by default whenever an INSERT or
DELETE statement is run. This is done by making the AUTOCOMMIT environment variable to ON.
Syntax:
SET AUTOCOMMIT ON;
Again, this can be turned off by making the AUTOCOMMIT environment variable to OFF.
Syntax:
SET AUTOCOMMIT OFF;
===============================================================================
5. Implement different
types of referential and
integrity constraints on
Relation Database.
Types of Integrity Constraints
Domain integrity constraint contains a certain set of rules or conditions to restrict the kind of
attributes or values a column can hold in the database table. The data type of a domain can be string,
integer, character, DateTime, currency, etc.
Entity Integrity Constraint is used to ensure that the primary key cannot be null.
Referential Integrity Constraint ensures that there must always exist a valid relationship between
two relational database tables.
Key Constraints are the set of entities that are used to identify an entity within its entity set uniquely.
When no value is defined for a column, the DEFAULT Constraint provides a default value.
================================================================
SELECT b.branch_name, a.account_number
FROM branch b
INNER JOIN account a ON b.branch_name = a.branch_name;
================================================================
================================================================
Insert (1, ‘rajesh’, 45) , ( 2, ‘anand’, 41), (3, ‘sathish’, 38), (4, ‘gopi’, 36)
Sqllab9.sql
DECLARE
rollno student.sno%type;
snm student.sname%type;
nage student.age%type;
BEGIN
rollno := &sno;
snm := '&sname';
nage := &age;
COMMIT;
SAVEPOINT save1;
rollno := &sno;
snm := '&sname';
nage := &age;
ROLLBACK TO save1;
END;
/
10. Write a trigger that
automatically deletes
students when they
graduate
Classes of Triggers in SQL Server
There are three types or classes of triggers in SQL Server, DML, DDL, and Logon triggers:
DML (Data Manipulation Language) Triggers – Fire when an INSERT, UPDATE, or DELETE
event occurs on a table, view, etc.
DDL (Data Definition Language) Triggers – Fire when a CREATE, ALTER, or DROP event
occurs on a database object.
Logon Triggers – Fire when a user logs into a database i.e. logon event.
BEFORE Triggers – This type of trigger fires before the data has been committed into the
database.
AFTER Triggers – This type of trigger fires after the event it is associated with completes and
can only be defined on permanent tables.
INSTEAD OF Triggers – This type of trigger fires instead of the event it is associated with and
can be applied to tables or views.
CREATE TABLE ex10 (
id NUMBER PRIMARY KEY,
name VARCHAR2(10), status VARCHAR2(15));
INSERT INTO ex10 (id, name, status) VALUES (1, 'Alice', 'enrolled');
INSERT INTO ex10 (id, name, status) VALUES (2, 'Bob', 'graduated');
INSERT INTO ex10 (id, name, status) VALUES (3, 'Charlie', 'enrolled');
COMMIT;
COMMIT;
END;
/
BEGIN
p_upd_schdcde;
END;
/
12. a) Create a cursor to
update the salary of
employees in EMP table.
12. b) Write a PL/SQL
program to raise an
Exception when the bonus
exceeds salary.
13. Design and
implementation real time
project with database
connection.