Postgres中的ALTER表查询

时间:2016-03-16 11:30:40

标签: sql postgresql ddl

我正在尝试将表中一列的数据类型从biginteger更改为varchar。

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_id_refs_airwaybill_number_d438187b”无法实施

     

详细信息:键列“awbstock_id”和“airwaybill_number”属于   不兼容的类型:bigint和字符变化。

1 个答案:

答案 0 :(得分:2)

你应该:

  1. 删除第一个表上的主键约束
  2. $Machines = "xxx", "yyyy", "zzz" Get-WmiObject Win32_LogicalDisk -ComputerName $Machines -Filter "DeviceID='C:'" | Select-Object pscomputername, @{Name="Size GB";Expression={ "{0:N2}" -f ($_.Size / 1GB)}}, @{Name="Free GB";Expression={ "{0:N2}" -f ($_.FreeSpace / 1GB) }}

    1. 在第二个表上删除外键约束
    2. ALTER TABLE product_awbstock DROP CONSTRAINT product_awbstock_pkey;

      1. 更改两个表上的列数据类型
      2. ALTER TABLE ??? DROP CONSTRAINT awbstock_id_refs_airwaybill_number_d438187b;

        ALTER TABLE product_awbstock ALTER COLUMN airwaybill_number TYPE varchar(15);

        1. 重新创建以前删除的约束
        2. ALTER TABLE ??? ALTER COLUMN airwaybill_id TYPE varchar(15);

          ALTER TABLE product_awbstock ADD CONSTRAINT product_awbstock_pkey PRIMARY KEY (airwaybill_number);