Resources | Subject Notes | Information Communication Technology ICT
A database is a structured collection of data that is organized for efficient storage, retrieval, and management. Databases are used in a wide variety of applications, from managing customer information to tracking inventory.
To ensure data integrity and relationships between different tables in a database, we use primary keys and foreign keys. These are fundamental concepts in relational database management.
A primary key is a column (or a set of columns) in a table that uniquely identifies each record (row) in that table. It must contain unique values and cannot contain null values.
Key characteristics of a primary key:
A foreign key is a column (or set of columns) in one table that refers to the primary key of another table. It establishes a link between the two tables. It ensures referential integrity, meaning that relationships between tables are maintained.
Key characteristics of a foreign key:
The primary key of one table and the foreign key in another table define a relationship between the two tables. This relationship can be one-to-one, one-to-many, or many-to-many.
Concept | Description |
---|---|
Primary Key | Uniquely identifies each record in a table. |
Foreign Key | References the primary key of another table, establishing a link. |
Relationship | The link between the primary and foreign keys defines the relationship between the tables. |
Consider two tables: Customers and Orders.
Customers Table:
Orders Table:
In this example, the CustomerID in the Orders table is a foreign key that links each order to a specific customer in the Customers table. This allows us to easily retrieve all orders placed by a particular customer.
Using primary and foreign keys is crucial for: