4

我还是 postgreSQL 的新手。我创建了两个表,我想返回并为其添加主键和外键约束,无论我做什么,我似乎都无法添加外键。这是我所拥有的:

两张表:

test=# \d statename
      Table "public.statename"
 Column |     Type      | Modifiers
--------+---------------+-----------
 code   | character(2)  | not null
 name   | character(30) |
Indexes:
    "statename_pkey" PRIMARY KEY, btree (code)

test=# \d customer
          Table "public.customer"   
    Column   |     Type      | Modifiers
-------------+---------------+-----------
 customer_id | integer       |
 name        | character(30) |
 telephone   | character(20) |
 city        | character(25) |
 street      | character(40) |
 state       | character(2)  |
 zipcode     | character(10) |
 country     | character(20) |

这是我正在运行的命令:

test=# ALTER TABLE customer ADD CONSTRAINT
state FOREIGN KEY (code) REFERENCES
statename (code) >MATCH FULL;

这是我得到的错误:

ERROR:  column "code" referenced in foreign key constraint does not exist

我在看专栏!我知道它存在!请大哥帮帮忙!

4

1 回答 1

3

您的客户表中不存在代码。是状态。

ALTER TABLE customer 
ADD CONSTRAINT state FOREIGN KEY (state) 
REFERENCES statename (code) >MATCH FULL;
于 2013-06-07T15:21:07.740 回答