In a database, a relation refers to a table that stores data in a structured format. It is a fundamental concept in relational databases, which are based on the relational model introduced by E.F. Codd. A relation consists of rows (also called tuples or records) and columns (also called attributes or fields). Each row represents a unique record, and each column represents a specific attribute of that record.
EmployeeID, Name, Department, and Salary.EmployeeID could be the primary key in an employee table.Consider a simple database for a library. One of the relations (tables) might be the Books table:
| BookID (Primary Key) | Title | Author | Genre | PublishedYear |
|---|---|---|---|---|
| 1 | 1984 | George Orwell | Dystopian | 1949 |
| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 |
| 3 | The Great Gatsby | F. Scott Fitzgerald | Classic | 1925 |
In this example:
Books table.BookID, Title, Author, Genre, and PublishedYear.BookID is the primary key, ensuring each book has a unique identifier.Relational databases use Structured Query Language (SQL) to manage and manipulate data. SQL allows you to create, read, update, and delete data in these relations (tables). For example:
Books table, you would use:SELECT * FROM Books;
SELECT * FROM Books WHERE PublishedYear > 1950;
It's important to distinguish between a relation (a table) and a relationship (how tables are connected). Relationships in a relational database are established using foreign keys, which link one table to another. For example, a Borrowers table might have a BookID foreign key to link borrowers to the books they borrowed from the Books table.
Relational databases and relations are widely used in various industries for tasks like managing customer data, tracking inventory, and storing financial records. If you're building a relational database, consider using TencentDB for MySQL or TencentDB for PostgreSQL, which are reliable and scalable relational database services. These services provide robust tools for creating, managing, and querying relations (tables) efficiently.