IT220 Database Management System

Database Management SystemUnit 67 min read

Database Security & Integrity Constraints

Unit 6 of Database Management System: Explores how databases protect data from unauthorized access, ensure accuracy, and maintain consistency through constraints, access control, and security mechanisms.

TAKEAWAYS:

  • Integrity constraints (e.g., primary keys, foreign keys) enforce data accuracy and consistency.
  • Security mechanisms (authentication, encryption, authorization) protect data from breaches.
  • Database auditing tracks unauthorized access and data changes.
  • SQL constraints (NOT NULL, UNIQUE, CHECK) enforce rules at the database level.
  • Role-based access control (RBAC) grants permissions based on user roles.
  • Malware and SQL injection are real threats requiring defense strategies.

1. Introduction to Database Security

Databases store sensitive data (e.g., customer records, financial transactions). Security ensures:

  • Confidentiality: Only authorized users access data.
  • Integrity: Data remains accurate and unaltered.
  • Availability: Data is accessible when needed.

Threats to Database Security

mindmap
  root((Database Security Threats))
    Unauthorized Access
    Malware & Viruses
    SQL Injection
    Data Leakage
    Denial-of-Service (DoS)

Example in Nepal:

  • eSewa/Khalti use encryption to protect transaction data from hackers.
  • NEPSE (Nepal Stock Exchange) enforces strict access controls to prevent stock manipulation.

2. Integrity Constraints

Integrity constraints ensure data accuracy and consistency. Types include:

A. Entity Integrity

  • Primary Key Constraint: Ensures no duplicate or null values in a primary key.
    erDiagram
        EMPLOYEE ||--o{ DEPARTMENT : "works_in"
        EMPLOYEE {
            int EmpID PK
            string FirstName
            string LastName
            float Salary
            int DeptID FK
        }
        DEPARTMENT {
            int DeptID PK
            string DeptName
        }
    Example: In a bank database, AccountID cannot be null or repeated.

B. Referential Integrity

  • Ensures foreign keys reference valid primary keys.
    erDiagram
        AUTHOR ||--o{ BOOK : "writes"
        AUTHOR {
            int AID PK
            string A_Name
        }
        BOOK {
            int BID PK
            string B_Name
            int AID FK
        }
    Example: If an author (AID=1) is deleted, all books by that author must be updated or flagged.

C. Domain Integrity

  • Ensures data fits predefined domains (e.g., Salary > 0).
    ALTER TABLE employees
    ADD CONSTRAINT chk_salary CHECK (Salary > 0);
    
08162431Not Null1 bitsUnique1 bitsPrimary Key1 bitsForeign Key1 bitsCheck1 bitsDefault1 bitsAuto Increment1 bitsReserved24 bits
SQL Data Type Constraints: Bit-level representation of common constraints.

D. User-Defined Constraints

  • Custom rules (e.g., Age > 18 for users).
    ALTER TABLE customers
    ADD CONSTRAINT chk_age CHECK (Age >= 18);
    

3. Security Mechanisms

A. Authentication

  • Verifies user identity (e.g., username/password, biometrics).
    sequenceDiagram
        participant User
        participant DB
        User->>DB: Login Request (Username, Password)
        DB-->>User: Verify Credentials
        alt Valid
            DB-->>User: Grant Access
        else Invalid
            DB-->>User: Reject Access

B. Authorization

  • Grants permissions (e.g., SELECT, INSERT, DELETE).
Role-Based Access Control (RBAC)Mandatory Access Control (MAC)Discretionary Access Control (DAC)Authorization Models

C. Encryption

  • Protects data in transit (TLS) and at rest (AES).
    flowchart TD
        A["Client"] -->|"HTTPS"| B["Server"]
        B -->|"AES-256"| C["Database"]

D. Auditing

  • Logs user actions (e.g., who modified a record?).
    CREATE TABLE audit_log (
        log_id INT PRIMARY KEY,
        user_id INT,
        action VARCHAR(50),
        timestamp DATETIME
    );
    

4. SQL Constraints

SQL enforces constraints via CREATE TABLE or ALTER TABLE:

Constraint Syntax Example
NOT NULL column_name DATA_TYPE NOT NULL Salary DECIMAL(10,2) NOT NULL
UNIQUE column_name UNIQUE Email VARCHAR(100) UNIQUE
PRIMARY KEY column_name PRIMARY KEY EmpID INT PRIMARY KEY
FOREIGN KEY column_name FOREIGN KEY DeptID INT FOREIGN KEY
CHECK CHECK (condition) CHECK (Salary > 0)

Worked Example:

CREATE TABLE employees (
    EmpID INT PRIMARY KEY,
    FirstName VARCHAR(50) NOT NULL,
    Salary DECIMAL(10,2) CHECK (Salary > 0),
    DeptID INT FOREIGN KEY REFERENCES departments(DeptID)
);

5. Database Security Models

Model Description Example
Centralized Single database server; all security managed centrally. Small businesses (e.g., local clinics)
Distributed Data split across multiple sites; security policies synchronized. Multinational banks (e.g., HSBC)
Client-Server Security enforced at server level (e.g., firewalls, encryption). Online banking (e.g., NMB)
Peer-to-Peer Each node enforces its own security (rare for databases). Decentralized apps (e.g., blockchain)

Example in Nepal:

  • NTC/Ncell use distributed security to protect customer data across multiple towers.
  • Daraz enforces centralized security for order processing.

6. Common Security Threats & Mitigations

Threat Impact Mitigation
SQL Injection Malicious SQL code execution. Use prepared statements.
Brute Force Guessing passwords. Enforce strong passwords + 2FA.
Data Leakage Unauthorized data exposure. Encrypt sensitive fields.
DoS Attacks Server overload. Rate limiting + load balancing.

Example:

  • Pathao uses SQL injection protection to prevent ride-hailing fraud.

7. Real-World Applications

In the Real World

  1. eSewa/Khalti:

    • Uses encryption (TLS) to secure transaction data.
    • Implements RBAC to restrict admin access to financial records.
  2. NEPSE (Nepal Stock Exchange):

    • Enforces referential integrity to prevent invalid stock trades.
    • Uses auditing logs to track suspicious activities.
  3. NTC/Ncell:

    • Applies domain integrity (e.g., phone numbers must be 10 digits).
    • Employs firewalls to block unauthorized network access.

Exam Tip

  • Focus on:
    • Definitions of integrity constraints (entity, referential, domain).
    • SQL constraints (NOT NULL, CHECK, FOREIGN KEY).
    • Security models (RBAC, encryption, auditing).
    • Real-world examples (e.g., how banks use constraints to prevent fraud).
  • Common mistakes:
    • Confusing primary key with unique key.
    • Forgetting to mention auditing in security discussions.
  • Practice:
    • Draw an ER diagram with constraints (e.g., foreign key relationships).
    • Write SQL queries with CHECK constraints.
    • Explain how SQL injection is prevented in real systems.

Based on the TU BITM syllabus for Database Management System (IT220), unit 6.

Discussion

Loading…