0% found this document useful (0 votes)
35 views

CH-2 Relational Model

- A database schema defines the logical structure and relationships between tables in a database. It includes schema objects like tables, fields, relationships, primary keys and foreign keys. - A database schema can be either physical or logical. A physical schema specifies how data is stored physically, while a logical schema defines logical constraints on the data. - Primary keys uniquely identify each row in a table. Foreign keys in one table establish relationships by referencing the primary keys in other tables. - Keys help organize and relate data across multiple large tables in a database.

Uploaded by

hk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

CH-2 Relational Model

- A database schema defines the logical structure and relationships between tables in a database. It includes schema objects like tables, fields, relationships, primary keys and foreign keys. - A database schema can be either physical or logical. A physical schema specifies how data is stored physically, while a logical schema defines logical constraints on the data. - Primary keys uniquely identify each row in a table. Foreign keys in one table establish relationships by referencing the primary keys in other tables. - Keys help organize and relate data across multiple large tables in a database.

Uploaded by

hk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

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

⮚ A database schema can be divided broadly into two categories


1. Physical Database Schema

A physical database schema specifies how the data is stored physically


on a storage system or disk storage in the form of Files and Indices.
Designing a database at the physical level is called a physical schema.

2. Logical 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

CREATE TABLE students


(
S_Id int NOT NULL,
LastName varchar (255) NOT NULL,
FirstName varchar (255),
Address varchar (255),
City varchar (255),
PRIMARY KEY (S_Id)
)
FOREIGN 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.

⮚ Let us take an example to explain it:

⮚ Here are two tables first one is students table and second is orders table.

⮚ Here orders are given by students.


PRIMARY KEY

S_Id LastName FirstName CITY

1 MAURYA AJEET ALLAHABAD


First Table
2 JAISWAL RATAN GHAZIABAD

3 ARORA SAUMYA MODINAGAR

O_Id OrderNo S_Id

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.

⮚ 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.
Difference between Super Key and Candidate Key

S.NO Super Key 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.

While in a relation, number of candidate


In a relation, number of super keys are more than
4. keys are less than number of super
number of candidate keys.
keys.

Candidate key’s attributes can also


5. Super key’s attributes can contain NULL values.
contain NULL values.
ER model

⮚ ER model stands for an Entity-Relationship model. It is a high-level data model. This


model is used to define the data elements and relationship for a specified system.

⮚ It develops a conceptual design for the database. It also develops a very simple and easy
to design view of data.

⮚ In ER modeling, the database structure is portrayed as a diagram called an entity-


relationship diagram.

⮚ 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

• An entity may be any object, class, person or place.


• An entity is denoted as a rectangle in an ER diagram.
• For example, in a school database, students, teachers, classes, and
courses offered can be treated as entities.
• All these entities have some attributes or properties that give them
their identity.
• An entity set is a collection of related types of entities. An entity set
may include entities with attribute sharing similar values.
Weak Entity

• An entity that depends on another entity called a weak entity.

• The weak entity doesn't contain any key attribute of its own.

• The weak entity is represented by a double rectangle.


ATTRIBUTE

• Entities are denoted utilizing their properties, known as attributes.

• All attributes have values.

• For example, a student entity may have name, class, and age as attributes.

• There exists a domain or range of values that can be assigned to attributes.

• For example, a student's name cannot be a numeric value.

• It has to be alphabetic. A student's age cannot be negative, etc.


TYPES OF ATTRIBUTE

1. Key attribute
2. Composite attribute
3. Single-valued attribute
4. Multi-valued attribute
5. Derived attribute
KEY ATTRIBUTE

1. Key attribute:

• Key is an attribute or collection of attributes that uniquely identifies an


entity among the entity set. For example, the roll number of a student
makes him identifiable among students.
TYPES OF ATTRIBUTE

• There are mainly three types of keys:

1. Super key: A set of attributes that collectively identifies an entity in the entity
set.

2. Candidate key: A minimal super key is known as a candidate key. An entity


set may have more than one candidate key.

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:

Single-valued attribute contain a single value. For example,


Social_Security_Number (SSN).

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

• A relationship is used to describe the relation between entities.


Diamond is used to represent the relationship.
TYPES OF 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.

For example, Indian citizen has only 1 Adhar card.


TYPES OF 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

• Integrity constraints are a set of rules.


• It is used to maintain the quality of information.
• 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.

•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

• A referential integrity constraint is specified between two tables.

• In the Referential integrity constraints, if a foreign key in Table 1


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.
• Example:
REFERENTIAL INTEGRITY CONSTRAINTS
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:
REFERENTIAL INTEGRITY CONSTRAINTS
Relational Algebra?

• Every database management system must define a query language to allow


users to access the data stored in the database.

• Relational Algebra is a procedural query language used to query the


database tables to access data in different ways.

• In relational algebra, input is a relation(table from which data has to be


accessed) and output is also a relation(a temporary table holding the data
asked for by the user).
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 - ∏)

• Project operation is used to project only a certain set of attributes of a relation.

• 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.

• Syntax: ∏A1, A2...(r)

• 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 (∪)

• This operation is used to fetch data from two relations(tables) or temporary


relation(result of another 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

•where A and B are relations.

•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)

• Above operation will give us name of Students who are attending


both regular classes and extra classes, eliminating repetition.
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 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)

You might also like