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

Domain Constraints in Database Management Systems

Uploaded by

susildahal234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Domain Constraints in Database Management Systems

Uploaded by

susildahal234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit: 5

Domain Constraints in Database Management Systems (DBMS) Unit: 5


Domain constraints ensure that the values entered in a database What is a Relational Database Design 3NF (Third Normal Form)
column adhere to specific rules and conditions. They play a vital Relational Database Design is a critical aspect of database A table is in Third Normal Form (3NF) if it satisfies 2NF and
role in maintaining the integrity and reliability of the data within management systems, focusing on structuring data additionally. No Transitive Dependency: Non-key attributes
a database. Here, we will explore some of the most important efficiently and effectively to ensure integrity, reduce must not depend on other non-key attributes. In other
domain constraints: Not Null, Unique, Primary Key, and Check
redundancy, and facilitate optimal data retrieval. A well- words, a non-key attribute should not determine another
constraints.
designed relational database adheres to several key non-key attribute.
1. Not Null Constraint(under Domain Constraints)
The Not Null constraint ensures that a column cannot have a
principles and concepts, including the identification and 3NF aims to eliminate transitive dependencies, where one
NULL value. This constraint is useful when a specific attribute utilization of various types of keys, understanding functional non-key attribute indirectly depends on the primary key
must always contain a valid value. For instance, in a student dependencies, recognizing and addressing anomalies, and through another non-key attribute.
database, the 'student_name' field should not be left blank, as applying normalization techniques through different normal Example of 3NF Violation and Correction
every student must have a name. forms. Student_ID Course_ID Instructor_Dept
 Not Null Constraint: Ensures that a column cannot Features of Good Relational Designs 101 MATH101 Mathematics
have NULL values. A good relational database design exhibits several essential
102 SCI102 Science
2. Unique Constraint features that contribute to its efficiency, integrity, and
The Unique constraint ensures that all values in a column are scalability: Converted to 3NF:
distinct. This prevents duplicate entries in a specific column. It is - Data Integrity: Ensuring accuracy and
particularly useful for attributes like email addresses, usernames, consistency of data through constraints and Student_ID Course_ID
or phone numbers.
relationships. 101 MATH101
 Unique Constraint: Ensures all values in a column
- Minimal Redundancy: Avoiding duplicate data
are distinct. 102 SCIENCE
to save storage and prevent inconsistencies.
3. Primary Key Constraint
The Primary Key constraint uniquely identifies each record in a
- Scalability: Designing the database to
table. A primary key column must contain unique and non-null accommodate growth in data volume and +
values. Each table can have only one primary key, which may complexity.
Instructor Instructor_Dept
consist of a single column or a combination of columns. - Flexibility: Allowing for easy modifications and
Dr. Smith Mathematics
 Primary Key Constraint: Uniquely identifies each updates to the database schema without
record in a table. significant restructuring. Dr. Johnson Science
4. Check Constraint - Efficient Query Performance: Structuring data to
The Check constraint allows you to specify a condition that must enable fast and efficient retrieval through BCNF (Boyce-Codd Normal Form)
be satisfied by all values in a column. It ensures that only valid optimized queries. A table is in Boyce-Codd Normal Form (BCNF) if it satisfies
data is entered into a column based on a predefined condition. 3NF and additionally Every determinant is a candidate key: A
Example: 5.3 Functional Dependencies determinant is any attribute (or set of attributes) that can
CREATE TABLE Products ( Functional dependencies are a fundamental concept in uniquely determine another attribute. In BCNF, for any
product_id INT,
relational database design, serving as the foundation for functional dependency, the attribute(s) on the left side must
product_name VARCHAR(50),
normalization and the elimination of data anomalies. A be a candidate key.BCNF is a stricter version of 3NF. It
price DECIMAL(10, 2),
CHECK (price > 0)); functional dependency, denoted as X → Y, exists when the eliminates situations where a non-prime attribute (an
In this example, the Check constraint ensures that the 'price' value of attribute Y is determined by the value of attribute X. attribute that is not part of any candidate key) determines
column only accepts values greater than 0. In other words, for any two tuples in a relation, if they have other attributes.
 Check Constraint: Ensures that column values the same value for attribute X, they must also have the same Student_ID Course_ID Instructor Instructor_Dept
