how can reduce duplication in database?
This time i really need suggestion tell me about how can i reduce duplication in database.
the best way is to add unique keys to your table. So, depending on what columns need to be unique is what you would put the key on examples:
email, username -- This would require that the email and username be unique.
- firstname.lastname@example.org, kitty is not the same as email@example.com, cat both are unique
- firstname.lastname@example.org, kitty is not the same as email@example.com, kitty both of these are unique
But, if we were to split these into two different unique keys
email -- This would require that only the email be unique
username -- This would require that only the username be unique
Now each one individually needs to be unique, whereas the first example both the email and username needed to be unique. I hope that makes sense.
now when we do our query....
that will insert into the database UNLESS it isn't unique, then nothing will be inserted.
insert ignore into members (email, username) values ('firstname.lastname@example.org', 'kitty');
Or we can do:
Usually you wouldn't have this on a members table, but to get the example across, if you have a duplicate key, you can update the duplicate row instead of doing an insert.
insert into members (email, username) values ('email@example.com', 'kitty') on duplicate key update member_count = member_count + 1;
Browser :: FireFox 1.5
Resolution :: 1152x864
Connection :: Cable Modem 2Mbs
We may need a better explanation of what you mean by "duplication", if the prior reply does not address your problem.
Originally Posted by chamadii
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)