2. Entity
An entity is a thing or object in the real world.
Each entity has a set of property is called attributes
Association between entity is known as relationship
3. ER Diagram in DBMS
An Entity–relationship model (ER model) describes the
structure of a database with the help of a diagram, which is
known as Entity Relationship Diagram (ER Diagram).
4. Symbols used in ER diagram
1. Rectangle: Represents Entity sets
2. Ellipses: Attributes
3. Diamonds: Relationship Set
4. Lines: They link attributes to Entity Sets and Entity sets to
Relationship Set
5. Double Ellipses: Multivalued Attributes
5. 6. Dashed Ellipses: Derived Attributes
7. Double Rectangles: Weak Entity Sets
8. Double Lines: Total participation of an entity in a relationship set
8. ER diagram has three main components:
1. Entity
2. Attribute
3. Relationship
1. Entity
An entity is an object or component of data. An entity is represented as
rectangle in an ER diagram.
ER diagram we have two entities Student and College and these two entities
have many to one relationship as many students study in a single college.
9. a.Weak Entity:
An entity that cannot be uniquely identified by its own attributes and
relies on the relationship with other entity is called weak entity.
The weak entity is represented by a double rectangle.
For example – a bank account cannot be uniquely identified without knowing the bank
to which the account belongs, so bank account is a weak entity.
10. b.Strong entity(-Independent)
The strong entity is the one whose existence does not depend on
the existence of any other entity in a schema
It is denoted by single rectangle
11.
12. 2. Attribute
An attribute describes the property of an entity.
An attribute is represented as Oval in an ER diagram
There are five types of attributes:
1 simple and composite
a)Simple attribute
Attribute that are not divided
Eg: ID number
13. b. Composite Attribute –
An attribute composed of many other attribute is called as composite attribute.
Composite attribute is represented by an oval comprising of ovals.
For example, Address attribute of student Entity type consists of Street, City, State, and
Country.
14. 2single and multivalued
a)Single valued
An attribute can assign single value
Eg:Name of an employee
b)Multivalued Attribute –
An attribute consisting more than one value for a given entity.
Multivalued attribute is represented by double oval.
For example, Phone_No (can be more than one for a given student).
15. 3.Stored and derived
a)Stored
It can assign value directly
Eg: student name
b)Derived
An attribute which can be derived from other attributes of the entity type is
known as derived attribute.
Derived attribute is represented by dashed oval.
e.g.; Age (can be derived from DOB).
16. 4 Key Attribute –
The attribute which uniquely identifies each entity in the entity set is
called key attribute.
Key attribute is represented by an oval with underlying lines.
For example, Roll_No will be unique for each student.
17. 5 Null valued
In some cases particular entities does not need a value in that case a special
value null is created
Eg: The apartment number is not needed if the house name is assigned
18. 3. Relationship
A relationship type represents the association between entity
types.
Relationship type is represented by a diamond and connecting the
entities with lines.
19. Types of relationship
a)One to one
When a single instance of an entity is associated with a single
instance of another entity then it is called one to one relationship.
For example, a person has only one passport and a passport is
given to one person.
20. 2. One to Many Relationship
When a single instance of an entity is associated with more than
one instances of another entity then it is called one to many
relationship.
For example – a customer can place many orders but a order
cannot be placed by many customers.
21. 3. Many to One Relationship
When more than one instances of an entity is associated with a single instance of
another entity then it is called many to one relationship.
For example – many students can study in a single college but a student cannot study in
many colleges at the same time.
22. 4. Many to Many Relationship
When more than one instances of an entity is associated with more than one instances of
another entity then it is called many to many relationship.
For example, a student can be assigned to many projects and a project can be assigned to
many students.
23. Participation Constraint:
Participation Constraint is applied on the entity participating in the relationship
set.
Total Participation –
Each entity in the entity set must participate in the relationship. If each
student must enroll in a course, the participation of student will be total.
Total participation is shown by double line in ER diagram.
Partial Participation –
The entity in the entity set may or may NOT participate in the relationship. If
some courses are not enrolled by any of the student, the participation of course
will be partial.
24. Keys
Keys play an important role in the relational database.
It is used to uniquely identify any record or row of data from the table. It is
also used to establish and identify relationships between tables.
It can be a single attribute or a combination of attribute
For example: In Student table, ID is used as a key because it is unique for each
student. In PERSON table, passport_number, license_number, SSN are keys
since they are unique for each person.
26. 1.Primary key
The PRIMARY KEY constraint uniquely identifies each record
in a table.
Primary keys must contain UNIQUE values, and cannot
contain NULL values.
A table can have only ONE primary key; and in the table, this
primary key can consist of single or multiple columns (fields).
27. In the above-given example, employee ID is a primary key
because it uniquely identifies an employee record
28. Rules for defining Primary key:
Two rows can't have the same primary key value
The primary key field cannot be null.
The value in a primary key column can never be
modified or updated if any foreign key refers to that
primary key.
29. 2 FOREIGN KEY
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table
that refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and
the table containing the candidate key is called the referenced or
parent table.
30. Look at the following two tables:
"Persons" table:
32. The "PersonID" column in the "Persons" table is the
PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN
KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that
would destroy links between tables.
The FOREIGN KEY constraint also prevents invalid data from
being inserted into the foreign key column, because it has to be
one of the values contained in the table it points to.
33. 3 Candidate key
A candidate key is an attribute or set of an attribute which can
uniquely identify a tuple.
The candidate keys are as strong as the primary key
34. In the EMPLOYEE table, id is best suited for the primary key. Rest
of the attributes like SSN, Passport_Number, and License_Number,
etc. are considered as a candidate key.
35. Properties of Candidate key:
It must contain unique values
Candidate key may have multiple attributes
Must not contain null values
It should contain minimum fields to ensure uniqueness
Uniquely identify each record in a table
36. 4 ALTERNATE KEYS
It is a column or group of columns in a table that uniquely
identify every row in that table.
A table can have multiple choices for a primary key but only one
can be set as the primary key.
All the keys which are not primary key are called an Alternate
Key.(The candidate key other than the primary key)
37. Example:
In this table, StudID, Roll No, Email are qualified to become a primary key. But
since StudID is the primary key, Roll No, Email becomes the alternative key.
38. Superkey
A superkey is a group of single or multiple keys which identifies
rows in a table. A Super key may have additional attributes that are
not needed for unique identification.
In the above-given example, EmpSSN and EmpNum name are
superkeys.
It can also be a set of attributes
Eg(empnum,empname)
39. Constraints
Constraints are the rules enforced on the data columns of a table.
These are used to limit the type of data that can go into a table.
Types of constraints
1. 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.
41. 2. Entity integrity constraints
The entity integrity constraint states that primary key value can't be
null.
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.
A table can contain a null value other than the primary key field.
Example:
42. 3. Referential Integrity Constraints
A referential integrity constraint is specified between two tables.
In the Referential integrity constraints, if a foreign key in Table1
refers to the Primary Key of Table 2, then every value of the
Foreign Key in Table 1 must be null or be available in Table 2.
44. 4. Key constraints
Keys are the entity set that is used to identify an entity within its entity
set uniquely.
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.
Example:
45. 5. 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:
CREATE TABLE STUDENT( ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT
NULL, STU_ADDRESS VARCHAR (235), PRIMARY KEY
(ROLL_NO) );
46. Relational Algebra
Relational algebra is a procedural query language.
It gives a step by step process to obtain the result of the query. It uses
operators to perform queries.
Types of Relational operation:
47. 1. Select Operation:
The select operation selects tuples that satisfy a given condition.
It is denoted by sigma (σ).
Notation: σ p(r)
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors
like: AND OR and NOT. These relational can use as relational
operators like =, ≠, ≥, <, >, ≤.
50. 2. Project Operation:
This operation shows the list of those attributes that we wish to appear
in the result.
Rest of the attributes are eliminated from the table.
It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION
52. 3. Union Operation:
Suppose there are two tuples R and S.
The union operation contains all the tuples that are either in R or S
or both in R & S.
It eliminates the duplicate tuples.
It is denoted by ∪.
Notation: R ∪ S
A union operation must hold the following condition:
1. R and S must have the attribute of the same number.
2. Duplicate tuples are eliminated automatically.
55. 4. Set Intersection:
Suppose there are two tuples R and S.
The set intersection operation contains all tuples that are in both R & S.
It is denoted by intersection ∩.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPO
SITOR)
Output:
56. 5. Set Difference:
Suppose there are two tuples R and S.
Set difference operation contains all tuples that are in R but not in S.
It is denoted by intersection minus (-).
Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) -
∏ CUSTOMER_NAME (DEPOSITOR)
Output:
57. 6. Cartesian product
The Cartesian product is used to combine each row in one table with
each row in the other table.
It is also known as a cross product.
It is denoted by X.
Notation: E X D
Example:
EMPLOYEE
60. 7. Rename Operation:
The rename operation is used to rename the output relation.
It is denoted by rho (ρ).
Notation:
ρ(Relation new, Relation old)
Example: We can use the rename operator to rename STUDENT
relation to STUDENT1.
ρ(STUDENT1, STUDENT)
61. Relational Calculus
Relational calculus is a non-procedural query language that tells the system
what data to be retrieved but doesn’t tell how to retrieve it.
Types of Relational Calculus
62. 1. Tuple Relational Calculus (TRC)
Tuple relational calculus is used for selecting those tuples that satisfy the given
condition.
Notation: { T| Condition }
In this form of relational calculus , define a tuple variable , specify the table
name in which the tuple is to be searched for along with a condition.
It can also specify a column name using a dot operator with the tuple to only
get a certain attribute in result.
63. Table: Student
Query to display the last name of those students where age is greater than 30
{ t.Last_Name | Student(t) AND t.age > 30 }
In the above query you can see two parts separated by | symbol.
The second part is where we define the condition and in the first part we specify the
fields which we want to display for the selected tuples.
64. The result of the above query would be:
Last_Name
---------
Singh
Query to display all the details of students where Last name is ‘Singh’
{ t | Student(t) AND t.Last_Name = 'Singh' }
65. 2. Domain Relational Calculus (DRC)
In domain relational calculus the records are filtered based on the domains.
Syntax:
{c1,c2,……….cn | F(c1,c2 ,…cn)}
Were c1,c2,….cn represents domain of attributes and F defines formula including
the condition for fetching data
66. Table: Student
Query to find the first name and age of students where student age is greater than 27
{< First_Name, Age > | ∈ Student ∧ Age > 27}
Note:
The symbols used for logical operators are: ∧ for AND, ∨ for OR and ┓ for NOT.
67.
68. Relational Database Design using ER to Relational Mapping
Mapping Entity
An entity is a real-world object with some attributes.
69. Mapping Process (Algorithm)
1. Create table for each entity.
2. Entity's attributes should become fields of tables with their respective data
types.
3. Declare primary key.
71. Mapping Process
1. Create table for a relationship.
2. Add the primary keys of all participating Entities as fields of table with their
respective data types.
3. If relationship has any attribute, add each attribute as field of table.
4. Declare a primary key composing all the primary keys of participating entities.
5. Declare all foreign key constraints.
72. Mapping Weak Entity Sets
A weak entity set is one which does not have any primary key associated with it.
73. Mapping Process
1. Create table for weak entity set.
2. Add all its attributes to table as field.
3. Add the primary key of identifying entity set.
4. Declare all foreign key constraints.