SQL Server错误与外键冲突

时间:2018-05-26 18:01:10

标签: sql sql-server sql-server-2017

我这里有这个代码:

create database shop11
use shop11

create table mathang
(
       MatHangID  INT primary key not null,
       TenMatHang varchar(50),
       SoLuong    int not null,
       Price      int not null,
)

create table nhacungcap
(
       MatHangID  INT foreign key references mathang,
       TenNhaCungCap varchar(50) ,
       DiaChi        varchar(100),
       SoDienThoai   int ,
       CONSTRAINT pk_M_CC primary key (MatHangID)
)

create table khachhang
(
       KhachHangID   int not null primary key,
       TenKhachHang  varchar(50)  not null,
       [DiaChi]      varchar(100) not null,
       [SoDienThoai] varchar(50)  not null,
)

create table donhang
(
       DonHangID int references khachhang(KhachHangID),
       TenDonHang    varchar(50),
       SoLuong       int,
       CONSTRAINT pk_DHID primary key (DonHangID)
)

INSERT INTO khachhang 
VALUES ('1','TOMMY','VIETNAM','123456789'),('2','TONY','VIETNAM','987654321'),
       ('3','TOMY','VIETNAM','1234567891'),('4','JOHNNY','VIETNAM','356112789'),
       ('6','KENNY','VIETNAM','1223236789')

INSERT INTO donhang 
VALUES ('1','LAPTOP ACER',100), ('2','LAPTOP ASUS',10),
       ('3','LAPTOP MSI',5), ('4','ZENPHONE ASUS',10),
       ('5','NOTEBOOK HP',10), ('6','IPHONE',10),
       ('7','MACBOOK PRO',10)

这是我得到的错误。我怎么能修复它,以及外键的这个错误怎么样:(请帮助,谢谢

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在khachhang.KhachHangID上引用khachhang.KhachHangID

值 (' 5',' NOTEBOOK HP',10) (' 7',' MACBOOK PRO',10)

在khachhang牌桌上不匹配。