postgresql:尝试创建表时,关系“table\u name”不存在

iyr7buue  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(514)

我正在通过pgadmin4运行此代码:

CREATE TABLE product(
    code integer, 
    pname varchar(30),
    descr varchar(50),
    utype varchar(30),
    uprice float, 
    manu varchar(30),
    sid integer,
    PRIMARY KEY (code, sid),
    FOREIGN KEY (sid) REFERENCES supplier(sid)
);

CREATE TABLE branch(
    bid integer,
    bname varchar(30),
    baddress varchar(50),
    PRIMARY KEY (bid)
);

CREATE TABLE stock(
    code integer,
    bid integer,
    units float,
    PRIMARY KEY (code, bid)
);

CREATE TABLE receipt(
    bid integer,
    rdate date,
    rtime time,
    ptype varchar(30),
    total float,
    PRIMARY KEY (bid, rdate, rtime)
);

CREATE TABLE purchase(
    bid integer,
    rdate date,
    rtime time,
    code integer,
    units float,
    PRIMARY KEY (bid, rdate, rtime, code),
    check (units > 0)
);

CREATE TABLE supplier(
    sid integer,
    sname varchar(30),
    address varchar(50),
    phone numeric (9,0),
    PRIMARY KEY (sid)
);

INSERT INTO product (code, pname, descr, utype, uprice, manu, sid) VALUES
        (987, 'Tomatoes',       'Vegetable',  'Kg',  5.99,  'manufacturer1', 111),
        (876, 'Cucumbers',      'Vegetable',  'Kg',  4.99,  'manufacturer1', 222),
        (765, 'Cornflakes',     'Cornflakes', 'Box', 15.9,  'manufacturer1', 222),

INSERT INTO branch (bid, bname, baddress) VALUES
        (987, 'tal aviv', 'road 1 tel aviv'),
        (878, 'Raanana',  'road 1 raanana'),
        (767, 'Holon',    'road 1 holon');

INSERT INTO stock (code, bid, units) VALUES
        (987, 989, 50),
        (987, 878, 75),
        (987, 767, 100),

INSERT INTO receipt (bid, rdate, rtime, ptype) VALUES
        (989, '2020-3-19', '10:00', 'Cash'),
        (989, '2020-7-16', '12:30', 'Credit'),
        (989, '2020-7-15', '15:35', 'Credit'),

INSERT INTO purchase (bid, rdate, rtime, code, units) VALUES
        (989, '2020-3-18', '10:00', 987, 5),
        (989, '2020-3-18', '10:00', 876, 3),
        (989, '2020-3-18', '10:00', 543, 4),

INSERT INTO supplier (sid, sname, address, phone) VALUES
        (111, 'supplier2', 'road2 tel aviv',  111111111),
        (222, 'supplier3', 'road3 jerusalem', 222222222),
        (333, 'supplier4', 'road2 eilat',     333333333);

我一直收到这样的消息:错误:关系“供应商”不存在sql状态:42p01
问题是product表(第一个表)中的外键,我知道这一点,因为我删除了它,它创建了所有带有值的表(我删除了一些值以便于编写)。
不确定是否是语法错误,我尝试了几种方法来解决它,但我不知道,有什么建议吗?提前谢谢。

euoag5mw

euoag5mw1#

你必须创造 supplier 前表 product 表组件 sid 从产品表引用到 sid 在供应商表中作为外键。
这是工作演示。

CREATE TABLE supplier(
    sid integer,
    sname varchar(30),
    address varchar(50),
    phone numeric (9,0),
    PRIMARY KEY (sid)
);

CREATE TABLE product(
    code integer, 
    pname varchar(30),
    descr varchar(50),
    utype varchar(30),
    uprice float, 
    manu varchar(30),
    sid integer,
    PRIMARY KEY (code, sid),
    FOREIGN KEY (sid) REFERENCES supplier(sid)
);
bq8i3lrv

bq8i3lrv2#

一个选项是稍后(在插入之前)添加约束。这样您就不必担心表创建的顺序。

ALTER TABLE product
ADD FOREIGN KEY (sid) REFERENCES supplier(sid);

相关问题