Reputation: 3
I'm trying to insert into my tables. The teams insert correctly but then i want to insert their team colors. However under this INSERT INTO, only the first color adds. Then I get an error for the second. How do I tell MySQL to add the colors for the second team and so on?
there is more code SQL but I wanted to keep it short
CREATE TABLE team
(
team_id int NOT NULL AUTO_INCREMENT,
team varchar(255) NOT NULL,
conference varchar(255) NOT NULL,
division varchar(255) NOT NULL,
CONSTRAINT teamer FOREIGN KEY (team_id) REFERENCES player (team),
PRIMARY KEY (team)
);
CREATE TABLE color
(
team varchar(255) NOT NULL,
primary_color varchar(255) NOT NULL,
secondary_color varchar(255) NOT NULL,
CONSTRAINT colorer FOREIGN KEY (team) REFERENCES team (team),
PRIMARY KEY (team)
);
-- INSERT
-- team
INSERT INTO team (team, conference, division)
VALUES ('SEA', 'NFC', 'N_WEST');
INSERT INTO team (team, conference, division)
VALUES ('ARI', 'NFC', 'N_WEST');
INSERT INTO team (team, conference, division)
VALUES ('SFO', 'NFC', 'N_WEST');
INSERT INTO team (team, conference, division)
VALUES ('STL', 'NFC', 'N_WEST');
-- INSERT
-- color
INSERT INTO color (primary_color, secondary_color)
VALUES ('Navy', 'Grey');
INSERT INTO color (primary_color, secondary_color)
VALUES ('Red', 'White');
INSERT INTO color (primary_color, secondary_color)
VALUES ('Red', 'Gold');
INSERT INTO color (primary_color, secondary_color)
VALUES ('Navy', 'Gold');
Upvotes: 0
Views: 108
Reputation: 781058
In the color
table you specify that team
is the primary key. That means it has to be different for each row. But you're not specifying the team in your INSERT
statements, so it's using the default value, the empty string. This causes all the inserts to try to use the same team. Since the team must be unique, you get an error because of the attempt to create a duplicate.
You need to specify a valid team name when inserting colors:
INSERT INTO colors (team, primary_color, secondary_color)
VALUES ('SEA', 'Navy', 'Grey');
INSERT INTO color (team, primary_color, secondary_color)
VALUES ('ARI', 'Red', 'White');
Upvotes: 1