Primary keys
A primary key contains unique values and identifies each row in a table.
For some databases, the primary key cannot contain NULL values. A table can have only one primary key, and this primary key can consist of single or multiple columns. When a primary key consists of multiple columns, the data from these columns is used to determine whether a row is unique.
Primary keys ( ) can be found in the Database tool window. For the reference on other node and object icons, refer to the Data sources and their elements chapter of Database tool window topic. For the table column icons, refer to the Possible icon combinations for columns chapter.
![Primary keys in Database Primary keys in Database](https://resources.jetbrains.com/help/img/idea/2024.2/database_object_primary_key.png)
Create a primary key
In the Database tool window ( ) , expand the data source tree until the nodes of tables.
Right-click the table node and select
.In the Modify dialog that opens, enter the name of your primary key in the Name field.
Click the Add button (
) in the Columns pane of the primary key editor tab.
In the Column Name field, type or select the column that you want to make a primary key.
In the Preview pane, you can view and change the generated SQL code.
Click OK to add your primary key.
![Create a primary key Create a primary key](https://resources.jetbrains.com/help/img/idea/2024.2/db_create_primary_key.png)
Create a composite primary key
In the Database tool window ( ) , expand the data source tree until the nodes of tables.
Right-click the table node and select
.In the Modify dialog that opens, enter the name of your composite primary key in the Name field.
Add the columns that you want to make a primary key of:
Click the Add button (
) in the Columns pane of the primary key editor tab.
In the Column Name field, type or select the column that you want to add to the composite primary key.
In the Preview pane, you can view and change the generated SQL code.
Click OK to add your composite primary key.
![Create a composite primary key Create a composite primary key](https://resources.jetbrains.com/help/img/idea/2024.2/db_create_composite_primary_key.png)
Make a column a primary key
In the Database tool window ( ) , expand the data source tree until the node of a child table.
Right-click a child table and select Modify Table.
In the Modify dialog that opens, select the column that you want to make a primary key.
Click the click the vertical ellipsis icon (
) next to the column Name field and select Make Primary Key.
The new primary key will appear under the keys node of the tree. The primary key editing tab will appear next to the column editing one.
Click OK.