简体   繁体   English

有关外键的SQL语法错误

[英]SQL syntax error regarding foreign key

Could someone help me figure why I might be receiving the following syntax error when trying to run my SQL file please. 有人可以帮我弄清楚为什么我尝试运行SQL文件时为什么会收到以下语法错误。 It's related to my foreign ke y t_id which I created in the table cyclist. 这与我在桌单车t_id创建的foreign ke y t_id有关。 However everything is referenced etc and runs fine. 但是,一切都被引用等等,并且运行良好。 Until I try to use it to input data...there must be something I'm missing 在我尝试使用它来输入数据之前...一定缺少我想要的东西

ERROR 1054 (42S22) at line 15: Unknown column 't_id' in 'field list'

Below is the SQL that's giving me the error (inputting data), below that I have copied the list of tables that I've created. 下面是给我错误(输入数据)的SQL ,下面是我复制所创建表的列表的信息。 Thank you. 谢谢。

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

From the Mysql Manual page on Using Foreign Key Constraints : 使用外键约束的Mysql手册页上:

Corresponding columns in the foreign key and the referenced key must have similar data types. 外键和引用键中的对应列必须具有相似的数据类型。 The size and sign of integer types must be the same. 整数类型的大小和符号必须相同。 The length of string types need not be the same. 字符串类型的长度不必相同。 For nonbinary (character) string columns, the character set and collation must be the same. 对于非二进制(字符)字符串列,字符集和排序规则必须相同。

In your case you have tinyint and int. 就您而言,您有tinyint和int。 That won't work, as they are not the same. 那是行不通的,因为它们不一样。

The initial error message is that wherever you are running this, it is complaining that t_id is not in the cyclist table. 最初的错误消息是,无论您在哪里运行它,都在抱怨t_id不在cyclist表中。 That can be verified with running the command show create table cyclist 可以通过运行命令show create table cyclist来验证

Firstly, for auto increment columns you don't need to specify an insert value. 首先,对于自动增量列,您无需指定插入值。 Assuming a primary key constraint, this value will be auto inserted when the row is created. 假设存在主键约束,则在创建行时将自动插入此值。

for example, reduce INSERT INTO team (id, team_name, main_colour, b_id, year_established, sponsor, budget) down to INSERT INTO team (team_name, main_colour, b_id, year_established, sponsor, budget) 例如,将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)

Primary keys CANNOT be null - they need to have a unique value. 主键不能为空-它们必须具有唯一的值。 Your inserts are trying to assign null for primary key id's which will throw out additional errors. 您的插入内容正在尝试为主键ID分配null,这将引发其他错误。

Secondly, t_id must not actually exist in the table itself for that error to be presented. 其次,t_id实际上必须不存在于表本身中,才能显示该错误。 Check to make sure any alterations have been committed to the database 检查以确保任何更改已提交到数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM