Day 35: Data Validation and Constraints
Introduction:
Data validation and constraints ensure that the data entered into a database is accurate, reliable, and adheres to specific rules and standards. Constraints are rules applied to table columns to enforce data integrity.
Key Concepts:
NOT NULL: Ensures that a column cannot have a NULL value.
UNIQUE: Ensures that all values in a column are unique.
PRIMARY KEY: Uniquely identifies each row in a table.
FOREIGN KEY: Establishes a relationship between columns in two tables.
CHECK: Ensures that values in a column meet a specific condition.
DEFAULT: Sets a default value for a column if no value is specified.
SQL Commands and Examples:
NOT NULL Constraint:
Ensure that a column cannot have a NULL value.
-- Create a Students table with a NOT NULL constraint on the LastName column
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255) NOT NULL,
Age INT
);
UNIQUE Constraint:
Ensure that all values in a column are unique.
-- Create a Courses table with a UNIQUE constraint on the CourseName column
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(255) UNIQUE,
Credits INT
);
PRIMARY KEY Constraint:
Uniquely identify each row in a table.
-- Create an Employees table with a PRIMARY KEY constraint on the EmployeeID column
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255)
);
FOREIGN KEY Constraint:
Establish a relationship between columns in two tables.
-- Create an Enrollments table with a FOREIGN KEY constraint on the StudentID and CourseID columns
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
CHECK Constraint:
Ensure that values in a column meet a specific condition.
-- Create a Students table with a CHECK constraint to ensure age is between 18 and 65
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
Age INT CHECK (Age BETWEEN 18 AND 65)
);
DEFAULT Constraint:
Set a default value for a column if no value is specified.
-- Create a Students table with a DEFAULT constraint on the Age column
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255),
Age INT DEFAULT 18
);
Practice Exercise:
Use the Students and Courses tables.
Write a query to create a Teachers table with columns
TeacherID,FirstName,LastName,CourseIDand apply the appropriate constraints.Write a query to add a NOT NULL constraint to the
LastNamecolumn in the Students table.Write a query to add a UNIQUE constraint to the
CourseNamecolumn in the Courses table.Write a query to add a CHECK constraint to ensure
Creditsin the Courses table is greater than 0.Write a query to add a DEFAULT constraint to set the
Agecolumn in the Students table to 18.
-- Create a Teachers table with appropriate constraints
CREATE TABLE Teachers (
TeacherID INT PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255) NOT NULL,
CourseID INT,
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
-- Add a NOT NULL constraint to the LastName column in the Students table
ALTER TABLE Students
MODIFY LastName VARCHAR(255) NOT NULL;
-- Add a UNIQUE constraint to the CourseName column in the Courses table
ALTER TABLE Courses
ADD CONSTRAINT unique_course_name UNIQUE (CourseName);
-- Add a CHECK constraint to ensure Credits in the Courses table is greater than 0
ALTER TABLE Courses
ADD CONSTRAINT check_credits CHECK (Credits > 0);
-- Add a DEFAULT constraint to set the Age column in the Students table to 18
ALTER TABLE Students
ALTER COLUMN Age SET DEFAULT 18;
Important Tips:
Constraints help maintain data integrity and prevent invalid data from being entered into the database.
Use appropriate constraints to enforce rules and standards that ensure data accuracy and reliability.
Regularly review and update constraints as necessary to align with changing data requirements.
Understanding and applying constraints ensures that your database maintains high data integrity and reliability.
0 Comments