Enforcing rules using constraints

In MySQL, constraints are rules enforced on data columns to ensure the integrity, validity, and reliability of the data within the database. Here are the various constraints that can be applied to tables and columns:

Column-Level Constraints

1.NOT NULL: Ensures that a column cannot have a NULL value.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL
);

2.UNIQUE: Ensures that all values in a column are unique.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(100) UNIQUE
);

3.PRIMARY KEY: Uniquely identifies each record in a table. It combines NOT NULL and UNIQUE.
CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL
);


4.AUTO_INCREMENT: Automatically generates a unique number for the column.
CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL
);

5.DEFAULT: Provides a default value for a column when no value is specified.
CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    enrollment_date DATE DEFAULT CURRENT_DATE
);

6.CHECK: Ensures that the values in a column satisfy a specific condition. (Supported in MySQL 8.0 and later.)
CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    gender ENUM('Male', 'Female', 'Other') CHECK (gender IN ('Male', 'Female', 'Other'))
);


Table-Level Constraints


1.FOREIGN KEY: Ensures referential integrity by linking one or more columns in a table to one or more columns in another table.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    birthdate DATE NOT NULL,
    email VARCHAR(100) NOT NULL,
    gender ENUM('Male', 'Female', 'Other') NOT NULL,
    enrollment_date DATE NOT NULL
);

CREATE TABLE courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_name VARCHAR(100) NOT NULL,
    course_code VARCHAR(10) NOT NULL,
    credits INT NOT NULL
);

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    enrollment_date DATE NOT NULL,
    PRIMARY KEY (student_id, course_id),
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(id)
);

2.UNIQUE (Table-Level): Ensures that the combination of two or more columns is unique.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL,
    UNIQUE (first_name, last_name)
);


Summary of Constraints
NOT NULL: Ensures a column cannot have a NULL value.
UNIQUE: Ensures all values in a column are unique.
PRIMARY KEY: Uniquely identifies each record in a table.
AUTO_INCREMENT: Automatically generates a unique number for the column.
DEFAULT: Provides a default value for a column.
CHECK: Ensures that the values in a column satisfy a specific condition.
FOREIGN KEY: Ensures referential integrity by linking columns in different tables.

These constraints help enforce data integrity and consistency within the database.

Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

KTU DBMS LAB CSL 333 Syllabus and Experiments

Creating a Database and Table ,executing queries - MySQL Lab Exercise