mysql - SQL PRIMARY KEY duplicate error same PRIMARY KEY across 3 tables -
getting error having duplicate primary key. how can use same primary key across multiple tables? showing how can use in tables below appreciated. error com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolati onexception: duplicate entry 'jam' key 'primary'
create table `skills` ( `playername` varchar(15) not null default '', `testa` double default null, `testb` double default null, `testc` double default null, `testd` double default null, primary key (`playername`) ) engine=myisam; create table `playerrights` ( `playername` varchar(15) not null default '', `rank` int(2) default null, primary key (`playername`) ) engine=myisam; create table `skillsoverall` ( `playername` varchar(15) not null default '', `lvl` int(11) default null, `xp` bigint(11) default null, primary key (`playername`) ) engine=myisam;
you can't spread primary key across multiple tables. should have table called players
1 row per player, , these 3 tables should have foreign key pointing that.
the playername
column can indeed primary key on each of 3 tables want be, it's unlikely that's want. if want there more 1 row per player in of these tables, shouldn't pk on tables.
a primary key means 2 things:
- unique (no 2 rows can have same value column(s))
- not null
the error got reflects row you're inserting violate #1.
the foreign key players
table ensures referential integrity. nothing stored players don't exist. , players stats stored aren't removed prior stats being removed, etc.
this first table's ddl fk players
table (you should have such table):
create table `skills` ( `playername` varchar(15) not null default '', `testa` double default null, `testb` double default null, `testc` double default null, `testd` double default null, index (playername), constraint fk_playername_rights foreign key (playername) references players (playername) on delete restrict on update restrict ) engine=myisam;
note: can change restrict cascade if want effect (based on want have happen if attempt change or remove player in players table)
Comments
Post a Comment