what is sql-constraints | type of SQL Constraints
What Are SQL Constraints?
SQL constraints are used to enforce business rules and ensure data integrity in a database. They define rules to restrict or allow specific values in database columns, preventing invalid data entry.
SQL provides several types of constraints to maintain accuracy and reliability in database tables:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY (Referential Key)
CHECK
Constraints can be declared at the column level (directly in the column definition) or at the table level (after all column declarations). Table-level constraints are useful when multiple columns participate in the constraint.
The following SQL query demonstrates how to create a table with a PRIMARY KEY constraint:
CREATE TABLE Student (
roll_no VARCHAR(7),
name VARCHAR(20),
address VARCHAR(50),
tot_marks INT,
branch VARCHAR(6),
PRIMARY KEY (roll_no)
);
In this article, we explored SQL constraints and their importance in maintaining database integrity. By using constraints, we can restrict specific actions on table data and ensure accurate and reliable information storage.
If you have any questions, feel free to reach out to us!