HTML Dropdown

Friday, 8 May 2026

Making Sense of SQL Keys and Constraints: A Friendly Guide

When learning SQL, understanding keys and constraints is an important step toward designing reliable and well‑structured databases. These concepts help maintain data accuracy, avoid duplication, and create meaningful relationships between tables. The handwritten notes on “Keys and Constraints in SQL” present these ideas in a simple, visual, and exam‑friendly way, making them easier for beginners to grasp.




Why Keys and Constraints Matter in SQL

In a database, data is stored in tables, and each table can grow large very quickly. Without proper rules, data can become messy, duplicated, or incorrect. Keys and constraints act like rules and identifiers, ensuring the data remains organized and trustworthy.


Primary Key: Identifying Each Record Uniquely

A Primary Key is a column (or set of columns) used to uniquely identify each row in a table.

Key points about a Primary Key:

  • It must be unique
  • It cannot be NULL
  • Each table can have only one primary key

Simple example:

id INT PRIMARY KEY

Here, the id column ensures that every record in the table can be identified without confusion. In handwritten notes, this concept is often shown with a key symbol next to a table, helping students visually remember that the primary key is special.


Foreign Key: Connecting Tables Together

A Foreign Key is used to create a link between two tables. It refers to the primary key of another table.

Why it is useful:

  • Connects related data across tables
  • Maintains data consistency
  • Avoids duplicate information

Simple example:

  • A Student table has a primary key id
  • A Course table contains student_id as a foreign key

This relationship shows which student is enrolled in which course. 


Constraints: Rules That Protect Your Data

Constraints are rules applied to table columns to control the type of data that can be stored. T


NOT NULL Constrain

The NOT NULL constraint ensures that a column cannot have empty values.

Meaning:
Every record must contain data in that column.

Example:

name VARCHAR(50) NOT NULL

This means the name field cannot be left blank.


UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

Purpose:

  • Prevents duplicate entries
  • Helps maintain data uniqueness

Example:

email VARCHAR(50) UNIQUE

No two records can have the same email address.


DEFAULT Constraint

The DEFAULT constraint assigns a value automatically when no value is provided.

Helpful when:

  • A common value is needed frequently
  • Data should never remain empty

Example:

salary INT DEFAULT 30000

``

If no salary is entered, the value 30000 is stored automatically.


CHECK Constraint

The CHECK constraint ensures that data follows a specific condition.

Used to:

  • Validate data before storing it

Example:

CHECK (age >= 18

This ensures that only valid ages (18 or above) are allowed.


No comments:

Post a Comment