What is a Primary Key?
Keys are used to access tables, and come in two varieties: Primary Keys and Foreign Keys. A Primary Key uniquely identifies a record in a table, while a Foreign Key accesses data in some other related table via its Primary Key.
Define a Simple Primary Key
If a Primary Key consists of a single column, it is very easy to define.
- Right-click on the table in a diagram to display the context menu. Select the Attributes menu option.
- In the Attributes dialog, select the column that makes up the Primary Key.
- Select the Primary Key checkbox and click on the Save button.
A stereotyped operation is automatically created. It is this operation that defines the Primary Key for the table. To remove a Primary Key, simply delete this operation.
Define a Complex Primary Key
Often, a Primary Key consists of more than one column. For example, a column LastName might not be unique within a table, so a Primary Key is created from the LastName, FirstName and DateOfBirth columns. Perform the following steps to create a complex Primary Key:
- Follow the steps above to create a Simple Primary Key. It doesn't matter which column you choose.
- Right-click on the table in a diagram to open the context menu. Select the Operations menu option.
- Select the Primary Key operation (its name begins with PK_) and then click on the Column tab.
- To add a column to the Primary Key, click on the New button, select a column from the Column Name list box, and then click on the Save button.
- Click on the Hand buttons (up and down arrow) to change the order of columns in the Primary Key, if necessary.
(See also the SQL Server Non-Clustered Primary Keys topic).
Define a Primary Key Name Template
To define the name template for a Primary Key, follow the steps below:
- Select the Tools | Options | Source Code Engineering | Code Editors menu option. The DDL page of the Options dialog displays.
- Click on the DDL Name Templates button. The DDL Name Template dialog displays, showing the default name templates.
- Edit or replace the template in the Primary Key Name Template field.
If you want to display the Primary Key description as PK_tablename_columnname then change the Primary Key Name Template field to PK_%tablename%_%columnname%.
- Click on the Save button.