satisfy a specific condition. value for attribute Y. 101 MATH101 Dr. Smith Mathematics
By applying these constraints, database administrators can Understanding Functional Dependencies: 102 SCI102 Dr. Johnson Science
ensure data accuracy, integrity, and consistency in their systems. - Definition: A functional dependency X → Y in a relation R Explanation of BCNF:
Each constraint plays a significant role in preventing errors, signifies that for any tuples t1 and t2 in R, if t1[X] = t2[X], BCNF is a stricter version of 3NF. A table is in BCNF if, for
improving data reliability, and maintaining the overall quality of then t1[Y] = t2[Y]. This implies that the attribute set X every non-trivial functional dependency (X → Y), X is a
the database. functionally determines the attribute set Y. superkey.
- Trivial Functional Dependency: A functional dependency is In the above table, assuming the functional dependency
Using Referential Integrity (UNDER Referential Integrity)
considered trivial if Y is a subset of X. For example, Student_ID → Instructor_Dept exists, this would violate
Referential integrity ensures that the relationships between
{EmployeeID, Name} → EmployeeID is trivial because BCNF because Instructor_Dept depends on a non-key
tables remain consistent. It enforces a link between two tables
using a foreign key, ensuring that the data in the foreign key
EmployeeID is part of the determinant set. attribute (Instructor), and Instructor_Dept is not fully
column corresponds to valid data in the referenced primary key - Non-Trivial Functional Dependency: A functional dependent on the primary key (Student_ID and Course_ID
column.A foreign key is a field in one table that uniquely dependency is non-trivial if Y is not a subset of X. For together).
identifies a row in another table. Referential integrity ensures instance, EmployeeID → Name indicates that knowing the Student-Course Table:
that you cannot insert a foreign key value that does not exist in EmployeeID allows us to determine the Name, which is not StudentID Course_ID
the primary key column of the related table. inherently part of the EmployeeID attribute. 101 MATH101
Example: - Examine Sample Data: Reviewing sample data can help 102 SCI102
CREATE TABLE Customers ( identify patterns indicating functional dependencies. If,
customer_id INT PRIMARY KEY, across multiple records, the same value of attribute X Instructor-Course Table:
customer_name VARCHAR(50));
consistently corresponds to the same value of attribute Y, a Course_ID Instructor Instructor_Dept
CREATE TABLE Orders (
order_id INT PRIMARY KEY, functional dependency may exist. MATH101 Dr. Smith Mathematics
customer_id INT, Conclusion:Functional dependencies are a cornerstone of SCI102 Dr. Johnson Science
order_date DATE, relational database design, providing the framework for
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)); understanding how attributes relate to one another. By
In this example, the 'customer_id' column in the Orders table is a meticulously identifying and analyzing functional
foreign key that references the 'customer_id' column in the dependencies, database designers can normalize relations
Customers table. This ensures that an order cannot be placed for
effectively, eliminate data anomalies, and create robust,
a customer who does not exist in the Customers table.
efficient, and reliable database schemas that uphold data
Cascading Actions Referential Integrity:
integrity and support the dynamic needs of applications and
Cascading actions help maintain referential integrity by users.
automatically updating or deleting related rows in a child table Anomalies
when corresponding changes are made in the parent table. There Anomalies are issues that arise in poorly designed databases,
are two common cascading actions: leading to redundancy and inconsistency. The main types
ON DELETE CASCADE: Automatically deletes related rows in the include:
child table when a row in the parent table is deleted. - Insertion Anomaly: Difficulty in adding new data due to
ON UPDATE CASCADE: Automatically updates the foreign key missing other data.
values in the child table when the primary key values in the - Deletion Anomaly: Unintended loss of data due to deletion
parent table are updated. of other data.
Example with Cascading Actions: - Update Anomaly: Inconsistencies resulting from partial
CREATE TABLE Customers (
updates to redundant data.
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50)); Decomposition Using Functional Dependencies
CREATE TABLE Orders ( Decomposition involves breaking down a relation into
order_id INT PRIMARY KEY, smaller relations to eliminate anomalies and redundancy.
customer_id INT, Using functional
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE);
In this example, if a customer is deleted from the Customers
table, all related orders in the Orders table will also be deleted
due to the ON DELETE CASCADE action. Similarly, if the
customer_id in the Customers table is updated, the
corresponding customer_id in the Orders table will be updated
automatically due to the ON UPDATE CASCADE action.
Unit :4 UNIT 5
Assertions and Triggers in Database Management Systems Keys in DBMS
(DBMS) In Database Management Systems (DBMS), keys are
Assertions and triggers are two important tools in database essential concepts used to ensure data integrity, organize
management systems used to maintain data integrity and data, and establish relationships between different tables.
enforce business rules. This note covers the creation and There are various types of keys, each serving a unique
deletion of assertions and triggers, along with a comparison function in database design. Below is an expanded
of the two. explanation of the key types:
1. Assertions 1 Super Key
Assertions are database constraints that specify conditions A super key is a set of one or more attributes (columns) that
or rules that must always hold true for the data in the uniquely identifies each record in a table. It may contain
database. They are applied to the entire database rather additional attributes that are not strictly necessary for
than a specific table or column, ensuring consistency and uniquely identifying records. Essentially
integrity across multiple tables. 2 Candidate Key
Creating Assertions: A candidate key is a minimal super key. It means that no
To create an assertion, the CREATE ASSERTION statement is attribute can be removed from the key without losing its
used, followed by a condition that must always be satisfied. ability to uniquely identify a record in the table.
CREATE ASSERTION valid_salary  Uniqueness: Every value in the candidate key
CHECK ( must be unique across all records.
NOT EXISTS (  Minimalism: No attribute can be removed
SELECT * without causing the key to lose its uniqueness.
FROM Employees 3Primary Key
WHERE salary < 0 )); The primary key is a special type of candidate key that is
In this example, the assertion ensures that no employee in chosen to uniquely identify each record in the table. Every
the Employees table has a negative salary. table must have exactly one primary key, and it cannot
Deleting Assertions accept NULL values.
To delete an assertion, the DROP ASSERTION statement is us 4Reference Key (Foreign Key)
DROP ASSERTION valid_salary; A foreign key (also referred to as a reference key) is an
This statement removes the valid_salary assertion from the attribute (or a set of attributes) in one table that points to
database. the primary key in another table. This relationship links two
2. Triggers tables and ensures the integrity of the relationship between
Triggers are procedural code that automatically execute in them.
response to specific events on a particular table, such as
INSERT, UPDATE, or DELETE. They help enforce business Normalization in DBMS
rules and automate certain database operations. Normalization is the process of organizing the attributes
Creating Triggers (columns) and tables of a relational database to reduce
To create a trigger, the CREATE TRIGGER statement is used, redundancy and dependency by dividing large tables into
specifying the event, timing (BEFORE or AFTER), and the smaller, manageable ones. The primary goal of normalization
action to be performed. is to eliminate data anomalies (insertion, update, and
CREATE TRIGGER update_salary_log deletion anomalies) and improve data integrity by organizing
AFTER UPDATE ON Employees the data in such a way that it avoids redundancy and
FOR EACH ROW inconsistency.Normalization involves applying a series of
BEGIN normal forms (NF), each having specific rules.
INSERT INTO Salary_Log (employee_id, old_salary, 1NF (First Normal Form)
new_salary, update_date) A table is in First Normal Form (1NF) if it meets the following
VALUES (OLD.employee_id, OLD.salary, NEW.salary, conditions:
NOW());  Atomicity: Each cell in the table must contain
END; only one value (i.e., no repeating groups or
In this example, the trigger records salary updates in a arrays).
Salary_Log table whenever the salary in the Employees table  Uniqueness: Each record (row) in the table must
is updated. be unique, typically ensured by a primary key
Deleting Triggers
To delete a trigger, the DROP TRIGGER statement is used. Example of 1NF Violation and Correction
DROP TRIGGER update_salary_log; Student_ID Courses
This statement removes the update_salary_log trigger from
101 Math, Science
the database.
102 History, Geography
3. Assertions v s Triggers
Feature Assertions Trigger Converted to 1NF:
Scope Enforces conditions Executes specific actions Student_ID Course
on the entire on a single table. 101 Math
database. 101 Science
Purpose Ensures data Automates tasks and
102 History
integrity by applying enforces rules at the
global rules. table level. 102 Geography
Event Not event-driven; Event-driven; responds to
Handling checks conditions INSERT, UPDATE, or 2NF (Second Normal Form)
globally. DELETE events. A table is in Second Normal Form (2NF) if it satisfies 1NF and
Impleme Defined using Defined using CREATE additionally:
ntation CREATE TRIGGER. No Partial Dependency: All non-key attributes must be fully
ASSERTION functionally dependent on the entire primary key, not just
Flexibility Limited to enforcing Can perform complex part of it. In simple terms, 2NF eliminates partial
conditions. actions like logging or dependency, where non-key attributes depend on only a
automatic updates. part of the primary key, especially in cases where the
Can impact More efficient for table- primary key is composite (involving more than one column).
Perfor
performance for specific operations. Student_ID Course_ID
mace large databases. 101 MATH101
102 SCI102
Converted to 2NF:
Student_ID Course_ID
101 MATH101
102 SCI102
+
Course_ID Instructor Instructor_Phone
MATH101 Dr. Smith 123456789
SCI102 Dr. Johnson 987654321

You might also like