A relational database organizes data in tables. Figure 22.1 illustrates a sample Employees
table that might be used in a personnel system. The table stores the attributes of employees. Tables are composed of rows (also called records) and columns (also called fields) in which values are stored. This table consists of six rows (one per employee) and five columns (one per attribute). The attributes are the employee’s
ID,
name,
department,
salary and
location.
The ID
column of each row is the table’s primary key—a column (or group of columns) requiring a unique value that cannot be duplicated in other rows. This guarantees that each primary-key value can be used to identify one row. A primary key composed of two or more columns is known as a composite key—in this case, every combination of column values in the composite key must be unique. Good examples of primary-key columns in other apps are a book’s ISBN number in a book information system or a part number in an inventory system—values in each of these columns must be unique. We show an example of a composite primary key in Section 22.3. LINQ to Entities requires every table to have a primary key to support updating the data in tables. The rows in Fig. 22.1 are displayed in ascending order by primary key. But they could be listed in descending order or in no particular order at all.
Each column represents a different data attribute. Some column values may be duplicated between rows. For example, three different rows in the Employees
table’s Department
column contain the number 413, indicating that these employees work in the same department.
You can use LINQ to Entities to define queries that select subsets of the data from a table. For example, a program might select data from the Employees
table to create a query result that shows where each department is located, in ascending order by Department
number (Fig. 22.2).