CH-2 Relational Model
CH-2 Relational Model
Database Schema :
• A database schema is a structure that represents the
logical storage of the data in a database. It represents
the organization of data and provides information about
the relationships between the tables in a given database.
• A database schema contains schema objects that may
include tables, fields, packages, views, relationships,
primary key, foreign key,
Database Schema
Database Schema
This schema defines all the logical constraints that need to be applied on
the data stored. It defines tables, views, and integrity constraints.
KEYS
⮚ In real world applications, number of tables required for storing the data is huge, and the
different tables are related to each other as well.
⮚ Also, tables store a lot of data in them. Tables generally extends to thousands of records
stored in them, unsorted and unorganised.
⮚ Now to fetch any particular record from such dataset, you will have to apply some
conditions, but what if there is duplicate data present and every time you try to fetch some
data by applying certain condition, you get the wrong data. How many trials before you get
the right data?
⮚ To avoid all this, Keys are defined to easily identify any row of data in a table
PRIMARY KEY
⮚ A column or columns is called primary key (PK) that uniquely identifies each row in the
table.
⮚ If you want to create a primary key, you should define a PRIMARY KEY constraint when
you create or modify a table.
⮚ When multiple columns are used as a primary key, it is known as composite primary key.
⮚ In designing the composite primary key, you should use as few columns as possible. It is
good for storage and performance both, the more columns you use for primary key the
more storage space you require.
⮚ In terms of performance, less data means the database can process faster.
PRIMARY KEY
⮚ In the relational databases, a foreign key is a field or a column that is used to establish a
link between two tables.
⮚ In simple words you can say that, a foreign key in one table used to point primary key in
another table.
⮚ Here are two tables first one is students table and second is orders table.
1 99586465 2
Second Table 2 78466588 2
3 22354846 3
4 57698656 1
FOREIGN KEY
⮚ The "S_Id" column in the "Students" table is the PRIMARY KEY in the "Students" table.
⮚ The "S_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
⮚ The foreign key constraint is generally prevents action that destroy links between tables.
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.
⮚ For example: In the above EMPLOYEE table, for(EMPLOEE_ID, / EMPLOYEE_NAME)
the name of two employees can be the same, but their EMPLYEE_ID can't be the same.
Hence, this combination can also be a key.
⮚ The super key would be EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-NAME), etc.
CANDIDATE KEY
⮚ A candidate key is an attribute or set of an attribute which can uniquely identify a tuple.
⮚ The remaining attributes except for primary key are considered as a candidate key. The
candidate keys are as strong as the primary key.
⮚ For example:
⮚ 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.
CANDIDATE KEY
CANDIDATE KEY
⮚ Super key is a set of an attribute which can uniquely identify a tuple. Super key is a
superset of a candidate key.
⮚ A candidate key is an attribute or set of an attribute which can uniquely identify a tuple.
⮚ The remaining attributes except for primary key are considered as a candidate key. The
candidate keys are as strong as the primary key.
⮚ For example:
⮚ 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.
Difference between Super Key and Candidate Key
Super Key is an attribute (or set of attributes) that is Candidate Key is a subset of a super
1.
used to uniquely identifies all attributes in a relation. key.
2. All super keys can’t be candidate keys. But all candidate keys are super keys.
Various super keys together makes the criteria to Various candidate keys together makes
3.
select the candidate keys. the criteria to select the primary keys.
⮚ It develops a conceptual design for the database. It also develops a very simple and easy
to design view of data.
⮚ For example, Suppose we design a school database. In this database, the student will be
an entity with attributes like address, name, id, age, etc. The address can be another
entity with attributes like city, street name, pin code, etc and there will be a relationship
between them.
Components of an ER Diagrams
ENTITY
• The weak entity doesn't contain any key attribute of its own.
• For example, a student entity may have name, class, and age as attributes.
1. Key attribute
2. Composite attribute
3. Single-valued attribute
4. Multi-valued attribute
5. Derived attribute
KEY ATTRIBUTE
1. Key attribute:
1. Super key: A set of attributes that collectively identifies an entity in the entity
set.
3. Primary key: A primary key is one of the candidate keys chosen by the
database designer to uniquely identify the entity set.
COMPOSITE ATTRIBUTE
2. Composite attribute:
An attribute that is a combination of other attributes is called a composite
attribute. For example, In student entity, the student address is a composite
attribute as an address is composed of other characteristics such as pin
code, state, country.
COMPOSITE ATTRIBUTE
3. Single-valued attribute:
4. Multi-valued Attribute:
If an attribute can have more than one value, it is known as a multi-valued
attribute. Multi-valued attributes are depicted by the double ellipse. For
example, a person can have more than one phone number, email-address,
etc.
DERIVED ATTRIBUTE
5. Derived attribute:
Derived attributes are the attribute that does not exist in the physical
database, but their values are derived from other attributes present in the
database. For example, age can be derived from date_of_birth. In the ER
diagram, Derived attributes are depicted by the dashed ellipse.
RELATIONSHIP
1. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is
known as one to one relationship.
2. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of an entity on
the right associates with the relationship then this is known as a one-to-many relationship
.
For example, A Customer can have many bank accounts.
TYPES OF RELATIONSHIP
3. Many-to-many relationship
When more than one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then it is known as a many-to-many
relationship.
For example, Employee can assign by many projects and project can have many
employees.
Integrity Constraints
•Thus, integrity constraint is used to guard against accidental damage to the database.
TYPES INTEGRITY 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:
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:
REFERENTIAL INTEGRITY 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:
REFERENTIAL INTEGRITY CONSTRAINTS
Relational Algebra?
• Relational Algebra works on the whole table at once, so we do not have to use loops etc
to iterate over all the rows(tuples) of data one by one. All we have to do is specify the
table name from which we need the data, and in a single line of command, relational
algebra will traverse the entire given table to fetch data for you.
• The primary operations that we can perform using relational algebra are:
1. SELECT (symbol: σ)
2. PROJECT (symbol: π)
3. RENAME (symbol: ρ)
4. UNION (υ)
5. SET DIFFERENCE (-)
6. CARTESIAN PRODUCT ( x )
Select Operation (sigma - σ)
• This is used to fetch rows(tuples) from table(relation) which satisfies a given condition.
• Syntax: σ p(r)
• Where, σ represents the Select Predicate, r is the name of relation(table name in which
you want to look for data), and p is the prepositional logic, where we specify the
conditions that must be satisfied by the data.
• In prepositional logic, one can use unary and binary operators like =, <, > etc, to specify
the conditions.
• Let's take an example of the Student table we specified above in the Introduction of
relational algebra, and fetch data for students with age more than 17.
• σage > 17 (Student)
Project Operation (pie - ∏)
• In simple words, If you want to see only the names all of the students in the Student
table, then you can use Project Operation.
• It will only project or fetch the columns or attributes asked for, and will also remove
duplicate data from the columns.
• where A1, A2 etc are attribute names(column names) and (r) is table name.
Project Operation (∏)
For example,
∏Name, Age(Student)
Above statement will show us only the Name and Age columns for all
the rows of data in Student table.
Union Operation (∪)
• For this operation to work, the relations(tables) specified should have same
number of attributes(columns) and same attribute domain.
• Also the duplicate tuples are automatically eliminated from the result.
• Syntax: A ∪ B
•For example, if we have two tables RegularClass and ExtraClass, both have a
column student to save name of student, then,
Union Operation (∪)
• ∏Student(RegularClass) ∪ ∏Student(ExtraClass)
• 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 DEPOSITOR table and BORROW table
•Input: ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
• Set Difference:
• Suppose there are two tuples R and S. The set intersection 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)
Set Difference:( Minus - )
● Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
● It is denoted by intersection minus (-).
● Notation: R - S
● Example:
● ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Cartesian Product(X)
• 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
•
•Input: EMPLOYEE X DEPARTMENT
• Rename Operation:
• The rename operation is used to rename the output relation. It is denoted by rho (ρ).
•
•Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
•
• ρ(STUDENT1, STUDENT)