Computer Science (2210)
Topic 8 of 11Cambridge O Levels

Databases

Learn to create, manage, and query relational databases using SQL and primary keys.

A database is an organised, persistent collection of data stored electronically. Unlike simple flat-file databases (like a single spreadsheet), which are prone to data redundancy and inconsistency, a relational database organises data into one or more structured tables. This model is the foundation of most modern data management systems.


### Structure of a Relational Database


A relational database consists of tables, which are also known as relations. Each table is designed to hold information about a specific type of entity, such as 'Students' or 'Books'.


* Tables (Relations): A grid-like structure containing all the data for a single entity.

* Records (Rows/Tuples): Each row in a table represents a single instance of that entity. For example, one row in a 'Students' table would contain all the information about one specific student.

* Fields (Columns/Attributes): Each column in a table represents a specific piece of information about the entity. For instance, a 'Students' table might have fields for 'StudentID', 'FirstName', 'LastName', and 'DateOfBirth'.


Each field is assigned a specific data type to ensure data integrity. Common data types include:

* Text/String: For alphanumeric characters (e.g., names, addresses).

* Number (Integer/Real): For numerical data (e.g., age, price).

* Boolean: For data with only two possible values, such as True/False or Yes/No.

* Date/Time: For storing dates and times in a specific format.


### The Primary Key

To ensure every record in a table is unique and can be reliably identified, we use a primary key. A primary key is a field (or a combination of fields) that contains a unique value for each record. For example, in a 'Students' table, the 'StudentID' would be an excellent primary key because no two students can have the same ID. This uniqueness is crucial for accurately retrieving, updating, and linking data between tables.


### Database Management System (DBMS)

The software used to create, query, and manage databases is called a Database Management System (DBMS). A DBMS provides an interface for users and applications to interact with the database, handling tasks like data security, backups, and enforcing data integrity rules. Examples include MySQL, Microsoft SQL Server, and Oracle Database.


### Structured Query Language (SQL)

Structured Query Language (SQL) is the standard programming language used to communicate with a relational database. It allows us to perform various operations, collectively known as data manipulation. The most common SQL commands are:


  • SELECT: Used to retrieve data from a database. The `WHERE` clause is used to filter records based on specific criteria.
  • * Process: `SELECT field1, field2 FROM TableName WHERE condition;`

    * Example: To find the names of all students in Grade 10:

    `SELECT FirstName, LastName FROM Students WHERE Grade = 10;`

    * To retrieve all fields for a record, we use the wildcard `*`:

    `SELECT * FROM Students WHERE StudentID = 'S105';`


  • INSERT INTO: Used to add a new record to a table.
  • * Process: `INSERT INTO TableName (field1, field2) VALUES (value1, value2);`

    * Example: To add a new student:

    `INSERT INTO Students (StudentID, FirstName, LastName, Grade) VALUES ('S121', 'Ali', 'Khan', 9);`


  • UPDATE: Used to modify existing records in a table. The `WHERE` clause is essential to specify which record(s) to change.
  • * Process: `UPDATE TableName SET field1 = newValue1 WHERE condition;`

    * Example: To change Ali Khan's grade to 10:

    `UPDATE Students SET Grade = 10 WHERE StudentID = 'S121';`


  • DELETE: Used to remove records from a table. The `WHERE` clause is critical to avoid deleting the wrong data.
  • * Process: `DELETE FROM TableName WHERE condition;`

    * Example: To remove the student with ID 'S121':

    `DELETE FROM Students WHERE StudentID = 'S121';`


    ### Advanced Querying with SQL

    SQL's `WHERE` clause can be combined with operators for more complex searches:

    * Comparison Operators: `=`, `>`, `<`, `<>` (not equal).

    * Logical Operators: `AND` (both conditions must be true), `OR` (at least one condition must be true), `NOT` (reverses the result of a condition).

    * LIKE: Used for pattern matching in text fields (e.g., `WHERE FirstName LIKE 'A%'` finds all names starting with 'A').


    Data can also be sorted using the ORDER BY clause.

    * Process: `SELECT * FROM TableName ORDER BY fieldName ASC/DESC;`

    * Example: To list all students alphabetically by their last name:

    `SELECT * FROM Students ORDER BY LastName ASC;`

    Key Points to Remember

    • 1A relational database organizes data into tables, which consist of records (rows) and fields (columns).
    • 2A **primary key** is a field with a unique value for each record, used for unique identification.
    • 3A **Database Management System (DBMS)** is the software used to create and manage databases.
    • 4**Structured Query Language (SQL)** is the standard language for interacting with relational databases.
    • 5The `SELECT` statement is used to retrieve data, with `WHERE` filtering the results and `ORDER BY` sorting them.
    • 6The `INSERT INTO` statement adds new records to a table.
    • 7The `UPDATE` statement modifies existing records, specified by a `WHERE` clause.
    • 8The `DELETE` statement removes records from a table, specified by a `WHERE` clause.

    Pakistan Example

    Managing Citizen Records with NADRA

    The National Database and Registration Authority (NADRA) in Pakistan manages a massive database containing the records of millions of citizens. In this system, each citizen is assigned a unique 13-digit Computerized National Identity Card (CNIC) number. This CNIC number serves as the **primary key** for the main citizens' table, ensuring that every individual's record is unique and can be accessed without error. A hypothetical SQL query to find a specific citizen would be: `SELECT * FROM Citizens WHERE CNIC = '12345-6789012-3';`. Similarly, if a citizen moves to a new city, an `UPDATE` query could be used to change their address, demonstrating how a DBMS and SQL are essential for managing national-level data accurately and efficiently.

    Quick Revision Infographic

    Computer Science — Quick Revision

    Databases

    Key Concepts

    1A relational database organizes data into tables, which consist of records (rows) and fields (columns).
    2A **primary key** is a field with a unique value for each record, used for unique identification.
    3A **Database Management System (DBMS)** is the software used to create and manage databases.
    4**Structured Query Language (SQL)** is the standard language for interacting with relational databases.
    5The `SELECT` statement is used to retrieve data, with `WHERE` filtering the results and `ORDER BY` sorting them.
    6The `INSERT INTO` statement adds new records to a table.
    Pakistan Example

    Managing Citizen Records with NADRA

    The National Database and Registration Authority (NADRA) in Pakistan manages a massive database containing the records of millions of citizens. In this system, each citizen is assigned a unique 13-digit Computerized National Identity Card (CNIC) number. This CNIC number serves as the **primary key** for the main citizens' table, ensuring that every individual's record is unique and can be accessed without error. A hypothetical SQL query to find a specific citizen would be: `SELECT * FROM Citizens WHERE CNIC = '12345-6789012-3';`. Similarly, if a citizen moves to a new city, an `UPDATE` query could be used to change their address, demonstrating how a DBMS and SQL are essential for managing national-level data accurately and efficiently.

    SeekhoAsaan.com — Free RevisionDatabases Infographic

    Test Your Knowledge!

    5 questions to test your understanding.

    Start Quiz