Primary key in database table

Category: |
It is accepted that tables in relational databases should have a primary key, marked with the PK symbol. Admittedly, it is not required, although its absence will generate many problems in the future, including performance issues.

The primary key ensures the uniqueness of data for a column and this means that you can't add more than one record with the same key. In addition, using the instruction "INSERT.... ON DUPLICATE KEY/ON CONFLICT.... UPDATE" can be used to force an update of selected row columns if the key already exists. This eliminates the need to use the SELECT statement to check if the PK is free.

Primary key in database table

A good practice affecting the performance of a database application is the use of AUTO_INCREMENT (MySQL) / IDENTITY (PostgreSQL, SQLServer) attributes in primary keys, which means autonumbering. The PK of each new record gets a new unique number. Please note that autonumbering can be used for only one table column and it must be an INTEGER column.

How to set primary key in SqlDBD?

You can add a primary key in two ways:

1. select the INTEGER type column and in the properties of this column enable AUTO_INCREMENT/IDENTITY - this action, in addition to enabling autonumbering, will create the primary key for the column (if it does not already exist).

primary key by autoincrement

2. select the table and in the properties window on the right, in the "Indexes and keys" section, click "Add index" - an index will be added, the type of which should be changed from INDEX to PRIMARY. Then, in the "Columns" section (index properties), click "Edit", select a column (or columns) from the list and confirm by clicking the icon next to it.

primary key by autoincrement part.1

primary key by autoincrement part.2

The second method, although it requires a bit more time, allows you to precisely set the columns of the primary key. However, in most cases the first method is sufficient.

This site uses cookies. Learn more about the purpose of their use and changes in cookie settings.
By using the website you agree to the use of cookies in accordance with current browser settings.