使用SQL脚本

时间:2017-12-13 14:37:45

标签: mysql sql foreign-keys relational-database

我正在编写一个mySQL脚本来创建一个关于音乐家和乐队的数据库。许多音乐家可以在1个乐队中,很多乐队都有1个流派。我已成功创建第一个名为musicians的表,其中musician_name和band_name属性成功,band_name为主键。现在我尝试创建名为band的第二个表,它将具有band_name和genre属性,使用band_name作为主键,也可以作为引用musicians表中band_name的外键。当我在MySQL命令行客户端中运行我的脚本时,脚本将显示第一个表,但随后我会收到多行错误,这些错误都表示" ERROR 1146(42S02):表' tours.bands&#39 ;不存在"。我在引用表之前创建了引用表,两个表中band_name的两种数据类型都相同,band_name包含全部唯一的值,因此我不确定我在做什么错。

这是我的SQL脚本:

DROP DATABASE IF EXISTS tours;
CREATE DATABASE tours;
USE tours;

CREATE TABLE musicians(
    musician_name varchar(30),
    band_name varchar(30),
    PRIMARY KEY(musician_name)
);

CREATE TABLE bands(
    band_name varchar(30),
    genre varchar(30),
    PRIMARY KEY (band_name),
    FOREIGN KEY (band_name) REFERENCES musicians(band_name)
);

INSERT INTO musicians (musician_name, band_name) VALUES ('Robinson', 'Purple Death');
INSERT INTO musicians (musician_name, band_name) VALUES ('Harvey', 'Purple Death');
INSERT INTO musicians (musician_name, band_name) VALUES ('Jackson', 'Purple Death');
INSERT INTO musicians (musician_name, band_name) VALUES ('Lynne', 'Megapain');
INSERT INTO musicians (musician_name, band_name) VALUES ('Perry', 'Megapain');
INSERT INTO musicians (musician_name, band_name) VALUES ('Hook', 'Megapain');
INSERT INTO musicians (musician_name, band_name) VALUES ('Matthews', 'Old Strawberry');
INSERT INTO musicians (musician_name, band_name) VALUES ('Lowden', 'Old Strawberry');
INSERT INTO musicians (musician_name, band_name) VALUES ('Bowyer', 'Old Strawberry');
INSERT INTO musicians (musician_name, band_name) VALUES ('Mills', 'Bakers Dozen');
INSERT INTO musicians (musician_name, band_name) VALUES ('Watson', 'Bakers Dozen');
INSERT INTO musicians (musician_name, band_name) VALUES ('Weston', 'Sarabelle Weston');
INSERT INTO musicians (musician_name, band_name) VALUES ('D Smith', 'Dimity Smith');
INSERT INTO musicians (musician_name, band_name) VALUES ('A Smith', 'Dimity Smith');
INSERT INTO musicians (musician_name, band_name) VALUES ('Atkins', 'Old Storm Town');
INSERT INTO musicians (musician_name, band_name) VALUES ('Ambrose', 'Old Storm Town');
INSERT INTO musicians (musician_name, band_name) VALUES ('Jones', 'Old Storm Town');
INSERT INTO musicians (musician_name, band_name) VALUES ('Williams', 'Horizons');
INSERT INTO musicians (musician_name, band_name) VALUES ('Moore', 'Horizons');
INSERT INTO musicians (musician_name, band_name) VALUES ('Tyler', 'Horizons');

SELECT * FROM musicians;

INSERT INTO bands (band_name, genre) VALUES ('Purple Death', 'Heavy Metal'); 
INSERT INTO bands (band_name, genre) VALUES ('Megapain', 'Heavy Metal'); 
INSERT INTO bands (band_name, genre) VALUES ('Old Strawberry', 'Folk'); 
INSERT INTO bands (band_name, genre) VALUES ('Bakers Dozen', 'Folk'); 
INSERT INTO bands (band_name, genre) VALUES ('Sarabelle Weston', 'Country and Western'); 
INSERT INTO bands (band_name, genre) VALUES ('Dimity Smith', 'Country and Western'); 
INSERT INTO bands (band_name, genre) VALUES ('Old Storm Town', 'Rock'); 
INSERT INTO bands (band_name, genre) VALUES ('Horizons', 'Rock'); 

SELECT * FROM bands;

表格带应如下所示:

Bands          |  Genre
Purple Death   | Heavy Metal
Megapain       | Heavy Metal
Old Strawberry | Folk
Bakers Dozen   | Folk

等...

非常感谢为什么这个表不会显示以及为什么mySQL认为该表不存在的任何帮助!感谢。

编辑:我发现问题实际上是关于引用表上缺少INDEX的问题。

2 个答案:

答案 0 :(得分:2)

您需要FK引用的列的索引。您的第一个表定义应该是

CREATE TABLE musicians(
    musician_name varchar(30),
    band_name varchar(30),
    PRIMARY KEY(musician_name),
    INDEX (band_name)
);

专业提示:运行SQL语句时必须查看错误消息,尤其是数据定义语句。不看错误信息就像闭着眼睛开车一样。如果有的话,它的效果很差。

当我运行你的数据定义语句时,我得到了这个。

  

SQL错误(1005):无法创建表bands(错误号:150“外键约束形成错误”)

答案 1 :(得分:0)

我认为你的外键是在错误的表格上。试试这个

CREATE TABLE bands(
    band_name varchar(30),
    genre varchar(30),
    PRIMARY KEY (band_name)    
);


CREATE TABLE musicians(
    musician_name varchar(30),
    band_name varchar(30),
    PRIMARY KEY(musician_name),
    FOREIGN KEY (band_name) REFERENCES bands(band_name)
);

INSERT INTO bands (band_name, genre) VALUES ('Purple Death', 'Heavy Metal'); 
INSERT INTO bands (band_name, genre) VALUES ('Megapain', 'Heavy Metal'); 
INSERT INTO bands (band_name, genre) VALUES ('Old Strawberry', 'Folk'); 
INSERT INTO bands (band_name, genre) VALUES ('Bakers Dozen', 'Folk'); 
INSERT INTO bands (band_name, genre) VALUES ('Sarabelle Weston', 'Country and Western'); 
INSERT INTO bands (band_name, genre) VALUES ('Dimity Smith', 'Country and Western'); 
INSERT INTO bands (band_name, genre) VALUES ('Old Storm Town', 'Rock'); 
INSERT INTO bands (band_name, genre) VALUES ('Horizons', 'Rock'); 

INSERT INTO musicians (musician_name, band_name) VALUES ('Robinson', 'Purple Death');
INSERT INTO musicians (musician_name, band_name) VALUES ('Harvey', 'Purple Death');
INSERT INTO musicians (musician_name, band_name) VALUES ('Jackson', 'Purple Death');
INSERT INTO musicians (musician_name, band_name) VALUES ('Lynne', 'Megapain');
INSERT INTO musicians (musician_name, band_name) VALUES ('Perry', 'Megapain');
INSERT INTO musicians (musician_name, band_name) VALUES ('Hook', 'Megapain');
INSERT INTO musicians (musician_name, band_name) VALUES ('Matthews', 'Old Strawberry');
INSERT INTO musicians (musician_name, band_name) VALUES ('Lowden', 'Old Strawberry');
INSERT INTO musicians (musician_name, band_name) VALUES ('Bowyer', 'Old Strawberry');
INSERT INTO musicians (musician_name, band_name) VALUES ('Mills', 'Bakers Dozen');
INSERT INTO musicians (musician_name, band_name) VALUES ('Watson', 'Bakers Dozen');
INSERT INTO musicians (musician_name, band_name) VALUES ('Weston', 'Sarabelle Weston');
INSERT INTO musicians (musician_name, band_name) VALUES ('D Smith', 'Dimity Smith');
INSERT INTO musicians (musician_name, band_name) VALUES ('A Smith', 'Dimity Smith');
INSERT INTO musicians (musician_name, band_name) VALUES ('Atkins', 'Old Storm Town');
INSERT INTO musicians (musician_name, band_name) VALUES ('Ambrose', 'Old Storm Town');
INSERT INTO musicians (musician_name, band_name) VALUES ('Jones', 'Old Storm Town');
INSERT INTO musicians (musician_name, band_name) VALUES ('Williams', 'Horizons');
INSERT INTO musicians (musician_name, band_name) VALUES ('Moore', 'Horizons');
INSERT INTO musicians (musician_name, band_name) VALUES ('Tyler', 'Horizons');