0

我得到了这个例子,在这里得到了一些帮助:

http://sqlfiddle.com/#!2/92e87/1

但是,如果我想尝试为表的每个子项插入信息,我似乎无法让它工作(使用此代码):

CREATE TABLE country (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(255) NOT NULL
)
;

CREATE TABLE location (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    coordinate varchar(255) NOT NULL,
    country_id integer NOT NULL REFERENCES country (id)
)
;

CREATE TABLE item (
    id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title varchar(60) NOT NULL,
    description varchar(900) NOT NULL,
    date datetime NOT NULL,
    source varchar(255) NOT NULL,
    link varchar(255) NOT NULL,
    location_id integer NOT NULL REFERENCES location (id)
)
;

Insert Into item (title) values ('Title');
Insert Into item (description) values ('Description');
Insert Into item (date) values ('1995-12-31T23:59:59Z');
Insert Into item (source) values ('Source');
Insert Into item (link) values ('Link');
Insert Into item (location_id) values ('1');

这是这样做的正确方法吗?其次,它告诉我“描述”没有默认值,但如果我总是将信息放入其中,它是否需要一个默认值?

感谢您提供的任何帮助

4

2 回答 2

2

对于每次插入,您最终都会在项目表中获得新的数据行。我不认为这是你想要的。相反,请执行以下操作:

INSERT INTO item VALUES (NULL, [title], [description], [date], [source], [link], [location_id]);

将 [ 和 ] 中的项目替换为适当的值。

您被告知 Description 没有默认值,因为在未为该列指定值的 INSERT 语句中,没有数据库可以为该字段填写的默认值。

于 2012-12-03T10:46:24.320 回答
2
Insert Into item (title, description, date, source, link, location_id) 
values ('Title', 'Description','1995-12-31T23:59:59Z','source','Link',1);

每个插入都充当插入一条新记录。因此,您需要将所有数据组合到一个插入语句中,如上所述。

SQL 要求提供默认值,因为您NOT NULL在创建的表定义中提到了。

于 2012-12-03T10:47:56.860 回答