A. Database Program WITHOUT Constraints
👉 Basic table creation (no Primary Key, Foreign Key, Check, Unique)
CREATE TABLE Department (
Insert Sample Data (Without Constraints)
INSERT INTO Department VALUES
INSERT INTO Employee VALUES
(101, 'Amit Sharma', 'Manager', 60000, 1),
(102, 'Neha Verma', 'Developer', 50000, 2),
(103, 'Rahul Patil', 'Analyst', 45000, 3);
INSERT INTO Appraisal VALUES
(1, 101, 2024, 9, 'Excellent'),
(2, 102, 2024, 8, 'Very Good'),
(3, 103, 2024, 6, 'Good');
B. Database Program WITH Constraints
👉 Demonstrates Primary Key, Foreign Key, NOT NULL, UNIQUE, CHECK
CREATE TABLE Department1 (
dept_name VARCHAR(50) NOT NULL UNIQUE,
location VARCHAR(50) NOT NULL
emp_name VARCHAR(50) NOT NULL,
designation VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) CHECK (salary > 0),
FOREIGN KEY (dept_id) REFERENCES Department1(dept_id)
CREATE TABLE Appraisal1 (
appraisal_id INT PRIMARY KEY,
appraisal_year INT CHECK (appraisal_year >= 2020),
performance_score INT CHECK (performance_score BETWEEN 1 AND 10),
FOREIGN KEY (emp_id) REFERENCES Employee1(emp_id)
Create Index (Optional but Recommended)
CREATE INDEX idx_employee_name
Insert Valid Data (With Constraints)
INSERT INTO Department VALUES
INSERT INTO Employee VALUES
(101, 'Amit Sharma', 'Manager', 60000, 1),
(102, 'Neha Verma', 'Developer', 50000, 2),
(103, 'Rahul Patil', 'Analyst', 45000, 3);
INSERT INTO Appraisal VALUES
(1, 101, 2024, 9, 'Excellent'),
(2, 102, 2024, 8, 'Very Good'),
(3, 103, 2024, 6, 'Good');
Constraints are used to enforce rules on data to maintain accuracy, consistency, and integrity in the database.
1. Department Table (PRIMARY, UNIQUE, NOT NULL, DEFAULT)
CREATE TABLE Department (
dept_name VARCHAR(50) NOT NULL UNIQUE,
location VARCHAR(50) DEFAULT 'Head Office'
PRIMARY KEY → dept_id
UNIQUE → dept_name
NOT NULL → dept_name
DEFAULT → location
2. Employee Table (PRIMARY, FOREIGN, CHECK, NOT NULL, DEFAULT)
emp_name VARCHAR(50) NOT NULL,
designation VARCHAR(50) NOT NULL,
CHECK (gender IN ('M','F')),
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
3. Appraisal Table (PRIMARY, FOREIGN, CHECK, BETWEEN, DEFAULT)
appraisal_id INT PRIMARY KEY,
CHECK (appraisal_year BETWEEN 2020 AND 2030),
CHECK (performance_score BETWEEN 1 AND 10),
rating VARCHAR(20) DEFAULT 'Pending',
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id)
PRIMARY KEY → appraisal_id
FOREIGN KEY → emp_id
CHECK BETWEEN → appraisal_year
CHECK BETWEEN → performance_score
DEFAULT → rating
4. Insert Data (Showing DEFAULT & BETWEEN in Action)
INSERT INTO Department (dept_id, dept_name)
👉 location automatically becomes “Head Office”
(emp_id, emp_name, designation, salary, gender, dept_id)
(101, 'Amit Sharma', 'Manager', 60000, 'M', 1);
👉 join_date automatically set
(appraisal_id, emp_id, appraisal_year, performance_score)
👉 rating automatically becomes “Pending”
5. Example of INVALID Data (for Viva Explanation)
VALUES (2, 101, 2018, 15, 'Excellent');
❌ Error because:
6. Create Index (Performance Constraint – Logical)
CREATE INDEX idx_employee_name