Database Concepts: Data Store, Schema,
Constraints, and Keys
1. Data Store, Schema, and Constraints
Data Store
Definition
A data store is any repository where data is stored, managed, and retrieved
for future use.
Types:
• Physical (e.g., paper files, magnetic tapes)
• Digital (e.g., databases, data warehouses, cloud storage)
Example
Examples:
→ Relational database – MySQL, PostgreSQL
→ NoSQL database – MongoDB, Cassandra
→ Cloud storage – Google Cloud Storage, AWS S3
1 1
Student Has ID Card
1:1 Relationship
1 M
Department Employs Employee
1:M Relationship
1
M N
Student Enrolls Course
M:N Relationship
Purpose:
1. Store persistent data
2. Allow access for reading and writing
3. Ensure data integrity and security
Schema
Definition
A schema is the structure/blueprint of how data is organized in a database.
It defines:
⋆ Tables (or collections in NoSQL)
⋆ Columns/attributes and their data types
⋆ Relationships between tables
⋆ Constraints that enforce data rules
Types of Schemas:
⋄ Physical schema – How data is physically stored
⋄ Logical schema – How data is logically organized for the user
⋄ View schema – How data is presented to different users
Relational Database Schema Example
Column Name Data Type Constraint
student id INT PRIMARY KEY
name VARCHAR(50) NOT NULL
age INT CHECK (age ¿ 0)
2
Constraints
Definition
Constraints are rules applied to data in the database to ensure accuracy and
reliability.
Types of Constraints:
Constraint Details
1. NOT NULL – Ensures a column cannot store NULL values.
2. UNIQUE – Ensures all values in a column are unique.
3. PRIMARY KEY – Uniquely identifies each record (combination of NOT NULL
+ UNIQUE).
4. FOREIGN KEY – Links a column to another table’s primary key (maintains
referential integrity).
5. CHECK – Ensures values meet a condition (e.g., salary > 0).
6. DEFAULT – Sets a default value if none is provided.
SQL Table Creation with Constraints
CREATE TABLE Students (
student_id INT PRIMARY KEY ,
name VARCHAR (50) NOT NULL ,
age INT CHECK ( age > 0) ,
email VARCHAR (100) UNIQUE ,
dept_id INT ,
FOREIGN KEY ( dept_id ) REFERENCES Departments ( dept_id )
);
2. Explanation of NOT NULL, UNIQUE, CHECK,
DEFAULT Constraints
NOT NULL
Constraint Details
Meaning: The column must have a value; it cannot store NULL.
Use case: Ensures important fields are always filled.
3
Example
CREATE TABLE Employees (
emp_id INT NOT NULL ,
name VARCHAR (50) NOT NULL
);
Here, both emp id and name must have a value for every row.
UNIQUE
Constraint Details
Meaning: All values in the column must be different.
Use case: Prevents duplicate entries in a column.
Example
CREATE TABLE Employees (
email VARCHAR (100) UNIQUE
);
No two employees can have the same email address.
CHECK
Constraint Details
Meaning: Ensures that values in a column satisfy a given condition.
Use case: Restricts data entry to valid ranges or formats.
Example
CREATE TABLE Employees (
age INT CHECK ( age >= 18)
);
Only employees 18 years or older can be entered.
DEFAULT
Constraint Details
Meaning: Sets a default value for a column if no value is given during data
insertion.
Use case: Automatically fills in a value when the user doesn’t provide one.
4
Example
CREATE TABLE Employees (
country VARCHAR (50) DEFAULT ’ Bangladesh ’
);
If no country is specified when inserting, it will automatically set ’Bangladesh’.
Quick Table Summary
Constraint Purpose Example Usage
name VARCHAR(50) NOT
NOT NULL Prevents missing values
NULL
email VARCHAR(100)
UNIQUE Prevents duplicate values
UNIQUE
age INT CHECK (age >=
CHECK Ensures a condition is met
18)
country VARCHAR(50)
DEFAULT Sets a value if none provided
DEFAULT ’Bangladesh’
3. Keys: Characteristics and Examples
What is a Key?
Definition
A key in a database is an attribute (or set of attributes) that is used to identify
rows in a table and/or establish relationships between tables.
5
Types of Keys, Characteristics & Examples
Key Type Characteristics Example
- Uniquely identifies each row.
- Cannot be NULL. student id in a
Primary Key
- Only one primary key per table (can be Students table.
composite).
- Any attribute or set of attributes that
In Students, both
could serve as a primary key.
student id and email
Candidate Key - Must be unique and not NULL.
could be candidate
- One is chosen as the primary key, others
keys.
become alternate keys.
- Candidate keys that are not chosen as email in Students if
Alternate Key the primary key. student id is the pri-
- Still unique. mary key.
- References the primary key of another
table. dept id in
Foreign Key - Maintains referential integrity. Students referencing
- Can have duplicate values. Departments(dept id).
- Can be NULL unless specified otherwise.
- A key made of two or more columns to
(course id,
uniquely identify a row.
Composite Key student id) in an
- Used when a single column is not unique
Enrollments table.
enough.
- Any set of attributes that uniquely
{student id},
identifies a row (includes candidate keys
Super Key {student id, name}
and primary key, plus possibly extra at-
both are super keys.
tributes).
Example SQL Table with Keys
Example
CREATE TABLE Departments (
dept_id INT PRIMARY KEY ,
dept_name VARCHAR (50) UNIQUE
);
CREATE TABLE Students (
student_id INT PRIMARY KEY , -- Primary Key
name VARCHAR (50) NOT NULL ,
email VARCHAR (100) UNIQUE , -- Alternate Key
dept_id INT , -- Foreign Key
FOREIGN KEY ( dept_id ) REFERENCES Departments ( dept_id )
);
CREATE TABLE Enrollments (
course_id INT ,
student_id INT ,
PRIMARY KEY ( course_id , student_id ) -- Composite Key
);
6
Quick Visual Relationship
Key Information
→ Primary Key → Unique identifier in its own table.
→ Foreign Key → Connects tables.
→ Composite Key → Multiple columns combined for uniqueness.
→ Candidate Key → Potential unique identifiers.
→ Alternate Key → Unique identifiers not used as primary key.
→ Super Key → Any unique identifier set (includes extra attributes).
Unary Supervises
Employee Student Belongs Dept
Binary
Teacher
Teaches
Course Student
Ternary
derived from
Date of Birth Age
Stored Derived
Student
7
Derived Attribute Example
Age is derived from Date of Birth:
→ Stored: Date of Birth (1995-03-15)
→ Calculated: Age = Current Year - Birth Year = 2025 - 1995 = 30 years
8
4. Diagram Showing Relationships Between Keys
Super Key
Any set of attributes that
uniquely identifies a row
Candidate Key
Minimal super key; po-
tential primary keys
Primary Key
Composite Key
Chosen candidate key;
Key made of multiple columns
unique and not NULL
Foreign Key
References primary
key of another table
9
5. Entity-Relationship (ER) Diagrams and Advanced
Database Concepts
ER Diagram Components
Definition
An Entity-Relationship (ER) Diagram is a visual representation that shows
the relationships among entities in a database.
ER Diagram Symbols:
• Rectangle – Represents an Entity
• Oval – Represents an Attribute
• Diamond – Represents a Relationship
• Double Oval – Represents a Multivalued Attribute
• Underlined Oval – Represents a Key Attribute
• Dashed Oval – Represents a Derived Attribute
Types of Entities
Constraint Details
Entity Types:
1. Strong Entity – Has primary key, can exist independently
2. Weak Entity – Depends on strong entity, no primary key of its own
Strong vs Weak Entity Examples
→ Strong Entity: Employee (emp id, emp name, phone, address)
→ Weak Entity: Dependent (name, relationship) – depends on Employee
10
Key Information
Attribute Classifications:
1. Simple Attribute – Cannot be subdivided (e.g., Name, Age)
2. Composite Attribute – Can be subdivided (e.g., Address = Street + City +
State)
3. Single-valued – Contains single value (e.g., SSN)
4. Multivalued – Contains multiple values (e.g., Phone numbers)
5. Derived – Calculated from other attributes (e.g., Age from Date of Birth)
6. Key – Uniquely identifies entity
7. Stored – Physically stored in database
Types of Relationships
Constraint Details
Relationship Cardinalities:
1. One-to-One (1:1) – Each entity in A relates to exactly one in B
2. One-to-Many (1:M) – Each entity in A relates to multiple in B
3. Many-to-One (M:1) – Multiple entities in A relate to one in B
4. Many-to-Many (M:N) – Multiple entities in A relate to multiple in B
Relationship Examples
⋆ 1:1 – Student Student ID Card
⋆ 1:M – Department → Employees
⋆ M:1 – Students → Course
⋆ M:N – Students Courses (via Enrollment)
Participation Constraints
Definition
Participation Types:
⋄ Total Participation – Every entity must participate (double line)
⋄ Partial Participation – Some entities may not participate (single line)
11
Example
Examples:
→ Total: Employee must belong to a Department
→ Partial: Employee may have Dependents
Degree of Relationships
Key Information
Relationship Degrees:
1. Unary (1°) – Relationship within same entity
2. Binary (2°) – Relationship between two entities
3. Ternary (3°) – Relationship among three entities
Complete ER Diagram Example
Symbol Shape Represents
Entity Rectangle A real-world object (Student, Course)
Double Rectan-
Weak Entity Entity dependent on another entity
gle
Attribute Oval Property of an entity (Name, Age)
Key Attribute Underlined Oval Uniquely identifies an entity
Multivalued
Double Oval Attribute with multiple values
Attr.
Derived At-
Dashed Oval Calculated from other attributes
tribute
Relationship Diamond Association between entities
12