由于尝试创建表的外键约束而导致 SQL 异常
我试图使用以下脚本创建一组表:
DROP TABLE ORDERS IF EXISTS;
DROP TABLE INVOICE IF EXISTS;
CREATE TABLE ORDERS(
ORDER_ID BIGINT NOT NULL PRIMARY KEY IDENTITY,
...
INVOICE_ID BIGINT,
...
);
CREATE TABLE INVOICE(
INVOICE_ID BIGINT NOT NULL PRIMARY KEY IDENTITY,
...
ORDER_ID BIGINT,
...
FOREIGN KEY(ORDER_ID) REFERENCES ORDERS(ORDER_ID)
);
ALTER TABLE ORDERS ADD FOREIGN KEY(INVOICE_ID) REFERENCES INVOICE(INVOICE_ID);
这会导致 sql 异常
java.sql.SQLException:由 FOREIGN KEY 约束引用: 语句中的 PUBLIC.INVOICE.SYS_FK_10079 [ DROP TABLE ORDERS IF 存在]
我不知道如何解决这个问题..我需要订单表中的invoiceId和INVOICE表中的OrderID作为外键..这就是我使用ALTER TABLE语句的原因..仍然,它似乎是错误的.. (顺便说一句,我正在使用 hsqldb)
我想知道如何正确执行此操作..欢迎任何帮助..
I was trying to create a set of tables using the following script:
DROP TABLE ORDERS IF EXISTS;
DROP TABLE INVOICE IF EXISTS;
CREATE TABLE ORDERS(
ORDER_ID BIGINT NOT NULL PRIMARY KEY IDENTITY,
...
INVOICE_ID BIGINT,
...
);
CREATE TABLE INVOICE(
INVOICE_ID BIGINT NOT NULL PRIMARY KEY IDENTITY,
...
ORDER_ID BIGINT,
...
FOREIGN KEY(ORDER_ID) REFERENCES ORDERS(ORDER_ID)
);
ALTER TABLE ORDERS ADD FOREIGN KEY(INVOICE_ID) REFERENCES INVOICE(INVOICE_ID);
This causes sql exception
java.sql.SQLException: is referenced by FOREIGN KEY constraint:
PUBLIC.INVOICE.SYS_FK_10079 in statement [ DROP TABLE ORDERS IF
EXISTS]
I couldn't figure out how to solve this ..I need invoiceId in ORDERS table and OrderID in INVOICE table as foreign keys..That is why I used the ALTER TABLE statement..Still ,it seems wrong..
(btw I am using hsqldb)
I would like to know how to do this properly..Any help most welcome..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先删除订单上的外键:
然后运行命令。
Drop the foreign key on orders first:
Then run your commands.