【发布时间】:2018-10-20 23:17:53
【问题描述】:
我创建了一个 MySQL 数据库,但在我在其中一个表中插入虚拟数据时遇到了困难。这是我用来创建包含三个表的数据库的查询:类别、交易、预算_2018;
use mct;
create table if not exists categories(
id int auto_increment not null,
category varchar(255),
primary key (id)
);
create table if not exists transactions(
id int auto_increment not null,
dt date,
vendor varchar(255),
amount decimal(7,2),
cat_id int default 1,
primary key(id),
foreign key(cat_id) references categories(id)
);
create table if not exists budgets_2018(
id int not null,
January decimal(7,2) default 0.00,
February decimal(7,2) default 0.00,
March decimal(7,2) default 0.00,
April decimal(7,2) default 0.00,
May decimal(7,2) default 0.00,
June decimal(7,2) default 0.00,
July decimal(7,2) default 0.00,
August decimal(7,2) default 0.00,
September decimal(7,2) default 0.00,
October decimal(7,2) default 0.00,
November decimal(7,2) default 0.00,
December decimal(7,2) default 0.00,
primary key(id),
foreign key(id) references categories(id)
);
insert into categories (category) values ('Uncategorized');
insert into budgets_2018(id) values ((select id from categories where category = 'Uncategorized'));
理论上,类别与交易具有一对多的关系,与budgets_2018 具有一对一的关系。此外,最后几行代码确实生成了一个包含一条记录(1,“未分类”)的类别表以及一个包含一条记录(每月 1、0.00、0.00...x11 多)的 budgets_2018 表。
这是我用来将一些虚拟数据添加到事务表中的查询。这些表有五列,第一列自动递增,第五列默认为 1。第五列 (cat_id) 也是主类别的外键因此我将它们排除在下面的插入查询之外:
insert into transactions (dt, vendor, amount) values (
(curdate(), 'DummyVendor1', 3.45),
(curdate(), 'DummyVendor2', 11.47),
(curdate(), 'DummyVendor3', 73.22),
(curdate(), 'DummyVendor4', 199.27),
(curdate(), 'DummyVendor5', 34.56),
(curdate(), 'DummyVendor6', 18.99),
(curdate(), 'DummyVendor7', 1.45),
(curdate(), 'DummyVendor8', 2.41)
);
但是在插入查询之后我不断收到
“错误 1136:列计数与第 1 行的值计数不匹配”。
如果我错了,请纠正我,但事务的第一列(id 自动递增)和第五列(cat_id 默认为 1)应该在不需要显式插入的情况下填充,对吗?所以我不确定是什么导致了错误。
谢谢。
【问题讨论】:
标签: mysql