我还是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
我正在看专栏!我知道它存在!请帮助一个兄弟!
答案 0 :(得分:3)
客户表中不存在代码。这是州。
ALTER TABLE customer
ADD CONSTRAINT state FOREIGN KEY (state)
REFERENCES statename (code) >MATCH FULL;