Unit 2 Part 2
Unit 2 Part 2
INTRODUCTION TO ER AND
RELATIONAL MODEL
The Entity-Relationship model defines the conceptual view of a database. ER model works on
the real-world entities. The ER model is best for designing databases and is very simple and easy
to design logical view of data. In ER Model, we disintegrate data into entities, attributes and
setup relationships between entities and this can be represented visually using the ER diagram.
ER Diagram is a visual representation of data that describes how data is related to each other.
Importance of ER Model
• ER Model is plain and simple for designing the structure.
• It saves time.
• Without ER diagrams you cannot make a database structure & write production code.
• It displays the clear picture of the database structure.
Relationship model represents data in the form of relations or tables. Relationship model is the
primary data model, which is used widely used around the world for data storage and
processing. This model is very simple to implement and very easy to process data with storage
efficiency. It consists of:
• Tables: In relationship model, relations are represent in the form of Tables. A table has
rows and columns, where rows represent records and columns represent the attributes.
Employee table is representing the relationship model.
• Tuple: A single row of a table, which contains a single record for that relation is called a
tuple. e.g.; Employee relation given above has 3 tuples.
• Relation Schema : A relation schema describes the relation name (table name),
attributes, and their names. Relational Schema of Employee relation can be represented
as:Emp_id,Emp_name,city,Contact_no. Dept.
• Relation Key: Each row has one or more attributes, known as relation key, which can
identify the row in the relation (table) uniquely.
• Attribute Domain: The possible values an attribute can take in a relation is called its
domain. For Example, domain of contact_no is 10 digits
• NULL Values: Values of some attribute for some tuples may be unknown, missing or
undefined which are represented by NULL.
Employee Table
Emp_id Emp_name City Contact _No. Dept.
101 A Delhi 1234567891 IT
102 B Bombay Null Marketing
103 C Goa 9985641235 Developer
Entity
An entity is any singular, identifiable and separate object. Entity either be living or non living
objects but can be easily identifiable. For example, in employee database, employee, managers
and departments can be considered as entities. All the entities have some attributes or properties
that give them their identity. An entity set is a collection of similar types of entities. An entity set
may contain entities with attribute sharing similar values.
Types of Entity
Strong Entity: The strong entity has a primary key. Weak entities are dependent on strong entity.
Its existence is not dependent on any other entity. Strong Entity is represented by a single
rectangle.
Weak Entity: The weak entities in DBMS do not have a primary key and are dependent on the
parent entity. It mainly depends on other entities. Weak Entity is represented by double
rectangle.
S_name
S_roll no Loan_name Loan_date
S_course
Attributes
Attributes are the properties of the entities. All attributes have values. For example, an employee
entity may have Empid, emp_name, department and date of hiring as attributes. There exists a
domain or range of values that can be assigned to attributes. For example, a employee name
cannot be a numeric value. It has to be alphabetic.
Types of Attributes
Simple Attribute: Simple attributes are atomic values, which cannot be divided further. For
example, marital status of an employee.
Composite Attribute : Composite attributes are combination of more than one simple attribute.
For example, a employee's complete name may have first_name and last_name.
Derived attribute: Derived attributes are the attributes that do not exist in the physical database,
but their values are derived from other attributes present in the database. For example, age of
employee should not be saved directly in the database, instead it can be derived from
data_of_birth.
Key Attribute : Single-value attributes contain single value. For example – empid.
Null Attribute: It does not have a value and can be left blank, it’s optional can be filled or cannot
be. For example: employee middle name.
Stored Attribute: Attribute that cannot be derived from other attributes are called as stored
attributes. For example: DOB of an employee
Simple Attribute
Derived Attribute
Marital Status
Key Attribute
Age
DOB
Emp_id
Employee
Stored Attribute
NULL Attribute
Keys
Key is an attribute or collection of attributes that uniquely identifies an entity among entity set.
For example, the empid of a employee makes him/her identifiable among employees.
Types of Keys
Primary Key: It is the first key which identifies a single instance of an entity uniquely. An entity
can contain multiple keys. In the emp table, emp_id can be primary key since it is unique for
each employee and we can even select License_Number and Passport_Number as primary key
since they are also unique. For each entity, selection of the primary key is based on requirement
and developers.
Candidate Key: A minimal super key is called a candidate key. An entity set may have more than
one candidate key. The remaining attributes except for primary key are considered as a candidate
key. The candidate keys are as strong as the primary key. In the emp table, emp_id is best suited
for the primary key and remaining attributes like Passport_Number, and License_Number, etc.
are considered as a candidate key.
Super Key: Super key is a set of an attribute which can uniquely identify a tuple. Super key is a
superset of a candidate key. In the emp table, for (emp_id, emp_name) the name of two
employees can be the same, but their emp_id can't be the same. Hence, this combination can also
be a key. The super key would be emp_id, (emp_id, emp_name), etc.
Foreign Key: Foreign keys are the column of the table which is used as primary key for one table
but acts as a candidate key of another table. In a organization, each employee works in a specific
department, and employee and department are two different entities. So we can't store the
information of the department in the employee table. Therefore we link these two tables through
the primary key of one table. We add the primary key of the dept table, dept_id as a new attribute
in the emp table. Now in the emp table, dept_id is the foreign key, and both the tables are related.
Degree of Relationship
The number of participating entities in a relationship defines the degree of the relationship.
Binary = degree 2
Ternary = degree 3
n-ary = degree n
Cardinality
Cardinality defines the number of entities in one entity set, which can be associated with the
number of entities of other set via relationship set.
Cardinality
One-to-One: In One - to - One Relationship, one entity is related with only one other entity. One
row in a table is linked with only one row in another table and vice versa. For example: A
Country can have only one Capital City.
Country Capital
1 1
Country Have Capital
One-to-Many: In One - to - Many Relationship, one entity is related to many other entities. One
row in a table A is linked to many rows in a table B, but one row in a table B is linked to only
one row in table A. For example: One Department has many Employees.
Department Employee
1 N
Department Have Employee
Many-to-One: In Many - to - One Relationship, many entities can be related with only one other
entity. For example: No. of Employee works for Department. Multiple rows in Employee table is
related with only one row in Department table.
Employee Department
N 1
Employee N Have Department
Many-to-Many: In Many - to - Many Relationship, many entities are related with the multiple
other entities. This relationship is a type of cardinality which refers the relation between two
entities. For example: Various Books in a Library are issued by many Students.
Book Student
N N
Book N Read By Students
Integrity constraints are a set of rules. It is used to maintain the quality of information. Integrity
constraints ensure that the data integrity is not affected while insertion, updating, and other
processes have to be performed on the database. These are used to limit the type of data that can
go into a table. An integrity constraint ensures the accuracy, reliability and consistency of the
data in the database. Thus, integrity constraint is used to guard against accidental damage to the
database.
Data integrity is handled in a relational database through the concept of referential integrity.
Constraints can be categorized in two ways:
• The constraints can be specified immediately after the column definition in command.
This is called column-level definition. The column level constraints are applied only to
one column.
• The constraints can be specified after all the columns are defined. This is called table-
level definition. The table level constraints are applied to the whole table.
Integrity
Constraints
Referential
Domain Entity integrity
Integrity Key Constraints
Constraints Constraints
Constraints
Domain Constraints
Domain constraints can be defined as the definition of a valid set of values for an attribute. 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.
Example:
Mapping Entity
An entity is a real-world object with some attributes.
Emp_Id
Employee Dept
SQL commands are instructions. It is used to communicate with the database. It is also used to
perform specific tasks, functions, and queries of data. SQL can perform various tasks like create
a table, add data to tables, drop the table, modify the table, set permission for users
Types of SQL
Command
DROP DELETE
UPDATE
• Drop: It is used to delete both the structure and record stored in the table.
Syntax:
2. DROP TABLE_NAME ;
• Alter: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
DML(Data Manipulation Language) : DML commands are used to modify the database. It is
responsible for all form of changes in the database. Examples of DML:
• Select: This command is used to fetch the data from the database. It is used to select the
attribute based on the condition described by WHERE clause.
Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
• Insert: The INSERT statement is a SQL query. It is used to insert data into the row of a
table.
Syntax:
• Update: This command is used to update or modify the value of a column in the table.
Syntax:
TCL(Transaction Control Language): TCL commands deals with the transaction within the
database. TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only. These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them. Here are some commands that come
under TCL:
Commit: Commit command is used to save all the transactions to the database.
Syntax:
1. COMMIT;
Rollback: Rollback command is used to undo transactions that have not already been saved to
the database.
Syntax:
1. ROLLBACK;
Create Table
Insert Row in a Table
Retrive Data From Table Using Select Command
Remove Data from Table Using Delete Command
Aggregate Functions are all about performing calculations on multiple rows of a single column
of a table and returning a single value. In database management an aggregate function is a
function where the values of multiple rows are grouped together as input on certain criteria to
form a single value of more significant meaning. Aggregate function are used because Top levels
managers are usually interested in knowing whole figures and not necessary the individual
details.
COUNT Function
The COUNT function returns the total number of values in the specified field. It works on both
numeric and non-numeric data types.
Count(*): Returns total number of records .i.e 6.
Count(salary): Return number of Non Null values over the column salary. i.e 5.
Count(Distinct Salary): Return number of distinct Non Null values over the column salary .i.e 4
SUM Function
Suppose we want a report that gives total amount of payments made so far. We can use the
MySQL SUM function which returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result returned.
sum(salary): Sum all Non Null values of Column salary i.e., 27000
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 22000
AVG Function
MySQL AVG function returns the average of the values in a specified column. Just like the
SUM function, it works only on numeric data types.
Avg(salary) = Sum(salary) / count(salary) = 27000/5=5400
Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 22000/4=5500
MIN Function
The MIN function returns the smallest value in the specified table field.
Min(salary): Minimum value in the salary column except NULL i.e., 4000.
MAX Function
Just as the name suggests, the MAX function is the opposite of the MIN function. It returns the
largest value from the specified table field.
Max(salary): Maximum value in the salary i.e., 7000
SUMMARY