我在尝试将数据输入图书表时不断收到错误消息
错误(消息515,级别16,状态2,行32无法插入值NULL 列'booksPubID',表'Librarys.dbo.books';专栏没有 允许空值。 INSERT失败。声明已经终止。)
我不明白为什么,我用其他表无数次地完成了同样的程序,并且工作正常。
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')
;
答案 0 :(得分:1)
错误很清楚。在下表中,SELECT name, count(*) as match_count FROM names WHERE (name = 'run' AND 'date' = '12/03/2015') OR (name = 'dance' AND 'date' = '10/01/2015') OR (name = 'dance' AND 'read' = '26/05/2015') GROUP BY name HAVING match_count = 3
为booksPubID
,特别是因为NOT NULL
表的FK 约束。你说的是,如果没有出版商,书籍就不能存在publishers
。删除not null
部分以允许在发布者表格中没有发布者的情况下存在图书。
not null
以下是解决问题的方法...... ONLINE DEMO
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
);