我收到一个错误“外键定义中的未知列“ CustID”” 和“没有这样的表:serviceticket”
有人可以帮助我吗?
//*[@id="lyra-wrapper"]/div/div[3]/section/div[2]/section[2]/section/div/button/div[2]
答案 0 :(得分:0)
下面的代码将起作用。
这具有三个更改,而不是使用FOREIGN KEY (CustID) REFERENCES customer (CustID)
,这需要定义 serviceticket 表中的 CustID 列; CustID REFERENCES customer (CustID)
,这将定义CustID列并设置外键约束。
第二个更改将 098636 替换为 170362 ,这将导致外键约束冲突,该值是 CustID 中存在的值客户表的列。
例如:-
这有效(在正确定义表之后):-
insert into serviceticket (TicketID, WatchID, CustID, DateRecieved, DateReturned, ExpectedDelivery, COMMENTS, DateReturned)
values (6745, 87463, '170362', 124319, 362836, 683743, 'Nothing to say', 583728)
> Affected rows: 1
> Time: 0.082s
(根据消息)这将失败
insert into serviceticket (TicketID, WatchID, CustID, DateRecieved, DateReturned, ExpectedDelivery, COMMENTS, DateReturned)
values (6745, 87463, 098636, 124319, 362836, 683743, 'Nothing to say', 583728)
> FOREIGN KEY constraint failed
> Time: 0s
第三个更改是交换删除表的顺序。
drop table if exists serviceticket; -- <<<<<<<<<< if not coding ON DELETE CASCADE the children have to be deleted first (so moved before dropping customer table)
drop table if exists customer;
PRAGMA foreign_keys = ON;
create table customer (CustID char(6) not null primary key UNIQUE,
CustNAME TEXT,
STREET TEXT,
CITY TEXT,
ZIP INT(5),
EMAIL VARCHAR,
PHONENUMBER INT(10)
);
create table serviceticket (TicketID char(4) not null primary key,
TicketNO char(5),
WatchID char(6),
DateRecieved char(6),
ExpectedDelivery char(6),
COMMENTS text,
DateReturned char(6),
CustID REFERENCES customer (CustID) -- <<<<<<<<<< defines the column and foreign key
);
insert into customer (CustID, CustNAME, STREET, CITY, ZIP, PHONENUMBER, EMAIL)
values (170362, 'James', 'Vvegen', 'New York', 12345, 8170710520, 'brt92@outlook.com');
insert into serviceticket (TicketID, WatchID, CustID, DateRecieved, DateReturned, ExpectedDelivery, COMMENTS, DateReturned)
values (6745, 87463,
'170362', -- <<<<<<<<<< MUST match a value in CustID column of the customer table (changed to work)
124319, 362836, 683743, 'Nothing to say', 583728);
您不妨考虑使用(或其变体):-
CustID REFERENCES customer (CustID) ON DELETE CASCADE ON UPDATE CASCADE
答案 1 :(得分:0)
您没有在定义外键之前定义CustID
:
create table serviceticket (TicketID char(4) not null primary key,
TicketNO char(5),
WatchID char(6),
DateRecieved char(6),
ExpectedDelivery char(6),
COMMENTS text,
DateReturned char(6),
CustID char(6),
FOREIGN KEY (CustID) REFERENCES customer(CustID)
);