外键约束不存在错误

问题描述 投票:0回答:2

我一直在我的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 database postgresql foreign-keys constraints
2个回答
1
投票
这里是外键的简短教程。 https://www.postgresqltutorial.com/postgresql-foreign-key/

最好的问候,Bjarni


0
投票

没有表maintenance,但您尝试对其进行引用。

    [f0引用之前必须先创建诸如rental之类的外部表。
  • 您必须在引用的表中具有命名字段。表bicycle_pickup_date中没有字段maintenance_contact_personterminal_idrental_periodbicycle.
  • 您的fk参考文献通常听起来很奇怪。您是否真的希望每辆自行车只能租一次?也许您是说租车是指一辆单车?
  • 而且,您的命名约定非常多余。无需在每个字段名称中重复表名,只需在代码中添加不必要的内容即可。
  • © www.soinside.com 2019 - 2024. All rights reserved.