When we designed the database in Lesson One, we decided that the EmployeeID and CustomerID columns would be key columns. They are the columns that enforce the relationship constraints between the tables. When flights are entered, we want to make sure that the EmployeeID and CustomerID values exist in the Employee and Customer tables. We use R:BASE's built-in constraints to enforce the relationships between our tables. In addition to enforcing the relationships between tables, by defining constraints, we automatically create indexes on the columns to speed data retrieval. There can be multiple primary keys in one table as well as multiple foreign keys in a table.
The Employee table has a one-to-many relationship with the Flights table, using the column EmployeeID, as there can be only one unique employee in the Employee table, and there can be many flights entered in the Flights table by an employee. The EmployeeID column would be the primary key for the Employee table and EmployeeID would be the foreign key in the Flights table.
The Customer table also has a one-to-many relationship with the Flights table, using the column CustomerID, as there can be only one unique customer in the Customer table, and there can be many flights entered in the Flights table for a customer. The CustomerID column would be the primary key for the Customer table and CustomerID would be the foreign key in the Flights table.
First, we'll make the EmployeeID column in Employee a primary key. A primary key is used to identify the columns that uniquely identify a row in a table. A primary key must be a unique value. The difference between a primary key and a foreign key is that the primary key data must be unique to that table and the foreign key can occur more than once.
To define a primary key:
1. | In the "Database Explorer" window, click the "Tables" option from the Group Bar. |
2. | Highlight the Employee table. |
3. | Select "Design Table". |
4. | Click the "Keys/Indexes" option. |
R:BASE displays currently defined indexes and constraints for the selected table. The window is blank, as none have been defined.
5. | Click the "New Key" option. |
R:BASE displays a sorting menu. The columns in the Employee table are listed. Sometimes, tables have multiple column keys. Our primary key is a single column key.
6. | Select "EmployeeID". |
You must choose the sort order for the constraint index.
7. | Click the "Ascending" radio button to select ascending order and select "Add". |
8. | Click the OK button. |
The "New Key/Index" dialog box is displayed.
9. | Select "Primary Key". |
The dialog box automatically displays options applicable to a primary key. We won't edit the default messages.
10. | Click the "OK" button. |
The primary key you just defined is now listed as a key/index for the Employee table.
11. | Click the "Save" option. |
12. | After confirming that the changes were saved, select the "Close" option. |
Now, repeat this procedure to make the CustomerID column in the Customer table a primary key.
Then, repeat this procedure again to make the FlightID column in the Flight table a primary key.
Next, we'll define the foreign keys for the Flights table. When we defined the table relationships, we noted that we could have many rows for each employee and many rows for each customer in the Flights table. By defining these columns as foreign keys, we ensure that matching values exist in the Employee and Customer tables.
To define a foreign key:
1. | In the "Database Explorer" window, click the "Tables" option from the Group Bar. |
2. | Highlight the Flights table. |
3. | Select "Design Table". |
4. | Click the "Keys/Indexes" option. |
R:BASE displays currently defined indexes and constraints for the selected table.
5. | Click the New Key button. |
R:BASE displays a sorting menu listing the columns in the Flights table. Sometimes, tables have multiple column keys. Our foreign keys are single column keys to match the defined primary keys.
6. | Select "EmployeeID". |
7. | Select the "Ascending" radio button to select ascending order and select "Add". |
8. | Click the OK button. |
The "New Key/Index" dialog box is displayed.
9. | Select "Foreign Key". |
The dialog box automatically displays options applicable to a foreign key.
R:BASE requires you to select a "Referenced Table," the table with the corresponding primary key for this foreign key.
10. | Select the "Employee" table within the "Referenced Table" drop down menu. |
11. | Select the reference key number within the "Referenced Key" drop down menu. |
Normally, there is only one number to choose in the "Referenced Key:" drop down box, unless more than one primary key is defined for a table.
12. | Click the OK button. |
The foreign key you just defined is now listed as a key/index for the Flights table.
Now make the CustomerID column in the Flights table a foreign key. Repeat the steps above, and select "Customer" as the matching (referenced) primary key table.
13. | Click the "Save" option. |
14. | After confirming that the changes were saved, select the "Close" option. |