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

4

I have a gym and I'm using Pk so all gym members have their own ID.

  • Can I use other PK in the same table?
  • For example: to prevent Número da Matricula or even CPF/RG are repeated, can you add PK to them?
asked by anonymous 20.06.2017 / 20:00

3 answers

5

It is not possible to have more than one PK in the same table. Use Constraints, in your problem use the UNIQUE restriction, which will ensure that all values in your column be different.

UNIQUE

  • Constraint UNIQUE uniquely identifies each record in a table of a database.
  • Constraints UNIQUE and PRIMARY KEY guarantee uniqueness in a column or set of columns.
  • A PRIMARY KEY constraint automatically has a constraint UNIQUE set.
  • You can have multiple constraints UNIQUE in a table, but only one Primary Key per table .
20.06.2017 / 20:05
3

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

TabTipo PK - Type

TabTipoItem PK - IdItem PK - Type

To prevent the Registration Number or even the CPF / RG from being repeated, can you add a PK to them?

You can use the field as Unique so that the values will not be repeated.

    
20.06.2017 / 20:17
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 into certain columns, make it a unique key .

    
20.06.2017 / 20:09