Professional Documents
Culture Documents
Components of an ER Diagram
An ER Diagram consists of the following components:
1. Entity
2. Attributes
3. Relationship
1. Entity
An entity may be an object, place, person, or an event which stores data in the
database. In an entity-relationship diagram, an entity is represented by a
rectangle. Student, course, manager, employee, patient, etc. are examples
of an entity.
Entity set: It is a set (or collection) of entities of the same type which share
the similar properties, or attributes.
For example,the group of people who are lecturers in a university can be
de?ned as an entity set lecturer. Similarly, the entity set of students might
represent the group of all students in the university.
An entity can be characterized into two types:
1. Strong entity: This type of entity has a primary key attribute which
uniquely identifies each record in a table. In the ER diagram, a strong
entity is usually represented by a single rectangle.
2. Weak entity:An entity does not have a primary key attribute and
depends on another strong entity via foreign key attribute. In the ER
diagram, a weak entity is usually represented by a double rectangle.
3. Attributes: An attribute in an Entity-Relationship Model describes the
properties or characteristics of an entity. It is represented by an oval or
ellipse shape in the ER diagram. Every oval shape represents one
attribute and is directly connected to its entity which is in the rectangle
in shape.
3. Relationship
A relationship in Entity-Relationship Model is used to describe the relation
between two or more entities. It is represented by a diamond shape in the ER
diagram. For example, student study in college, employee works in a
department. Here, ‘study in’ and ‘works in’ are the relationships.
Degree of Relationship
A relationship where a number of different entity set participate is called as
degree of a relationship.
Degree of relationship can be categorized into the following types:
1. Unary Relationship
2. Binary Relationship
3. Ternary Relationship
4. n-ary Relationship
1. Unary Relationship:
Table
The following is the outcome of a successful conversion:
1. One-to-one (1:1)
2. One-to-many (1:M)
3. Many-to-many (M:N)
1. One-to-one
o In a one-to-one relationship, one occurrence of an entity relates to only one occurrence
in another entity.
o A one-to-one relationship rarely exists in practice.
o For example: if an employee is allocated a company car then that car can only be
driven by that employee.
o Therefore, employee and company car have a one-to-one relationship.
2. One-to-many
o In a one-to-many relationship, one occurrence in an entity relates to many occurrences
in another entity.
o For example: An employee works in one department, but a department has many
employees.
o Therefore, department and employee have a one-to-many relationship.
3. Many-to-many
o In a many-to-many relationship, many occurrences in an entity relate to many
occurrences in another entity.
o Same as a one-to-one relationship, the many-to-many relationship rarely exists in
practice.
o For example: At the same time, an employee can work on several projects, and a
project has a team of many employees.
o Therefore, employee and project have a many-to-many relationship.
OR
Generalization, Specialization and
Aggregation in ER Model
DBMS Generalization
In the below example, it can be seen that the employee is a high-level entity
which is divided into three sub-entities (Ram, Shyam, and Mohan). The sub-
entities are the names of the employees (relating to the high-level entity).
Therefore, splitting a high-level entity into a low-level entity is
called specialization.
DBMS Aggregation
Example:
The student and program are two entity set and related through a relationship
set write. Suppose, a student who writes any program must require a
programming language installed in their system. So, there will be another
relationship set requires. You need to connect the relationship
set requires with an entity set programming language and relationship
set write. But, we can connect only entity set to a relationship set.
One relationship set cannot be connected with another relationship set; for this,
we need aggregation. Here, the write (relationship set) will treat like a higher-
level entity and can be associated with a relationship set requires. So,
aggregation is needed when you express a relationship set with another
relationship set.
For Example, Employee working for a project may require some machinery.
So, REQUIRE relationship is needed between relationship WORKS_FOR and
entity MACHINERY. Using aggregation, WORKS_FOR relationship with its
entities EMPLOYEE and PROJECT is aggregated into single entity and
relationship REQUIRE is created between aggregated entity and
MACHINERY.
Views in DBMS
Views in SQL are considered as a virtual table. A view also contains rows and
columns. To create the view, we can select the fields from one or more tables present
in the database. A view can either have specific rows based on certain condition or
all the rows of a table.
A view is nothing more than a SQL statement that is stored in the database with an
associated name. A view is actually a composition of a table in the form of a
predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view can be
created from one or many tables which depends on the written SQL query to create
a view.
Views, which are a type of virtual tables allow users to do the following −
• Structure data in a way that users or classes of users find natural or
intuitive.
• Restrict access to the data in such a way that a user can see and
(sometimes) modify exactly what they need and no more.
• Summarize data from various tables which can be used to generate
reports.
Creating Views
Database views are created using the CREATE VIEW statement. Views can be
created from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the
specific implementation.
The basic CREATE VIEW syntax is as follows −
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
You can include multiple tables in your SELECT statement in a similar way as you use
them in a normal SQL SELECT query.
Following is an example to create a view from the CUSTOMERS table. This view
would be used to have customer name and age from the CUSTOMERS table.
The WITH CHECK OPTION in this case should deny the entry of any NULL values in
the view's AGE column, because the view is defined by data that does not have a
NULL value in the AGE column.
Updating a View
A view can be updated under certain conditions which are given below −
• The SELECT clause may not contain the keyword DISTINCT.
• The SELECT clause may not contain summary functions.
• The SELECT clause may not contain set functions.
• The SELECT clause may not contain set operators.
• The SELECT clause may not contain an ORDER BY clause.
• The FROM clause may not contain multiple tables.
• The WHERE clause may not contain subqueries.
• The query may not contain GROUP BY or HAVING.
• Calculated columns may not be updated.
• All NOT NULL columns from the base table must be included in the
view in order for the INSERT query to function.
So, if a view satisfies all the above-mentioned rules then you can update that view.
The following code block has an example to update the age of Ramesh.
This would ultimately update the base table CUSTOMERS and the same would
reflect in the view itself. Now, try to query the base table and the SELECT statement
would produce the following result.
Inserting Rows into a View
Rows of data can be inserted into a view. The same rules that apply to the UPDATE
command also apply to the INSERT command.
Here, we cannot insert rows in the CUSTOMERS_VIEW because we have not
included all the NOT NULL columns in this view, otherwise you can insert rows in a
view in a similar way as you insert them in a table.
This would ultimately delete a row from the base table CUSTOMERS and the same
would reflect in the view itself. Now, try to query the base table and the SELECT
statement would produce the following result.
Dropping Views
Obviously, where you have a view, you need a way to drop the view if it is no longer
needed. The syntax is very simple and is given below −
DROP VIEW view_name;
Following is an example to drop the CUSTOMERS_VIEW from the CUSTOMERS
table.
Student_Marks
Syntax:
1. CREATE VIEW view_name AS
2. SELECT column1, column2.....
3. FROM table_name
4. WHERE condition;
Query:
1. CREATE VIEW DetailsView AS
2. SELECT NAME, ADDRESS
3. FROM Student_Details
4. WHERE STU_ID < 4;
Just like table query, we can query the view to view the data.
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query:
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have
to be performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o The data type of domain includes string, character, integer, time, date, currency,
etc. The value of the attribute must be available in the corresponding domain.
2. Entity integrity constraints
(Entity is further converted into table. So its constraints on table)
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation and
if the primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key.
A primary key can contain a unique and null value in the relational table.
NOT NULL:
NOT NULL constraint makes sure that a column does not hold NULL value.
When we don’t provide value for a particular column while inserting a record
into a table, it takes NULL value by default. By specifying NULL constraint, we
can be sure that a particular column(s) cannot have NULL values.
Example:
DEFAULT:
The DEFAULT constraint provides a default value to a column when there is
no value provided while inserting a record into a table.
CHECK:
This constraint is used for specifying range of values for a particular column
of a table. When this constraint is being set on a column, it ensures that the
specified column must have the value falling in the specified range.
In the above example we have set the check constraint on ROLL_NO column
of STUDENT table. Now, the ROLL_NO field must have the value greater than
1000.
Key constraints:
PRIMARY KEY and Foreign Keys
Foreign Key
-- Create a table
CREATE TABLE Department(DEPTNO,DNAME VARCHAR(30));
-- Create a table
CREATE TABLE Employee1(ENO INTEGER PRIMARY KEY, ENAME VARCHAR(20),
JOD VARCHAR(15),DEPTNO INTEGER references Department(DEPTNO) on
delete restrict);
Observations:
Case:1
Drop TABLE Department;
Gives output:
Error: FOREIGN KEY constraint failed
Case:2
Perform following query removing delete restrictions
Drop TABLE Department;
References:
https://www.youtube.com/watch?v=V_zL94zyuYc
https://www.mycompiler.io/new/mysql
Database Schema
Schema diagram
The design of the database is called a schema. This tells us about the structural view of the
database. It gives us an overall description of the database. A database schema defines how
the data is organised using the schema diagram.
Example: Let us suppose we have three tables Employee, Department and Project. So, we
can represent the schema of these three tables using the schema diagram as follows. In
this schema diagram, Employee and Department are related and the Employee and Project
table are related.
There are three levels of the schema. The three levels of the database schema are defined
according to the three levels of data abstraction .
• View Schema
• Logical Schema
• Physical Schema
View Schema or External Schema
View Schema defines the design of the database at the view level of the data abstraction. It
defines how an end-user will interact with the database system. There are many view schema
for a database system. Each view schema defines the view of data for a particular group of
people. It shows only those data to a view group in which they are interested and hides the
remaining details from them.
Example : A website has different views depending upon the user's authorization. A college
website has a different view for students, faculty and dean. Similarly, a companies website
would have a different view for the employee, accountant and manager.
External/Conceptual Mapping is done between the external schema and logical schema to
transform the request from an external schema to the conceptual schema. This mapping
relates the external schema with the logical schema.
Example: If we have to define an employee schema then it will have attributes like
Emlpoyee_id, Name, Age, Salary, Phone_no etc. So, the data types for these attributes would
be defined here. Also, if we have more than one table in our schema then how these tables
would be related is also defined here. Like if we have some more tables like department table
and project table then how these tables would be related is defined here.
Reduction/Converting ER Diagrams into Tables-
Following rules are used for converting an ER diagram into the tables-
A strong entity set with only simple attributes will require only one table in relational
model.
• Attributes of the table will be the attributes of the entity set.
• The primary key of the table will be the key attribute of the entity set.
Example-
• A strong entity set with any number of composite attributes will require only one
table in relational model.
• While conversion, simple attributes of the composite attributes are taken into
account and not the composite attribute itself.
Example-
NOTE-
If we consider the overall ER diagram, three tables will be required in relational model-
• One table for the entity set “Employee”
• One table for the entity set “Department”
• One table for the relationship set “Works in”
Sample Problem-
Solution-
NOTE- Here, combined table will be drawn for the entity set B and relationship set R.
Case-03: For Binary Relationship With Cardinality Ratio m:1
NOTE- Here, combined table will be drawn for the entity set A and relationship set R.
Here, two tables will be required. Either combine ‘R’ with ‘A’ or ‘B’
Way-01:
1. AR ( a1 , a2 , b1 )
2. B ( b1 , b2 )
Way-02:
1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )
Thumb Rules to Remember
While determining the minimum number of tables required for binary relationships
with given cardinality ratios, following thumb rules must be kept in mind-
• For binary relationship with cardinality ration m : n , separate and individual
tables will be drawn for each entity set and relationship.
• For binary relationship with cardinality ratio either m : 1 or 1 : n , always
remember “many side will consume the relationship” i.e. a combined table
will be drawn for many side entity set and relationship set.
• For binary relationship with cardinality ratio 1 : 1 , two tables will be required.
You can combine the relationship set with any one of the entity sets.
Rule-06: For Binary Relationship With Both Cardinality Constraints and Participation
Constraints-
Case-01: For Binary Relationship With Cardinality Constraint and Total Participation Constraint
From One Side-
Because cardinality ratio = 1 : n , so we will combine the entity set B and relationship set
R.
Then, two tables will be required-
1. A ( a1 , a2 )
2. BR ( a1 , b1 , b2 )
Because of total participation, foreign key a1 has acquired NOT NULL constraint, so it
can’t be null now.
Case-02: For Binary Relationship With Cardinality Constraint and Total Participation Constraint
From Both Sides-
If there is a key constraint from both the sides of an entity set with total participation,
then that binary relationship is represented using only single table.
Weak entity set always appears in association with identifying relationship with total
participation constraint.
Database having certain constraints and tables, need not to be a relational database
system always. For perfect database management system, there are certain rules for
the database, popularly known as Codd’s 13 (0 to 12) rules. These rules were
proposed by Dr Edgar Frank Codd (EF Codd) in 1985 to define a perfect relational
database system. For a relational database to be a perfect, it has to follow its rules.
But, no RDBMS can follow or obey all its rules. Oracle database only obeys 8.5
rules out of 13.
A database management system that satisfies Codd’s rules is called a fully
relational database management system.
The Codd’s 13 rules (0 to 12) for the relational databases are as follows:
Rule 0: Foundation Rule
This rule defines that for a system to qualify as an RDBMS, it must be able to
manage the database entirely through the relational capabilities.