Unit 2 Domain Relation and Keys
Unit 2 Domain Relation and Keys
Program : B.Tech
Course Name: Database Management System
Course code: (E2UC302B)
Relational Model in DBMS
Relational model can represent as a table with columns and rows. Each row is known
as a tuple. Each table of the column has a name or attribute.
Attribute: It contains the name of a column in a particular table. Each attribute Ai must
have a domain, dom(Ai)
Relational schema: A relational schema contains the name of the relation and name of
all columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can
identify the row in the relation uniquely.
Relational Model
Let's look at a scenario to understand the relational model:
Consider a case where you wish to store the name, the CGPA attained, and the roll number of all
the students of a particular class. This structured data can be easily stored in a table as described
below:
As we can notice from the above relation:
Any given row of the relation indicates a student i.e., the row of the table
describes a real-world entity.
The columns of the table indicate the attributes related to the entity. In this case,
the roll number, CGPA, and the name of the student.
Domains
Definition: In database design, a domain defines the permissible values that a column in a table
can hold. It's essentially a set of valid values for a given attribute.
Example: Consider a database for a university. Suppose you have a table Students with a column
Grade. The domain for the Grade column might be restricted to a set of valid grades, such as A,
B, C, D, and F. This ensures that only these grades can be entered into the Grade column.
Relation
Definition: A relation in a database context is essentially a table. It’s a collection of tuples
(rows), where each tuple represents a record. Each tuple has attributes (columns) that are
defined by a schema.
Example: Consider two tables in a database, Courses and Enrollments.
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.
Definition: In a relational database, keys are used to uniquely identify records and
establish relationships between tables. There are several types of keys, including:
Primary Key: A column or a set of columns that uniquely identifies each row in a table.
Foreign Key: A column or set of columns in one table that refers to the primary key of
another table.
Candidate Key: A column or set of columns that could serve as a primary key.
Primary Keys
Example: Primary Key: In the Students table, StudentID is the primary key because it
uniquely identifies each student.
Candidate Key
A candidate key is a set of one or more columns in a database table that can
uniquely identify each row in that table.
In other words, a candidate key is a potential primary key for the table.
Each table can have one or more candidate keys, but only one of them is
chosen to be the primary key.
Key Points about Candidate Keys
Uniqueness: The values in a candidate key must be unique for every row in
the table.
Minimality: A candidate key should be minimal, meaning that if any column
is removed from the key, it should no longer be able to uniquely identify
rows.
Candidate Key: Example
• Identifying Candidate Keys
• UserID: Each UserID is unique and can identify a row in the Users table. So, UserID is a
candidate key.
• Email: Each Email is unique for every user in this table and can also uniquely identify a row.
Therefore, Email is another candidate key.
• PhoneNumber: Each Phone Number is unique in this table and can also uniquely identify a
row. So, Phone Number is also a candidate key.
• Choosing the Primary Key
• Out of these candidate keys, one is chosen to be the primary key. For example, let’s choose
UserID as the primary key. The primary key is usually chosen based on various factors such as
simplicity, efficiency, and stability.
• UserID is the primary key.
• Email and PhoneNumber are candidate keys but are not the primary key.
Foreign Keys
Foreign Key: In the Enrollments table, StudentID is a foreign key that references the StudentID in the
Students table. Similarly, CourseID in Enrollments references CourseID in the Courses table.
In this example:
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.
Entity integrity constrains
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.
Referential Integrity Constraints
• The referential integrity constraint is specified between two relations and is used
to maintain the consistency among tuples in the two relations.
• Informally, the referential integrity constraint states that a tuple in one relation
that refers to another relation must refer to an existing tuple in that relation.
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.
Referential Integrity Constraints
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.
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 of Integrity Constraint
Relational Algebra
• Relational Algebra is procedural query language, which takes Relation
as input and generate relation as output.
• Relational algebra mainly provides theoretical foundation for
relational databases and SQL.
Select operation
It displays the records that satisfy a condition. It is denoted by sigma (σ) and is a horizontal subset of the original
relation.
Thank You