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:

  1. Attribute-level constraints and defaults

  2. Key constraints

  3. Referential integrity constraints

  4. Tuple-level (row-level) constraints using CHECK

  5. 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:

Salary DECIMAL(10,2) NOT NULL

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:

Dno INT NOT NULL DEFAULT 1

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):

Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21)

This ensures department numbers are between 1 and 20.


4. CHECK with CREATE DOMAIN

Domains allow reusable constraints across multiple tables.

Example:

CREATE DOMAIN D_NUM AS INTEGER CHECK (D_NUM > 0 AND D_NUM < 21);

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):

Dnumber INT PRIMARY KEY

Example (composite key):

PRIMARY KEY (Essn, Pno)

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:

Dname VARCHAR(15) UNIQUE

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:

FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber)

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.

ON DELETE SET NULL

c. SET DEFAULT

  • Sets the foreign key to its default value.

ON DELETE SET DEFAULT

d. CASCADE

  • Propagates changes automatically.

ON DELETE CASCADE ON UPDATE CASCADE

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:

CONSTRAINT EMPPK PRIMARY KEY (Ssn)

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:

CHECK (Dept_create_date <= Mgr_start_date)

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 TypePurpose
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

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS