Загрузка...

Primary Key | Foreign Key | Unique Key | Null Not | Check Constraint | Integrity |Constraint in SQL

SQL Constraints – Complete Explanation
🔹 What are Constraints?

Constraints are rules applied to table columns in SQL to ensure data accuracy, consistency, and integrity.

👉 They prevent invalid data from being entered into the database.

🔹 Types of SQL Constraints
✅ 1. NOT NULL Constraint
Ensures a column cannot have NULL (empty) values

Example:

CREATE TABLE Student (
ID INT NOT NULL,
Name VARCHAR(50) NOT NULL
);
👉 Every record must have ID and Name

✅ 2. UNIQUE Constraint
Ensures all values in a column are different

Example:

CREATE TABLE Student (
Email VARCHAR(100) UNIQUE
);
👉 No duplicate email allowed

✅ 3. PRIMARY KEY Constraint
Uniquely identifies each record
Cannot contain NULL values
Only one primary key per table

Example:

CREATE TABLE Student (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);
👉 ID will be unique for every student

✅ 4. FOREIGN KEY Constraint
Creates a relationship between two tables
Refers to a PRIMARY KEY in another table

Example:

CREATE TABLE Orders (
OrderID INT,
StudentID INT,
FOREIGN KEY (StudentID) REFERENCES Student(ID)
);
👉 Ensures StudentID exists in Student table

✅ 5. CHECK Constraint
Ensures values meet a specific condition

Example:

CREATE TABLE Student (
Age INT CHECK (Age = 18)
);
👉 Age must be 18 or above

✅ 6. DEFAULT Constraint
Assigns a default value if no value is provided

Example:

CREATE TABLE Student (
Country VARCHAR(50) DEFAULT 'India'
);
👉 If no value is given → "India" is stored

🎯 Why Constraints are Important?
Maintain data integrity
Avoid duplicate or invalid data
Improve database reliability
Help in data relationships
🚀 Summary
NOT NULL → No empty values
UNIQUE → No duplicate values
PRIMARY KEY → Unique identifier
FOREIGN KEY → Table relationship
CHECK → Condition validation
DEFAULT → Default value

#SQL
#SQLConstraints
#PrimaryKey
#ForeignKey
#DatabaseDesign
#LearnSQL
#SQLTutorial
#SQLForBeginners
#DataAnalytics
#Database
#TechLearning
#Coding
#SQLCourse
#DataEngineering
#Programming
sql constraints
primary key in sql
foreign key in sql
not null constraint sql
unique constraint sql
check constraint sql
default constraint sql
sql tutorial for beginners
learn sql step by step
sql full course
database constraints explained
sql interview questions
sql table constraints

Видео Primary Key | Foreign Key | Unique Key | Null Not | Check Constraint | Integrity |Constraint in SQL канала Insights Analytics
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять