1

i have a table which has only two column one is pid(Photo ID) and another one is tid(Tag ID)

+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| pid | int(10) | NO | | NULL | |
| tid | int(10) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+

this table will store a photo id with a tag id, but i am facing a problem and i guess which is against normalization as well, here is my table data.

+-----+-----+
| pid | tid |
+-----+-----+
| 10 | 1 |
| 10 | 2 |
| 10 | 3 |
| 10 | 4 |
| 0 | 6 |
| 0 | 10 |
| 0 | 10 |
| 10 | 1 |
| 10 | 2 |
| 10 | 3 |
| 10 | 4 |
+-----+-----+

it store many duplicate values, how to get rid of this?
I mean, how i can store this types of row only for one time: | 10 | 1 |
I have tried with UNIQE but this will going to store a photo id or a tag id only for one time?
I'm using PHP
Thanks

asked Apr 14, 2014 at 9:59

1 Answer 1

3

You need a compound unique index.

Suppose your table is called photos. You can do this:

CREATE TABLE photos_new LIKE photos;
ALTER TABLE photos_new ADD UNIQUE INDEX pid_tid_index (pid,tid);
INSERT IGNORE INTO photos_new SELECT * FROM photos;
ALTER TABLE photos RENAME photos_old;
ALTER TABLE photos_new RENAME photos;

If it works out, then run

DROP TABLE photos_old;

Give it a Try !!!

answered Apr 14, 2014 at 10:06
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.