Keys
Keys
Integrity constraints in a database management system (DBMS) are rules and restrictions applied to the data to
ensure its accuracy, consistency, and reliability . They help maintain the integrity of the data by preventing invalid
data modifications and ensuring that data relationships and dependencies are preserved. Integrity constraints are
crucial for upholding data quality and preventing data corruption. They act as safeguards to protect against accidental
or intentional data inconsistencies.
Here is a detailed explanation of each type of integrity constraint, as described in the sources:
Key Constraints:
These constraints enforce the properties of keys, which are attributes or sets of attributes that uniquely identify
entities within an entity set. Key constraints are also referred to as Entity Constraints. There are various types of
keys, including:
Candidate Key: A set of attributes that can uniquely identify a tuple. A relation can have multiple candidate keys.
For example, in a student relation, both the "student ID" and "email address" could serve as candidate keys,
assuming they are unique for each student.
Primary Key: The chosen candidate key used to enforce entity integrity and uniquely identify each row. It is a
specific candidate key designated as the primary means of identifying a tuple. Typically, one candidate key is
chosen as the primary key, and other candidate keys become alternate keys. For instance, in the student
relation, "student ID" might be chosen as the primary key.
Foreign Key: An attribute in one relation that refers to the primary key of another relation, establishing a link
between them. It enforces referential integrity by ensuring that values in the foreign key column match existing
primary key values in the referenced table. For example, a "department ID" attribute in the student relation might
reference the "department ID" primary key in a department relation.
Uniqueness: No two tuples in a relation can have the same value for the primary key attribute(s).
Non-Nullability: The primary key attribute(s) cannot contain NULL values.
Domain Constraints:
Domain constraints restrict the range of permissible values for an attribute. These constraints specify the data type
and any additional restrictions on the values an attribute can hold. For instance:
Domain constraints help ensure that data entered into the database adheres to predefined rules and corresponds to
real-world expectations. They can be implemented using data types, check constraints, or custom validation routines.
Referential Integrity Constraints:
Referential integrity constraints maintain consistency between related tables using foreign keys. They enforce the
relationship between a foreign key in one table and the primary key of another table (or the same table). These
constraints guarantee that:
Values in the foreign key column either match existing primary key values in the referenced table or are NULL.
Changes to the referenced table (e.g., deleting or updating a row with a primary key referenced by foreign keys in
other tables) do not violate referential integrity. The database system can enforce referential integrity by rejecting
operations that would violate the constraint or by cascading changes to related tables.
In addition to the three main types, other constraints can be categorised as:
Tuple Uniqueness Constraint: Ensures that every tuple (row) in a relation is unique. This constraint prevents
duplicate rows within a table.
Single-Value Constraint: Requires that each attribute of an entity set holds a single value, allowing for NULL
values to represent missing or inapplicable information.
General Constraints: Encompass constraints that do not fit into the other categories. These might include
business rules specific to an organisation or application, such as:
The salary of an employee cannot exceed the salary of their manager.
The quantity of an item in an order cannot be negative.
Data Accuracy: Ensure that data entered into the database is accurate and adheres to predefined rules.
Data Consistency: Prevent inconsistencies between related tables and maintain data integrity throughout the
database.
Data Reliability: Ensure that the database reflects a consistent and accurate representation of the real world.
Data Integrity: Preserve data integrity and prevent data corruption due to invalid modifications.
By enforcing these constraints, database systems help organisations maintain the quality and reliability of their data,
which is crucial for decision-making, reporting, and overall data management.
Keys
Keys in databases are attributes (Columns) or sets of attributes(Columns) that are used to identify and relate
data within a relational database . They play a crucial role in ensuring data integrity, maintaining relationships
between tables, and facilitating efficient data retrieval. The sources explain several types of keys:
Types of Keys:
Candidate Key: A candidate key is a minimal set of attributes that can uniquely identify a tuple (row) in a
relation (table) . A relation can have multiple candidate keys. For instance, in a student relation, both the
"student ID" and "email address" could serve as candidate keys if they are unique for each student.
Primary Key: The primary key is a specific candidate key chosen to uniquely identify each tuple in a
relation . It acts as the main identifier for a record and enforces entity integrity by ensuring that each row has a
unique and non-null value for the primary key attribute(s). Only one candidate key can be designated as the
primary key. In the student relation example, the "student ID" might be selected as the primary key, making it the
primary means of identifying a student record.
NOTE : Candidate key can be null value but the primary key cannot be a null value \
Super Key: A super key is a set of one or more attributes(Columns) that, taken collectively, can uniquely
identify a tuple (Row) in a relation (Table) . It may contain redundant attributes that are not necessary for
unique identification. Any set of attributes that includes a candidate key is also considered a super key. For
example, the combination of "student ID" and "student name" would be a super key, but it is not a candidate key
because "student name" is not required for unique identification if "student ID" is already present.
Alternate Key: An alternate key is a candidate key that is not chosen as the primary key . i.e. Candidate
keys other than primary key. It provides an alternative way to uniquely identify a tuple. In the student relation, if
"student ID" is chosen as the primary key, "email address" (assuming it is unique) could be an alternate key.
Foreign Key: A foreign key is an attribute (Column) or set of attributes (Columns) in one relation that refers
to the primary key of another relation (or the same relation). It establishes a link between the two relations
and enforces referential integrity, ensuring that relationships between tables are maintained . For instance, a
"department ID" attribute in the student relation could be a foreign key referencing the "department ID" primary
key in a department relation. The foreign key constraint guarantees that any value in the "department ID" column
of the student relation must correspond to an existing "department ID" in the department relation.
Composite Key: A composite key, also called a compound key, is a key that consists of two or more
attributes to uniquely identify a tuple in a relation . This is used when no single attribute can guarantee
uniqueness. For example, in a 'Books' table, a composite key might be formed using the 'Title' and 'Author'
attributes because there can be multiple books with the same title by different authors.
Surrogate Key: A surrogate key is an artificial key introduced when no natural primary key exists or when
using existing attributes as primary keys would be impractical . Surrogate keys are often generated as
sequential numbers or unique identifiers. For instance, if a table lacks a natural key, a surrogate key like "Record
ID" can be added as an auto-incrementing integer to serve as the primary key.
Entity Integrity: Primary keys ensure that each entity in a relation is uniquely identifiable. This prevents
duplicate records and ensures that each row can be distinguished from others.
Referential Integrity: Foreign keys establish and maintain relationships between tables. They guarantee
that relationships between tables are valid and consistent. For example, a foreign key constraint prevents a
student record from being associated with a non-existent department.
Efficient Data Retrieval: Keys, particularly primary keys, are often used to create indexes, which speed up data
retrieval operations. Indexes allow the database management system to quickly locate specific records without
scanning the entire table.
Data Consistency and Accuracy: By enforcing uniqueness and relationship constraints, keys help maintain the
overall consistency and accuracy of the data in the database. They prevent data anomalies and
inconsistencies that can arise from data redundancy or improper relationships between tables.
Understanding the different types of keys and their use cases is essential for designing and managing relational
databases effectively. Keys are fundamental to ensuring data integrity, enforcing relationships, and facilitating efficient
data access.