Database Integrity: Entity, Referential, Attribute, User, Key
Summary
Learn the concepts and examples of the 5 types of integrity (Entity, Referential, Attribute, User, Key) that ensure database accuracy and consistency, and finish with practice problems for the Information Processing Engineer exam.
💡 Short-answer practice problems for the Information Processing Engineer exam are at the end of this post. It is recommended to solve the problems multiple times.
❗️ It's important to identify the type of integrity based on its description.
📊 Summary of Database Integrity Types
Database integrity means maintaining the accuracy, consistency, and validity of data. Various constraints are used to enhance the reliability of the database.
Type | Core Keyword | Description |
---|---|---|
Entity Integrity | PRIMARY KEY | The primary key cannot contain NULL values and must be unique within the relation. |
Referential Integrity | FOREIGN KEY | A foreign key value must match a primary key value in the referenced relation or be NULL . |
Attribute (Domain) Integrity | DOMAIN , CHECK , DEFAULT | Attribute values must belong to a defined domain (data type, range, etc.). |
User-Defined Integrity | TRIGGER | Integrity conditions defined by the user to fit specific business rules. |
Key Integrity | UNIQUE | No two tuples in a relation can have the same key value. |
1. Entity Integrity
Entity integrity is a constraint that ensures every tuple (row) in a relation can be uniquely identified. It uses a Primary Key, which must satisfy two conditions:
- It cannot have a
NULL
value. (Not Null) - It must have a unique value within the relation. (Unique)
- Example: If
student_id
is set as the primary key in aStudent
table, every student must have a unique ID, and no student data can exist without one.
CREATE TABLE Student (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
major VARCHAR(50)
);
2. Referential Integrity
Referential integrity is a constraint that ensures the relationship between two relations remains valid. It is enforced using a Foreign Key, which must either be one of the primary key values from the referenced relation or be NULL
.
- Example: The
student_id
in theEnrollment
table is a foreign key that references thestudent_id
in theStudent
table. Therefore, anystudent_id
entered into theEnrollment
table must already exist in theStudent
table. You cannot enter enrollment information for a non-existent student.
CREATE TABLE Enrollment (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_code VARCHAR(10),
FOREIGN KEY (student_id) REFERENCES Student(student_id)
);
3. Attribute (Domain) Integrity
Attribute (Domain) integrity is a constraint that ensures each attribute (column) value in a relation belongs to a valid range of values (a domain). It can be set using data types, NULL
constraints, default values, and CHECK
constraints.
- Example: In a
Student
table, you can enforce that thegpa
attribute only accepts numbers between 0.0 and 4.5, and thatgender
can only be 'Male' or 'Female'.
CREATE TABLE Student (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) CHECK(gender IN ('Male', 'Female')),
gpa DECIMAL(2, 1) CHECK(gpa BETWEEN 0.0 AND 4.5),
registration_date DATE DEFAULT CURRENT_DATE
);
4. User-Defined Integrity
User-defined integrity is a set of constraints defined by the user to enforce specific business rules or organizational policies in the database. It is often implemented using Triggers.
- Example: You can create a rule where if a product's stock in the
Products
table drops below 10, its information is automatically added to theReorder
table.
CREATE TRIGGER low_stock_alert
AFTER UPDATE ON Products
FOR EACH ROW
BEGIN
IF NEW.stock < 10 THEN
INSERT INTO Reorder (product_code, request_date) VALUES (NEW.product_code, NOW());
END IF;
END;
5. Key Integrity
Key integrity is a constraint that ensures every tuple in a relation has a unique key value. While the Primary Key constraint covers both entity and key integrity, the UNIQUE
constraint can be used to enforce uniqueness on non-primary key attributes as well.
- Example: In a
Members
table, theemail
is not the primary key, but you can set aUNIQUE
constraint to ensure that every member has a different email address.
CREATE TABLE Members (
member_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL
);
📝 Exam Preparation Problems
Problem | What is the constraint that ensures a primary key cannot have a NULL value and must be unique within the relation? |
Your Answer | |
Correct Answer | Reveal Answer |
Problem | What is the constraint that requires a foreign key value to either match a primary key value in another table or be NULL? |
Your Answer | |
Correct Answer | Reveal Answer |
Problem | What is the constraint that ensures attribute values belong to a predefined data type or range? |
Your Answer | |
Correct Answer | Reveal Answer |
[Student] Table
student_id | name | major |
---|---|---|
1001 | John Doe | Computer Science |
1002 | Jane Smith | Software Engineering |
NULL | Peter Pan | Information Tech. |
Problem | Which integrity constraint does the 'Student' table above violate? (Assuming student_id is the primary key.) |
Your Answer | |
Correct Answer | Reveal Answer |
Category | ( 1 ) Integrity Constraint | ( 2 ) Integrity Constraint | ( 3 ) Integrity Constraint |
---|---|---|---|
Constraint Target | Attribute | Tuple | Attribute and Tuple |
Related Key | - | Primary Key | Foreign Key |
# of Constraints in Relation | Same as # of attributes | 1 | 0 to many |
Problem | What are the integrity constraints for 1, 2, and 3 in the table above? |
Your Answer | |
Correct Answer | Reveal Answer |