Is it possible to have more than one Primary key in a table?

Asked

Viewed 6,709 times

5

I have a gym and I’m wearing one Pk for all gym users to have their own (ID).

  • It is possible to use other PK in the same table?
  • For example: to prevent Número da Matricula or even the CPF/RG if repeated, it is possible to add a PK for the same?

3 answers

5

Can’t have more than one PK in the same table. Use Constraints (Constraints), in your problem use the constraint UNIQUE, which will ensure that all values in your column are different.

UNIQUE

  • The restriction UNIQUE uniquely identifies each record in a table of a database.
  • The constraints UNIQUE and PRIMARY KEY ensure unity in a column or set of columns.
  • A Constraint PRIMARY KEY automatically has a restriction UNIQUE definite.
  • You can have several constraints UNIQUE in a table, but only one Primary key by table.
  • 1

    Thanks, I get it now!

  • 1

    @Guilhermemoura good that can help :) Great question. Welcome to Stack Overflow, recommend reading on tour to understand a little more how things work around here.

3

Is it possible to use other PK in the same table? - No, what you can use is the PK of another table like FK (Foreign Key) from another table. Another possible way would also be to use a primary key composed of 2 fields. Example: Tabitem PK - idItem

Tabtype PK - Idtipo

Tabtipoitem PK - Iditem PK - Idtipo

To prevent the Registration Number or even the CPF/RG from repeating, it is possible to add a PK to them?

You can use the field as Unique that this way the values will not repeat themselves.

2

It is not possible to have more than one primary key per table, it is the column or set of columns that identify a row.

To prevent repeated values from being inserted in certain columns, it makes it a unique key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.