当我运行代码时,我在JavaDerby数据库中得到了这个外键错误

fdbelqdn  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(423)

当我运行代码时,我在javaderby数据库中得到了这个外键错误

CREATE TABLE PURCHASE (
    $PURCHASEID INT NOT NULL GENERATED ALWAYS AS IDENTITY,$
    $PURCHASEDATE DATE,$
    $PURCHASEQUANTITY INT,$
    $CHICPRICE DOUBLE,$
    $CUSTNIC VARCHAR(14) NOT NULL,$
    $PRIMARY KEY (PURCHASEID)$
       $ FOREIGN KEY (CUSTNIC) REFERENCES CUSTOMER(CUSTNIC)$
);

[异常,错误代码30000,sqlstate 42x01]语法错误:在第8行第9列遇到“foreign”。第8行第9列

ckx4rj1h

ckx4rj1h1#

试试这个

CREATE TABLE PURCHASE (
    $PURCHASEID INT NOT NULL GENERATED ALWAYS AS IDENTITY,$
    $PURCHASEDATE DATE,$
    $PURCHASEQUANTITY INT,$
    $CHICPRICE DOUBLE,$
    $CUSTNIC VARCHAR(14) NOT NULL,$
    $PRIMARY KEY (PURCHASEID)$
       $FOREIGN KEY (CUSTNIC) REFERENCES CUSTOMER(CUSTNIC)$
);
8aqjt8rx

8aqjt8rx2#

尝试创建表,然后使用添加外键约束

alter table PURCHASE add FOREIGN KEY (CUSTNIC) 
REFERENCES CUSTOMER (CUSTNIC)

似乎有一个“,”失踪前外国

0yycz8jy

0yycz8jy3#

如果在主键后加逗号,应该可以

CREATE TABLE PURCHASE (
    $PURCHASEID INT NOT NULL GENERATED ALWAYS AS IDENTITY,$
    $PURCHASEDATE DATE,$
    $PURCHASEQUANTITY INT,$
    $CHICPRICE DOUBLE,$
    $CUSTNIC VARCHAR(14) NOT NULL,$
    $PRIMARY KEY (PURCHASEID),$
       $ FOREIGN KEY (CUSTNIC) REFERENCES CUSTOMER(CUSTNIC)$
);

相关问题