sql - Need a database efficiency suggestion -
i have following database table. trying figure out way can structure can have position each player column. because each user going have multiple players , there multiple users, cannot figure out best way model db table efficiency.
create table `user_players` ( `id` int(11) not null auto_increment, `user_id` int(11) not null, `firstname` varchar(100) collate utf8_unicode_ci not null, `lastname` varchar(100) collate utf8_unicode_ci not null, `username` varchar(100) collate utf8_unicode_ci not null, `email` varchar(100) collate utf8_unicode_ci not null, `player1` varchar(100) collate utf8_unicode_ci not null, `player2` varchar(100) collate utf8_unicode_ci not null, `player3` varchar(100) collate utf8_unicode_ci not null, `player4` varchar(100) collate utf8_unicode_ci not null, `player5` varchar(100) collate utf8_unicode_ci not null, `player6` varchar(100) collate utf8_unicode_ci not null,
the thing can think of adding player_position ever player, this...
`player1` varchar(100) collate utf8_unicode_ci not null, `player_position1` varchar(100) collate utf8_unicode_ci not null, `player2` varchar(100) collate utf8_unicode_ci not null, `player_position2` varchar(100) collate utf8_unicode_ci not null,
is there better, more efficient way this?
if want design efficient databases, i'd suggest first atleast knowledge normalization.
to learn basics of normalization, refer to:
- what normalisation (or normalization)?
- http://www.studytonight.com/dbms/database-normalization.php
- https://www.youtube.com/watch?v=tcabzrvxv2i
clearly database not normalized , needs normalization.
issue 1: achieve 1st normalization form assigning primary key.
issue 2: database consists of transitive dependency(transitive dependency if consider id
primary key. thereafter, player
fields depend upon non key attribute. i.e. user_id
).
- fix creating different tables user , player.
- also take @ concept of foreign key.
if fix these 2 issues you'll no longer need both id
, user_id
together. can drop 1 of them.
final database schema:
create table `user` ( `user_id` int(11) not null primary key, /*make auto_increment if wish to*/ `firstname` varchar(100) collate utf8_unicode_ci not null, `lastname` varchar(100) collate utf8_unicode_ci not null, `username` varchar(100) collate utf8_unicode_ci not null, `email` varchar(100) collate utf8_unicode_ci not null ) create table `player` ( `player_id` int(11) not null primary key, /*make auto_increment if wish to*/ `player1` varchar(100) collate utf8_unicode_ci not null, `player2` varchar(100) collate utf8_unicode_ci not null, `player3` varchar(100) collate utf8_unicode_ci not null, `player4` varchar(100) collate utf8_unicode_ci not null, `player5` varchar(100) collate utf8_unicode_ci not null, `player6` varchar(100) collate utf8_unicode_ci not null, foreign key (user_id) references user(user_id) )
p.s.: syntax may vary depending upon type of database you're using.
Comments
Post a Comment