简体   繁体   English

如何修改列数据类型并在 postgresql 中添加对它的引用?

[英]how to modify column data type and add references to it in postgresql?

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.现在我有一个 person 表,但后来我意识到我需要从另一个表中引用 employeeid,我不想删除这个现有的 person 表,因为它现在有数据。 how to add the REFERENCES employee(employeeid) to the employeeid column of the person table ?.如何将REFERENCES employee(employeeid)添加到人员表的employeeid ID 列中?。

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?那么如何修改现有的employeeid 使其具有references 关键字,因为它已经有数据?

Use ALTER TABLE :使用ALTER TABLE

ALTER TABLE person
   ADD FOREIGN KEY (employeeid) REFERENCES employee(employeeid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM