Primary Key

primary_key

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.

  1. Right-click on the table in a diagram to display the context menu. Select the Attributes menu option.
  2. In the Attributes dialog, select the column that makes up the Primary Key.
  3. 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:

  1. Follow the steps above to create a Simple Primary Key. It doesn't matter which column you choose.
  2. Right-click on the table in a diagram to open the context menu. Select the Operations menu option.
  3. Select the Primary Key operation (its name begins with PK_) and then click on the Column tab.
  4. 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.
  5. 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:

  1. Select the Tools | Options | Source Code Engineering | Code Editors menu option. The Code Editors page of the Options dialog displays.
     
    CodeEditorOption
     
  2. Click on the DDL Name Templates button. The DDL Name Template dialog displays, showing the default name templates.
     
    NameTemplate
     
  3. Edit or replace the template in the Primary Key Name Template field.

Note:

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%.

  1. Click on the Save button.