Sql Postgres中的ALTER表查询
我正在尝试将表中一列的数据类型从biginger更改为varcharSql Postgres中的ALTER表查询,sql,postgresql,ddl,Sql,Postgresql,Ddl,我正在尝试将表中一列的数据类型从biginger更改为varchar myproject-# \d+ product_awbstock Table "public.product_awbstock" Column | Type | Modifiers | Storage | Description -------------------+----------------------
myproject-# \d+ product_awbstock
Table "public.product_awbstock"
Column | Type | Modifiers | Storage | Description
-------------------+--------------------------+-----------+---------+-------------
airwaybill_number | bigint | not null | plain |
used | boolean | not null | plain |
created | timestamp with time zone | not null | plain |
modified | timestamp with time zone | not null | plain |
Indexes:
"product_awbstock_pkey" PRIMARY KEY, btree (airwaybill_number)
"product_awbstock_used" btree (used)
我使用这个查询,也给出了错误
alter table product_awbstock ALTER COLUMN airwaybill_number TYPE varchar(15);
错误:外键约束
“awbstock\U id\U refs\U AIRBYBILL\U编号\U d438187b”无法实现
详细信息:“awbstock\u id”和“airbybill\u number”的关键列为
不兼容类型:bigint和字符变化
你应该:
ALTER TABLE product\u awbstock DROP CONSTRAINT product\u awbstock\u pkey代码>
在第二个表上删除外键约束
altertable???DROP CONSTRAINT awbstock\u id\u refs\u AIRBYBILL\u编号\u d438187b代码>
更改两个表上的列数据类型
ALTER TABLE product\u awbstock ALTER COLUMN airbybill\u number TYPE varchar(15)代码>
altertable???ALTER列airwaybill_id TYPE varchar(15)代码>
重新创建以前删除的约束
ALTER TABLE product\u awbstock ADD CONSTRAINT product\u awbstock\u pkey主键(airwaybill\u编号)代码>
altertable???添加约束awbstock\u id\u refs\u airbybill\u number\u d438187b外键(awbstock\u id)引用产品awbstock(airbybill\u number)代码>错误消息表明此列是从其他表引用的。不同类型的列之间不能有引用。应删除product_awbstock.airwaybill_number和?.awbstock_id之间的约束。您还必须将另一个表的列更改为varchar。但是当前的bigint数据类型有什么问题吗?@jarlh需要将该字段更新为字母数字。