TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2474 3.69K
๐Ÿ”ฅ Now, let's move to the next topic:

โœ… SQL Constraints

Essential for data integrity & important in interviews ๐Ÿ’ฏ

๐Ÿง  1. What are Constraints in SQL?
Constraints are rules applied on table columns
๐Ÿ‘‰ to maintain accurate & valid data

Think like this ๐Ÿ‘‡
๐Ÿ‘‰ โ€œDatabase safety rulesโ€

โšก 2. Why Use Constraints?
โœ” Prevent invalid data
โœ” Maintain consistency
โœ” Improve data integrity
โœ” Enforce relationships

๐Ÿ“Š Types of Constraints

NOT NULL
โ€ข Purpose: Prevent NULL values

UNIQUE
โ€ข Purpose: No duplicate values

PRIMARY KEY
โ€ข Purpose: Unique identifier

FOREIGN KEY
โ€ข Purpose: Create relationship

CHECK
โ€ข Purpose: Apply condition

DEFAULT
โ€ข Purpose: Set default value

๐Ÿ”ฅ 3. NOT NULL Constraint
๐Ÿ‘‰ Column cannot contain NULL

CREATE TABLE employees (
emp_id INT,
name VARCHAR(50) NOT NULL
);


๐Ÿ”ฅ 4. UNIQUE Constraint
๐Ÿ‘‰ Prevent duplicate values

CREATE TABLE users (
email VARCHAR(100) UNIQUE
);


๐Ÿ”ฅ 5. PRIMARY KEY
๐Ÿ‘‰ Unique + NOT NULL

CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50)
);


โœ” Every row must have unique emp_id

๐Ÿ”ฅ 6. FOREIGN KEY
๐Ÿ‘‰ Creates relationship between tables

CREATE TABLE employees (
emp_id INT PRIMARY KEY,
dept_id INT,
FOREIGN KEY (dept_id)
REFERENCES departments(dept_id)
);


โœ” dept_id must exist in departments table

๐Ÿ”ฅ 7. CHECK Constraint
๐Ÿ‘‰ Restrict values using condition

CREATE TABLE employees (
salary INT CHECK (salary > 0)
);


โœ” Salary cannot be negative

๐Ÿ”ฅ 8. DEFAULT Constraint
๐Ÿ‘‰ Assign default value automatically

CREATE TABLE employees (
city VARCHAR(50) DEFAULT 'Pune'
);


๐ŸŽฏ 9. Practice Tasks
1. Create table using PRIMARY KEY
2. Add UNIQUE constraint on email
3. Create FOREIGN KEY relationship
4. Use CHECK for salary > 0
5. Add DEFAULT city value

โšก Mini Challenge ๐Ÿ”ฅ
๐Ÿ‘‰ Create students table with:
โ€ข student_id โ†’ PRIMARY KEY
โ€ข email โ†’ UNIQUE
โ€ข age > 18 using CHECK
โ€ข city default = 'Mumbai'

Double Tap โค๏ธ For More
  • โค 11
  • ๐Ÿ‘ 2
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’