I'm writing a mySQL script to create a database about musicians and bands. Many musicians can be in 1 band and many bands have 1 genre. I've made the first table called musicians with the attributes musician_name and band_name successfully, with band_name as a primary key. Now I'm trying to create the second table called bands, which will have the attributes band_name and genre, using band_name as the primary key and also as a foreign key referencing band_name from the musicians table. When I run my script in MySQL Command Line Client, the script will display the first table, but then I receive multiple lines of errors that all say "ERROR 1146 (42S02): Table 'tours.bands' doesn't exist". I've created the referenced table before the referencing table, both data types of band_name are the same in both tables, and band_name includes values which are all unique, so I'm not sure what I'm doing wrong.
Here is my SQL Script:
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;
The table bands should look like:
Bands | Genre
Purple Death | Heavy Metal
Megapain | Heavy Metal
Old Strawberry | Folk
Bakers Dozen | Folk
etc...
Would appreciate any help as to why this table will not display and why mySQL thinks the table doesn't exist! Thanks.
EDIT: I found the issue was actually about a missing INDEX on the referenced table.
show tables, then run only the part for creatingbandstable (if it is actually not there) and show us the error (if any). Then we can try to help. I've run through your scripts and all went well.