即使通过外键应该有一个值,也会出现 Null 错误

Getting Null error even tho there should be a value via the foreign key

我在尝试将我的数据输入书籍时不断收到错误消息 table

error(Msg 515, Level 16, State 2, Line 32 Cannot insert the value NULL into column 'booksPubID', table 'Librarys.dbo.books'; column does not allow nulls. INSERT fails. The statement has been terminated.)

我不明白为什么,我已经对其他 table 执行了无数次相同的程序,而且效果很好。

create database Librarys

use Librarys

create table publishers (
    pubID int primary key not null identity (200,1),
    pubName varchar(100) not null,
    pubAddress varchar(200) not null,
    pubPhone varchar(20) not null,
);
insert into publishers
    (pubName, pubAddress, pubPhone)
    values
    ('World Class Books','9152 Longfellow Court Grand Island, NE 68801','(202)-555-0199'),
    ('Andy And Ally','19 Indian Summer Street Hempstead, NY 11550','(426)-535-4861'),
    ('Penguin Random House','7506 Fifth Court Holyoke, MA 01040','(202)-555-0148'),
    ('Hachette Livre ','58 Corona Ave. Kaukauna, WI 54130','(339)237-1422'),
    ('HarperCollins','9292 E. Cardinal Street West Lafayette, IN 47906','(361)474-5931'),
    ('Pan Macmillan','7002 Hillcrest Street Vernon Rockville, CT 06066','(713)515-6501'),
    ('Bloomsbury','9332 Ohio Ave. Copperas Cove, TX 76522','(219)738-8246'),
    ('Simon & Schuste','8464 Jockey Hollow Street Goldsboro, NC 27530','(610)921-2491'),
    ('Scholastic','47 West Heritage St. Henderson, KY 42420','(609)354-2678')
;

create table books (
    bookID int primary key not null identity (1,1),
    booksPubID int not null constraint fkPubID foreign key references publishers(pubID) on update cascade on delete cascade,
    bookTitle varchar(50) not null
);

insert into books 
    (bookTitle)
    values 
    ('The Lost Tribe'),
    ('Misery'),
    ('The Silmarillion'),
    ('The Hobbit'),
    ('The Fellowship of the Ring'),
    ('Two Towers'),
    ('Return of the King'),
    ('One Fish Two Fish Red Fish Blue Fish'),
    ('The Cat in the Hat'),
    ('Eragon'),
    ('The Megicians Nephew'),
    ('The Name Of The WInd'),
    ('A Game of Thrones'),
    ('Mistborn'),
    ('2001 A Space Odyssey'),
    ('The FOrever War'),
    ('Hyperion'),
    ('Beyond the High Mist'),
    ('To Kill a Mockingbird'),
    ('The Giver'),
    ('Gathering Blue'),
    ('Messenger'),
    ('Son'),
    ('The Great Gatsby'),
    ('Pride and Prejudice')
;

错误很明显。在下面的 table 中,booksPubIDNOT NULL,特别是因为 publishers table 的 FK constraint。您通过 not null 声明一本书没有出版商就不可能存在。删除 not null 部分以允许出版商 table.

中没有出版商的图书存在
create table books (
    bookID int primary key not null identity (1,1),
    booksPubID int not null constraint fkPubID foreign key references publishers(pubID) on update cascade on delete cascade,
    bookTitle varchar(50) not null
);

这是你如何修复它... ONLINE DEMO

drop table books;
drop table publishers;
create table publishers (
    pubID int primary key not null identity (200,1),
    pubName varchar(100) not null,
    pubAddress varchar(200) not null,
    pubPhone varchar(20) not null,
);

create table books (
    bookID int primary key not null identity (1,1),
    booksPubID int null constraint fkPubID foreign key references publishers(pubID) on update cascade on delete cascade,
    bookTitle varchar(50) not null
);


insert into publishers
    (pubName, pubAddress, pubPhone)
    values
    ('World Class Books','9152 Longfellow Court Grand Island, NE 68801','(202)-555-0199'),
    ('Andy And Ally','19 Indian Summer Street Hempstead, NY 11550','(426)-535-4861'),
    ('Penguin Random House','7506 Fifth Court Holyoke, MA 01040','(202)-555-0148'),
    ('Hachette Livre ','58 Corona Ave. Kaukauna, WI 54130','(339)237-1422'),
    ('HarperCollins','9292 E. Cardinal Street West Lafayette, IN 47906','(361)474-5931'),
    ('Pan Macmillan','7002 Hillcrest Street Vernon Rockville, CT 06066','(713)515-6501'),
    ('Bloomsbury','9332 Ohio Ave. Copperas Cove, TX 76522','(219)738-8246'),
    ('Simon & Schuste','8464 Jockey Hollow Street Goldsboro, NC 27530','(610)921-2491'),
    ('Scholastic','47 West Heritage St. Henderson, KY 42420','(609)354-2678')
;

insert into books 
    (bookTitle)
    values 
    ('The Lost Tribe'),
    ('Misery'),
    ('The Silmarillion'),
    ('The Hobbit'),
    ('The Fellowship of the Ring'),
    ('Two Towers'),
    ('Return of the King'),
    ('One Fish Two Fish Red Fish Blue Fish'),
    ('The Cat in the Hat'),
    ('Eragon'),
    ('The Megicians Nephew'),
    ('The Name Of The WInd'),
    ('A Game of Thrones'),
    ('Mistborn'),
    ('2001 A Space Odyssey'),
    ('The FOrever War'),
    ('Hyperion'),
    ('Beyond the High Mist'),
    ('To Kill a Mockingbird'),
    ('The Giver'),
    ('Gathering Blue'),
    ('Messenger'),
    ('Son'),
    ('The Great Gatsby'),
    ('Pride and Prejudice')
;

select * from books
select * from publishers