【问题标题】:Foreign Key Constraint does not exist ERROR外键约束不存在 ERROR
【发布时间】:2020-01-11 17:28:52
【问题描述】:

我的 psql 数据库中不断出现此错误;

bikefacility=# ERROR:  syntax error at or near "c"
bikefacility-# LINE 1: c
bikefacility-#         ^
bikefacility-# bikefacility=# ALTER TABLE bicycle ADD CONSTRAINT fk_maintenance_contact_person FOREIGN KEY (maintenance_contact_person) REFERENCES maintenance(maintenance_contact_person);
ERROR:  syntax error at or near "ERROR"
LINE 1: ERROR:  syntax error at or near "c"
        ^
bikefacility=# ERROR:  column "maintenance_contact_person" referenced in foreign key constraint does not exist
bikefacility-# bikefacility=# ALTER TABLE bicycle ADD CONSTRAINT fk_rental_period FOREIGN KEY (rental_period) REFERENCES rental(rental_period);
ERROR:  syntax error at or near "ERROR"
LINE 1: ERROR:  column "maintenance_contact_person" referenced in fo...
        ^
bikefacility=# ERROR:  column "rental_period" referenced in foreign key constraint does not exist
bikefacility-# bikefacility=# ALTER TABLE bicycle ADD CONSTRAINT fk_terminal_id FOREIGN KEY (terminal_id) REFERENCES terminal(terminal_id);
ERROR:  syntax error at or near "ERROR"
LINE 1: ERROR:  column "rental_period" referenced in foreign key con...
        ^
bikefacility=# ERROR:  column "terminal_id" referenced in foreign key constraint does not exist

这是我的代码。我在代码中创建了与您在此处看到的相同样式的外键。

CREATE TABLE member (
  member_id INTEGER PRIMARY KEY,
  member_fname VARCHAR(15) NOT NULL,
  member_lname VARCHAR(15) NOT NULL,
  member_status VARCHAR(15) NOT NULL,
  member_address VARCHAR(10) NOT NULL,
  member_email VARCHAR(30) NOT NULL
);

CREATE TABLE bicycle (
  bicycle_id INTEGER PRIMARY KEY,
  bicycle_brand VARCHAR(25) NOT NULL,
  bicycle_model VARCHAR(25) NOT NULL,
  bicycle_colour VARCHAR(15) NOT NULL,
  bicycle_type VARCHAR(20) NOT NULL,
  bicycle_size VARCHAR(10) NOT NULL,
  bicycle_availability VARCHAR(20) NOT NULL

);

ALTER TABLE bicycle ADD CONSTRAINT fk_bicycle_pickup_date FOREIGN KEY (bicycle_pickup_date) REFERENCES rental(bicycle_pickup_date) >MATCH FULL;
ALTER TABLE bicycle ADD CONSTRAINT fk_maintenance_contact_person FOREIGN KEY (maintenance_contact_person) REFERENCES maintenance(maintenance_contact_person);
ALTER TABLE bicycle ADD CONSTRAINT fk_terminal_id FOREIGN KEY (terminal_id) REFERENCES terminal(terminal_id);
ALTER TABLE bicycle ADD CONSTRAINT fk_rental_period FOREIGN KEY (rental_period) REFERENCES rental(rental_period);

CREATE TABLE sponsor (
  sponsor_id INTEGER PRIMARY KEY,
  sponsor_name VARCHAR(15) NOT NULL,
  sponsor_contact VARCHAR(30) NOT NULL,
  sponsor_period DATE NOT NULL,
  sponsor_address VARCHAR(50) NOT NULL,
  sponsor_fee DECIMAL (6, 2) NOT NULL
);

CREATE TABLE terminal (
  terminal_id INTEGER PRIMARY KEY,
  terminal_address VARCHAR(50) NOT NULL,
  terminal_minstorage VARCHAR(50) NOT NULL,
  terminal_maxstorage VARCHAR(50) NOT NULL
);

CREATE TABLE rental (
  rental_no INTEGER PRIMARY KEY,
  rental_period DATE NOT NULL,
  bicycle_pickup_date DATE NOT NULL
);



它说这些列不存在,但我知道它们存在,因为它们就在那里!有人可以帮帮我吗?提前致谢!

【问题讨论】:

  • SQL server 还是 PostgreSQL?

标签: sql-server database postgresql foreign-keys constraints


【解决方案1】:

postgresql 的外键语法不正确。比如ALTER TABLE bicycle ADD CONSTRAINT fk_terminal_id FOREIGN KEY (terminal_id) REFERENCES terminal(terminal_id);要求在bikes中有一个字段,命名为terminal_id,因为之前的terminal_id在你的查询中是指自行车表,应该引用终端表中的terminal_id。

这里是外键的简短教程。 https://www.postgresqltutorial.com/postgresql-foreign-key/

最好的问候,
比亚尼

【讨论】:

    【解决方案2】:

    您的架构非常混乱。

    • 没有表 maintenance 但您尝试引用它。
    • 必须先创建像rental 这样的外部表,然后才能对其进行 fk 引用。
    • 表中必须有指定字段,您从中进行引用。表bicycle. 中没有字段bicycle_pickup_datemaintenance_contact_personterminal_idrental_period
    • 您的 fk 引用通常听起来很奇怪。你真的希望每辆自行车只能租一次吗?也许您的意思是租赁指的是一辆自行车?

    另外,您的命名约定是相当多余的。无需在每个字段名称中重复表名,这只会在您的代码中添加不必要的混乱。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2014-10-24
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2016-09-27
      • 2014-08-24
      相关资源
      最近更新 更多