[go: up one dir, main page]

0% found this document useful (0 votes)
17 views3 pages

SQL Constraints With Examples

The document outlines various SQL constraints, including PRIMARY KEY, UNIQUE, NOT NULL, CHECK, DEFAULT, and FOREIGN KEY, along with their definitions and examples. Each constraint serves a specific purpose in ensuring data integrity within a database. A summary table provides a quick reference for each constraint's description, NULL allowance, uniqueness, and additional notes.

Uploaded by

21501a0473
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

SQL Constraints With Examples

The document outlines various SQL constraints, including PRIMARY KEY, UNIQUE, NOT NULL, CHECK, DEFAULT, and FOREIGN KEY, along with their definitions and examples. Each constraint serves a specific purpose in ensuring data integrity within a database. A summary table provides a quick reference for each constraint's description, NULL allowance, uniqueness, and additional notes.

Uploaded by

21501a0473
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Constraints with Examples

1. PRIMARY KEY Constraint

Ensures each row in a table is unique and not NULL.

Example:

CREATE TABLE students (

student_id INT PRIMARY KEY,

name VARCHAR(100)

);

2. UNIQUE Constraint

Ensures values in a column (or group of columns) are distinct.

Example:

CREATE TABLE users (

user_id INT PRIMARY KEY,

email VARCHAR(100) UNIQUE

);

3. NOT NULL Constraint

Ensures that a column must have a value (no NULL).

Example:

CREATE TABLE employees (

emp_id INT PRIMARY KEY,


SQL Constraints with Examples

name VARCHAR(100) NOT NULL

);

4. CHECK Constraint

Ensures that values meet a specific condition.

Example:

CREATE TABLE accounts (

acc_id INT PRIMARY KEY,

balance DECIMAL(10, 2),

CHECK (balance >= 0)

);

5. DEFAULT Constraint

Sets a default value if none is provided.

Example:

CREATE TABLE customers (

cust_id INT PRIMARY KEY,

country VARCHAR(50) DEFAULT 'India'

);

6. FOREIGN KEY Constraint

Links two tables; ensures value in one table matches a primary key in another.
SQL Constraints with Examples

Example:

CREATE TABLE departments (

dept_id INT PRIMARY KEY,

dept_name VARCHAR(100)

);

CREATE TABLE employees (

emp_id INT PRIMARY KEY,

emp_name VARCHAR(100),

dept_id INT,

FOREIGN KEY (dept_id) REFERENCES departments(dept_id)

);

Summary Table

Constraint | Description | Allows NULL | Unique | Notes

PRIMARY KEY | Unique row ID | No | Yes | Only one per table

UNIQUE | Distinct values | Yes | Yes | Can be on multiple columns

NOT NULL | No empty values | No | No | Requires input

CHECK | Rule-based values | Yes | No | Conditions like age > 0

DEFAULT | Fills missing vals | Yes | No | Uses if value not given

FOREIGN KEY | Links two tables | Yes | No | Refers to another table

You might also like