Primary key
This article explains what a primary key is and how it is used in NocoDB.
A primary key is a special database table field designated to uniquely identify each table record. As it uniquely identifies an individual record of a table, it is used internally by NocoDB for all operations associated with a record
Primary Key in NocoDB
Primary Key that gets defined / used in NocoDB depends on how underlying table was created. Summary is captured below
-
From UI, Create new table / Import from Excel / Import from CSV
- An
ID
[datatype: Integer] system field created by default during table creation is used as primary key - Additional system fields
created-at
,updated-at
are inserted by default
- An
-
Connect to existing external database
- Existing
primary key
field defined for a table is retained as is; NocoDB doesn't insert a new ID field - Additional system fields
created-at
,updated-at
are not inserted by default
- Existing
-
Import from Airtable
- Airtable record ID is marked as primary key for imported records, and is mapped to field
ncRecordId
[datatype: varchar] - If a new record is inserted after migration & if ncRecordId field was omitted during record insertion - auto generated string will be inserted by NocoDB
- Computed hash value for the entire record is stored in system field
ncRecordHash
- Additional system fields
created-at
,updated-at
are not inserted by default
- Airtable record ID is marked as primary key for imported records, and is mapped to field
-
Create new table using SDK / API
- No default primary key field is introduced by NocoDB. It has to be explicitly specified during table creation (using attribute
pk: true
)
- No default primary key field is introduced by NocoDB. It has to be explicitly specified during table creation (using attribute
FAQ
What if Primary Key was missing?
In NocoDB, it's possible for a table to have no primary key—this can happen if the table is created externally or through the SDK/API without specifying one. While new records can still be added to such tables, updating or deleting them won’t be possible since there's no unique identifier to reference each record.
Can I change the Primary Key to another field within tables?
You can't update Primary Key from NocoDB UI. You can reconfigure it at database level directly & trigger meta sync
explicitly.