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?
Use ALTER TABLE
:
ALTER TABLE person
ADD FOREIGN KEY (employeeid) REFERENCES employee(employeeid);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.