Primary keys
To define or edit table keys, edit your table and navigate to tab Keys or change values directly in the right-side panel in section Keys.
![primary key](https://www.datensen.com/blog/wp-content/uploads/primary-key.png)
A primary key exists in every table. If you wish to add a new alternate key, click the button + Add Key.
![add key](https://www.datensen.com/blog/wp-content/uploads/add-key.png)
To add a new column either to a primary key or alternate key, expand the key details and click the + Add Column to Key button and then choose the column name from the dropdown.
![add a column to a key](https://www.datensen.com/blog/wp-content/uploads/add-column-to-key.png)
Example:
table customer contains two keys. A primary key and one alternate key named customer_ak_1. The alternate key contains two columns: name and surname.
![mariadb keys](https://www.datensen.com/img/mariadb/mariadb-keys.png)
Primary keys and foreign keys
Keys are used in relationships.
Whenever you create a new relationship between two tables, a primary key is used for the reference and a new foreign key column is added to a child table.
Example:
![simple foreign key migration](https://www.datensen.com/blog/wp-content/uploads/foreign-key-simple-mysql.png)
To use the previously created alternate key, edit the relation by double-clicking it and navigate to tab Key.
![primary key](https://www.datensen.com/img/mariadb/mariadb-pk.png)
Change the value in Key drop down to the alternate key name.
![alternate key](https://www.datensen.com/img/mariadb/mariadb-ak.png)
As a result, the customer_id column disappears from the child table and two new columns are added to the child table instead.
![alternate key used in MysQL ERD](https://www.datensen.com/blog/wp-content/uploads/alternate-key-mysql-erd-1024x335.png)
Primary foreign key
If you check the PK (primary key) checkbox that belongs to a foreign key column, the column becomes the primary key and its icon color changes to blue.
![primary foreign key](https://www.datensen.com/blog/wp-content/uploads/primary-foreign-key-mysql-1024x382.png)
#