Specifying Constraints in SQL
Specifying Constraints in SQL
In a relational database, constraints are rules that restrict the values that can be stored in the database. These rules ensure the correctness, consistency, and integrity of the data. SQL provides several mechanisms to specify constraints directly as part of table definitions.
The constraints discussed in this section are schema-based constraints, meaning they are declared using SQL’s Data Definition Language (DDL) and are enforced automatically by the DBMS.
The main types of constraints covered here are:
-
Attribute-level constraints and defaults
-
Key constraints
-
Referential integrity constraints
-
Tuple-level (row-level) constraints using
CHECK -
Naming constraints for better management
Specifying Attribute Constraints and Attribute Defaults
1. NOT NULL Constraint
By default, SQL allows attributes to take the value NULL, which represents missing or unknown information. However, in many cases, NULL values are not acceptable.
-
The NOT NULL constraint prevents an attribute from having NULL values.
-
All attributes that are part of a PRIMARY KEY are automatically NOT NULL.
-
NOT NULL can also be applied to non-key attributes when values are mandatory.
Example:
This ensures that every employee must have a salary value.
2. DEFAULT Clause
The DEFAULT clause assigns a value automatically when a tuple is inserted without specifying that attribute.
-
Default values reduce data-entry errors.
-
If no DEFAULT is specified:
-
The default value is NULL (unless NOT NULL is specified).
-
Example:
If a new employee is inserted without specifying a department number, the department is automatically set to 1.
3. CHECK Constraint on Attributes or Domains
The CHECK clause restricts attribute values to satisfy a logical condition.
-
Applied at:
-
Attribute level
-
Domain level
-
-
Enforced whenever data is inserted or updated.
Example (attribute-level CHECK):
This ensures department numbers are between 1 and 20.
4. CHECK with CREATE DOMAIN
Domains allow reusable constraints across multiple tables.
Example:
This domain can then be used wherever a department number is required, improving:
-
Consistency
-
Maintainability
-
Readability
Specifying Key and Referential Integrity Constraints
1. Primary Key Constraint
A PRIMARY KEY uniquely identifies each tuple in a relation.
Characteristics:
-
Must be unique
-
Cannot be NULL
-
Can be:
-
Single attribute
-
Composite (multiple attributes)
-
Example (single attribute):
Example (composite key):
2. UNIQUE Constraint (Candidate Keys)
The UNIQUE constraint ensures that attribute values are unique but allows NULLs (unless NOT NULL is specified).
-
Used to represent candidate keys
-
A table can have multiple UNIQUE constraints
Example:
3. Referential Integrity and FOREIGN KEY
A FOREIGN KEY establishes a relationship between two tables.
-
Enforces that a value in the foreign key must:
-
Match a primary key in the referenced table, or
-
Be NULL (if allowed)
-
Example:
Referential Triggered Actions
When a referenced tuple is deleted or updated, SQL allows the designer to specify what action should be taken.
a. RESTRICT (Default)
-
Rejects the operation if it causes a violation.
b. SET NULL
-
Sets the foreign key value to NULL.
c. SET DEFAULT
-
Sets the foreign key to its default value.
d. CASCADE
-
Propagates changes automatically.
Example Interpretation:
-
ON DELETE CASCADE: deleting a department deletes all related projects. -
ON UPDATE CASCADE: updating a primary key updates all referencing foreign keys.
Design Guideline :
-
CASCADE is suitable for:
-
Relationship tables (WORKS_ON)
-
Weak entity tables (DEPENDENT)
-
Multivalued attributes (DEPT_LOCATIONS)
-
6.2.3 Giving Names to Constraints
SQL allows constraints to be explicitly named using the CONSTRAINT keyword.
Advantages:
-
Easier to drop or modify constraints
-
Better error messages
-
Improved schema readability
Example:
Rules:
-
Constraint names must be unique within a schema
-
Naming is optional but strongly recommended
Specifying Constraints on Tuples Using CHECK
Row-Level (Tuple-Level) CHECK Constraints
CHECK constraints can also be applied at the table level, where they enforce conditions involving multiple attributes in the same row.
-
Evaluated whenever a row is inserted or updated
-
Cannot reference other tables (unless using assertions)
Example:
This ensures that:
-
A manager cannot start managing a department before the department exists.
CHECK vs Assertions
-
CHECK (table-level):
-
Applies to individual tuples
-
Limited in expressive power
-
-
ASSERTION:
-
Can specify constraints involving multiple tables
-
Uses full SQL queries
-
Discussed later
-
Summary
| Constraint Type | Purpose |
|---|---|
| NOT NULL | Disallows NULL values |
| DEFAULT | Assigns automatic values |
| CHECK | Restricts allowed values |
| PRIMARY KEY | Uniquely identifies tuples |
| UNIQUE | Enforces candidate keys |
| FOREIGN KEY | Maintains referential integrity |
| CASCADE / SET NULL / SET DEFAULT | Defines actions on updates/deletes |
| CONSTRAINT naming | Improves maintainability |
Comments
Post a Comment