I know that primary key values cannot be repeated. However, I want to make it so that games can be played on more than one platform. For example "Dark Souls" on PC and PS4. How do I modify the relational design so that I can give a SpelID
multiple consoles without repeating it?
This is my code:
drop table if exists Spel;
drop table if exists Plattform;
drop table if exists Företag;
create table Företag (
FöretagID varchar(50) primary key,
Företag text,
Högk text
);
create table Plattform (
PlattformID integer primary key,
Plattform varchar (50)
);
create table Spel (
SpelID integer primary key,
namn varchar(50),
genre text,
år date,
Åldergräns char(2),
foreign key (SpelID) references Plattform (PlattformID),
foreign key (namn) references Företag (FöretagID)
);
insert into Företag (FöretagID, Företag, Högk)
values ('Zelda', 'Capcom', 'Japan'),
('Bloodborne', 'From', 'Japan');
insert into Plattform (PlattformID, Plattform)
values (0001, 'Nintendo'),
(0001, 'PS4'),
(0002, 'PS4'),
(0002, 'Nintendo');
insert into Spel (SpelID, namn, genre, år, Åldergräns)
values (0001, 'Zelda', 'Äventyr', '2015-10-12', 7),
(0002, 'Bloodborne', 'A-RPG', '2017-09-09', 18);
I have tried to add them in the Plattform
table but I know that a primary key can only have one specific value. (Spel = Game)
2
Answers
First you need to drop the current CONSTRAINT:
Warning: Because of the
CASCADE
other constraints are also dropped… You will have to re-create those too! (see DBFIDDLE from more info)Then you als need to add the field
SpellID
to the tablePlattform
:After that you can re-create the primary key of table
Plattform
:see: DBFIDDLE
One game can be published on many platforms, and one platform can host many games. You need a many-to-many relationship between games and platforms in your relational model. See:
Could look like this:
Above modifications are minor fixes and other suggestions.
Here comes the magic (the implementation of the m:n relationship):
Sample data:
fiddle