1

尝试将数据输入到 books 表时,我不断收到错误消息

错误(消息 515,级别 16,状态 2,第 32 行无法将值 NULL 插入列 'booksPubID',表 'Librarys.dbo.books';列不允许空值。插入失败。语句已终止。)

我不明白为什么,我已经无数次用其他表做了同样的过程,它工作正常。

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')
;
4

1 回答 1

1

错误很明显。在下表中,booksPubID是,特别是由于该表NOT NULL的 FK约束publishers。你是说一本书没有出版商就不可能存在not null。删除该not null部分以允许在出版商表中没有出版商的图书存在。

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
);

这是你如何解决它... 在线演示

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
于 2017-09-26T18:30:30.467 回答