Reputation: 23
Is it possible to make a primary key (that is gonna be used as a foreign key) not having auto-increment. Imagine a actor typing in his/her id from a different source. I want to take this random int value and store it as primary
Upvotes: 0
Views: 1774
Reputation: 1116
Yes.
You actually have to specify AUTO INCREMENT
or data type SERIAL
for it to automatically increment values. If your PRIMARY KEY
that will be referenced by this FOREIGN KEY
is data type SERIAL
then you will need to make the FOREIGN KEY
's data type BIGINT UNSIGNED
. SERIAL
is effectively an alias for setting the column to BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
. Setting the the FOREIGN KEY
to BIGINT UNSIGNED
would give it the same integer range as a serial.
Remember, a PRIMARY KEY
must be unique. So if this table is meant to have multiple records that will reference a single record in the other table, you may be better off making a PRIMARY KEY
that is type SERIAL
and then have another column that is your FOREIGN KEY
that is type BIGINT UNSIGNED
. This would allow you to have multiple records with the same value for the FOREIGN KEY
column but you will still have a valid PRIMARY KEY
Upvotes: 2