You can create a foreign key when you are creating or altering a table. Instructions on how to create a foreign key are provided below.
To create a foreign key relationship:
- Verify that your key columns in the referenced table and the dependent table have the same data types, sizes, and null status. You can examine these column attributes using the Catalog Browser dialog box.
- Display the Create Table or Alter Table dialog box for the dependent table. This is the table on which you are creating a foreign key.
- Click the Create button from the Relationships group box.
SQLWizard displays the Create Relationship dialog box.
- Type a name for your foreign key in the Foreign Key Name field.
- Type the name of a table in the Referenced Table field, or click the Browse button and choose a table from the Open Table dialog box. When you specify a referenced table, remember that it must have a primary key already defined.
- The Table Columns list displays the column names from the dependent table (the EMPLOYEE table in the example above). These are available for you to match to the Referenced Columns that are displayed in the other list.
- Match a column name from the Table Columns list to each of the Referenced Columns. There are two ways to do this:
- Highlight a column name and click the right arrow button.
- Double-click a column name.
SQLWizard moves the selected field from the dependent Table Columns list to the Foreign Key Cols list, next to the first available referenced column.
If you make any errors in assigning fields, highlight the name in the Foreign Key Cols list and move it back to the Table Columns list by clicking on the left arrow button.
- To change the ordering of the foreign key columns, click a foreign key column and use the up and down arrows to reposition it next to the correct column from the referenced table. The column only moves up or down if there is another matching referenced column (i.e., the same data type, length, and null permission) above or below it in the list.
- Click OK to accept the relationship definition.
SQLWizard displays the name of the foreign key in the Relationships group box on the Alter Table dialog box, preceded by a + sign to indicate a new relationship. The name of the referenced table appears in parentheses.
- Click OK from the Alter Table dialog box.
- Click OK to confirm altering the table.
SQLWizard creates the foreign key on the selected table.