A database is a systematically organised collection of data that can be stored, accessed, updated and managed efficiently. Unlike a simple file, a database is designed to handle large volumes of related data while ensuring consistency, integrity and security. Databases are used everywhere: banks maintain account records, hospitals store patient histories, schools keep student databases, and online shops manage product inventories. The software that manages a database is called a DBMS (Database Management System).
The relational model, proposed by Edgar F. Codd in 1970, organises data into tables of rows and columns connected through keys. A relational database is a collection of such related tables, and the language used to query it is SQL (Structured Query Language). Because nearly every modern application rests on a relational database, understanding its concepts is essential for a computer science student.
This chapter introduces the terminology of relational databases: tables, rows, columns, keys, and relationships. It explains the different types of databases and DBMSs, the advantages of using a DBMS over a file system, the constraints that keep data valid, and the concept of relational algebra as a foundation for queries. Later chapters build on these ideas to write actual SQL statements.
A database is a collection of interrelated data stored together with minimum redundancy. A DBMS is the software that defines, creates, maintains and controls access to the database. Popular DBMS examples include MySQL, Oracle, PostgreSQL, Microsoft SQL Server and SQLite. The DBMS provides an interface for users to query and manipulate data, and it enforces rules for data integrity and security.
The advantages of a DBMS over traditional file systems are numerous. Data redundancy is reduced because shared data is stored once. Data inconsistency is avoided because redundancy is the root cause of conflicting copies. The DBMS enforces integrity constraints, allows concurrent access by multiple users with proper locking, provides recovery from crashes through backup and rollback, and restricts access to authorised users for security. These properties make databases far more robust than ordinary files.
import sqlite3
connection = sqlite3.connect("school.db")
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS students (roll INTEGER, name TEXT, marks INTEGER)")
connection.commit()
connection.close()
The example uses Python's built-in SQLite library to create a real database with a student table, showing that database concepts map directly to working code.
A well-designed database has several important characteristics. It provides data independence, meaning the storage format can change without affecting application programs. It allows shared and concurrent access, so many users can work with the data simultaneously under control of the DBMS. It ensures data integrity by enforcing constraints, and it supports security and authorisation so that only permitted users can read or modify data. It also maintains data consistency, where each value agrees with the constraints and with related values in other tables.
Redundancy control is central to design. If the same address is stored in two tables, updating it in one place but not the other creates inconsistency. By normalising data into related tables and linking them with keys, the database eliminates such duplication.
The relational model views data as a collection of tables (also called relations). Each table has a name and consists of rows (records or tuples) and columns (fields or attributes). Every row is a unique record of the table, and every column represents a specific attribute of the records.
Consider a STUDENT table with columns ROLL, NAME and MARKS. Each row holds one student's record. The set of valid values for a column is its domain. For example, the domain of MARKS might be integers from 0 to 100. A column's degree refers to the number of attributes (columns) in the table, and cardinality refers to the number of rows (records).
| ROLL | NAME | MARKS |
|---|---|---|
| 1 | Aarav | 85 |
| 2 | Bhavna | 92 |
| 3 | Chirag | 78 |
Here the degree is 3 (three columns) and the cardinality is 3 (three rows).
Keys are the attributes or sets of attributes used to identify and relate records. The most important keys are:
import sqlite3
connection = sqlite3.connect("school.db")
cursor = connection.cursor()
cursor.execute("CREATE TABLE student (roll INTEGER PRIMARY KEY, name TEXT)")
cursor.execute("CREATE TABLE marks (roll INTEGER, subject TEXT, marks INTEGER, FOREIGN KEY (roll) REFERENCES student(roll))")
connection.commit()
connection.close()
In the example, roll is the primary key of the student table and a foreign key in the marks table, linking each mark record to its student.
Relational databases connect tables through relationships defined by foreign keys. Three types of relationships exist:
The foreign key in the "many" side stores the primary key of the "one" side, forming the link that makes queries across tables possible.
Constraints are rules enforced by the DBMS to keep data valid. NOT NULL ensures a column always has a value. UNIQUE ensures no two rows share the same value in a column. PRIMARY KEY combines NOT NULL and UNIQUE for the identifying column. CHECK restricts values to a valid range or list. DEFAULT supplies a value when none is given. FOREIGN KEY enforces referential integrity, ensuring that a referenced record exists in the parent table.
CREATE TABLE student (
roll INTEGER PRIMARY KEY,
name TEXT NOT NULL,
marks INTEGER CHECK (marks BETWEEN 0 AND 100),
grade TEXT DEFAULT 'F'
);
Relational algebra is a theoretical language of operations on relations that forms the mathematical foundation of SQL. The core operations are:
Every SQL query can be expressed in relational algebra. For example, "list the names of students with marks above 80" is a select followed by a project. Understanding these operations clarifies how the database engine executes queries.
Besides relational databases, other database models exist. Hierarchical databases organise data as a tree, network databases use graphs, object-oriented databases store objects, and NoSQL databases handle unstructured and massive data with flexible schemas. Relational databases remain the most widely used for business applications because of their strong integrity guarantees and standardised SQL interface.
| Term | Meaning |
|---|---|
| Table / Relation | Collection of related records |
| Row / Tuple | A single record |
| Column / Attribute | A single field of data |
| Degree | Number of attributes |
| Cardinality | Number of tuples |
| Domain | Set of valid values for a column |
| Key | Purpose |
|---|---|
| Primary Key | Uniquely identifies each row; not null, no duplicates |
| Candidate Key | A key eligible to be primary |
| Alternate Key | Candidate key not chosen as primary |
| Foreign Key | References primary key of another table |
| Composite Key | Primary key made of multiple columns |
Databases provide the systematic foundation for storing and managing the structured data introduced in the previous chapter. The relational model organises data into tables, rows and columns, connected by keys that guarantee uniqueness and create relationships. A DBMS layers over the raw data to enforce integrity, security and concurrency, and constraints such as primary keys and check rules keep records valid. Relational algebra explains formally how queries select, project and join data, preparing the ground for SQL. With these concepts in place, the next chapter introduces Structured Query Language, the practical tool for creating, querying and modifying relational databases.