Make table with two record unique - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Make table with two record unique (/showthread.php?tid=69573) |
Make table with two record unique - omid_student - 12-16-2017 Hello I have below table In this table we can add sUsername: omid sProductID: 1 ---------------- sUsername: omid sProductID: 2 ---------------- sUsername: omid sProductID: 1 in high record i add two same record Actually i need that insert unique record in table Username and sProduct can be duplicate But we cannot have two same record example : sUsername: omid sProductID: 1 ---------------- sUsername: omid sProductID: 2 ---------------- sUsername: jack sProductID: 1 ---------------- sUsername: jack sProductID: 2 In high record different record for each username RE: Make table with two record unique - plonknimbuzz - 12-16-2017 this is very simple. this case call composite primary key. you can google for this. create new table tbl_user_product_relation sUsername Primary Key char(8) sProductID primary key int(11) sDatetime timestamp current_timestamp try to add entry directly from php myadmin sUsername : tony sProducID: 1 sUsername: tony sProductID: 2 sUsername: joe sProductID: 1 all above will successfull. and below will not allowed sUsername: tony sProductID: 1 this is what do you want, right? in PHP you need to check this select * from tbl_user_product_relation where sUsername=? and sProductID=? if num_rows() == 1 Insert else echo 'duplicate' NOTE: Quote:hei.. what about my sID? you dont need that. What you know about table is: every table must have id as primary key and auto_increment Nooooo... that's totally wrong. and if you pretend to not remove sID, you will have 3 composite primary key, and you will always have duplicate possibility, because: sID sUsername sProductID 1 tony 1 2 tony 1 3 tony 1 100 tony 1 is different RE: Make table with two record unique - omid_student - 12-17-2017 (12-16-2017, 10:58 PM)plonknimbuzz Wrote: this is very simple. Oh yes you right AutoIncrement id is wrong completely I try now and reply you about it Thank so much RE: Make table with two record unique - omid_student - 12-17-2017 Yes it is working now I remove autoincrement from table and now i dont have any duplicate record Thanks RE: Make table with two record unique - plonknimbuzz - 12-20-2017 (12-17-2017, 01:53 AM)omid_student Wrote: Yes it is working nowglad to hear that |