有人可以帮我解释为什么我在尝试运行SQL
文件时可能会收到以下语法错误。它与我在桌上自行车手中创建的foreign ke
y t_id
相关。但是一切都被引用等运行良好。在我尝试使用它来输入数据之前......必须有一些我缺少的东西
ERROR 1054 (42S22) at line 15: Unknown column 't_id' in 'field list'
下面是SQL
给我错误(输入数据),下面是我复制了我创建的表格列表。谢谢。
INSERT INTO bike (id, bike_brand, cost, colour)
VALUES
(NULL, 'Canyon Aeroad', '£3500', 'White'),
(NULL, 'Scott Addict', '£4000', 'Black'),
(NULL, 'BMC Timemachine', '£2800', 'Matt Black'),
(NULL, 'Pinarello Dogma F8', '£5800', 'Blue');
INSERT INTO team (id, team_name, main_colour, b_id, year_established, sponsor, budget)
VALUES
(NULL, 'Movistar', 'Purple', NULL, '2003', 'MOVISTAR', '£20,433,043'),
(NULL, 'Orica Greenedge', 'Green', Null, '2005', 'Greenedge', '£30,123,448'),
(NULL, 'BMC', 'Red', NULL, '2008', 'BMC Switzerland', '£49,432,123'),
(NULL, 'Sky', 'Blue', NULL, '2010', 'Sky', '£71,123,543');
INSERT INTO cyclist (cyclist_id, t_id, firstname, lastname, gender, Age)
VALUES
(NULL, NULL, 'Alex', 'Dowsett', 'Male', '28'),
(NULL, NULL, 'Nairo', 'Quintana', 'Male', '25'),
(NULL, NULL, 'Simon', 'Yates', 'Male', '22'),
(NULL, NULL, 'Adam', 'Yates', 'Male', '23'),
(NULL, NULL, 'Taylor', 'Phinney', 'Male', '24'),
(NULL, NULL, 'Stefan', 'Kung', 'Male', '21'),
(NULL, NULL, 'Chris', 'Froome', 'Male', '28'),
(NULL, NULL, 'Geraint', 'Thomas', 'Male', '29');
INSERT INTO race(race_name, team_capacity, prize_money, main_sponsor)
VALUES
('Tour De France', '12', '£2,500,000', 'Festina'),
('Tour of Britian', '6', '£250,000', 'Aviva'),
('Tour of Flanders' '8', '£1,250,000', 'TooGoodForOne');
DROP TABLE IF EXISTS bike, team, cyclist, race;
CREATE TABLE bike (
id INT AUTO_INCREMENT,
bike_brand VARCHAR (50),
cost VARCHAR(50),
colour VARCHAR (50),
PRIMARY KEY (id)
);
CREATE TABLE team (
id INT AUTO_INCREMENT,
team_name VARCHAR (50),
main_colour VARCHAR (50),
b_id INT,
year_established INT (50),
sponsor VARCHAR (50),
budget VARCHAR,
PRIMARY KEY (id),
FOREIGN KEY (b_id)
REFERENCES bike (id)
);
CREATE TABLE cyclist (
cyclist_id INT AUTO_INCREMENT,
t_id INT,
firstname VARCHAR (50),
lastname VARCHAR (50),
gender CHAR (1),
Age INT,
PRIMARY KEY (cyclist_id),
FOREIGN KEY (t_id)
REFERENCES team (id)
);
CREATE TABLE race (
race_name VARCHAR (50),
team_capacity INT,
prize_money VARCHAR(50),
main_sponsor VARCHAR (50),
PRIMARY KEY (race_name)
);
答案 0 :(得分:3)
来自Using Foreign Key Constraints上的Mysql手册页:
外键和引用键中的对应列必须 有类似的数据类型。整数类型的大小和符号必须是 相同。字符串类型的长度不必相同。对于 非二进制(字符)字符串列,字符集和排序规则 必须是一样的。
在你的情况下,你有tinyint和int。这不会起作用,因为它们不一样。
初始错误消息是,无论您在何处运行此消息,都会抱怨t_id
表中没有cyclist
。可以通过运行命令show create table cyclist
答案 1 :(得分:2)
首先,对于自动增量列,您不需要指定插入值。假设存在主键约束,则在创建行时将自动插入此值。
例如,将INSERT INTO team (id, team_name, main_colour, b_id, year_established, sponsor, budget)
缩减为INSERT INTO team (team_name, main_colour, b_id, year_established, sponsor, budget)
主键不能为空 - 它们需要具有唯一值。您的插入内容正在尝试为主键ID分配null,这将导致其他错误。
其次,t_id实际上不应存在于表中,以便呈现该错误。检查以确保已对数据库进行任何更改