2

how to modify an existing column and add a references to it ?

let's say I have this table create script and executed it to the server

create table person (
id bigserial primary key,
firstname varchar(255),
flastname varchar(255),
employeeid int
);

now I have a person table, but then later on I realized that I need to reference the employeeid from another table and I don't want to drop this existing person table as it has data now. how to add the REFERENCES employee(employeeid) to the employeeid column of the person table ?.

if only I didn't forget to add that references keyword,my create table should have been like this below

create table person (
id bigserial primary key,
firstname varchar(255),
flastname varchar(255),
employeeid int references employee(employeeid)
);

so how to modify the existing employeeid to have the references keyword to it since it already has data?

1 Answer 1

4

Use ALTER TABLE:

ALTER TABLE person
   ADD FOREIGN KEY (employeeid) REFERENCES employee(employeeid);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.