tags:

views:

76

answers:

3

I do file parsing in Perl and insert into a table in a MySQL database. For example, I have the following fields:

S.No ,PCID,USERNAME, TIME   INFORMATION.
1     203  JANE      22:08  updation
2     203  JANE      22:09  deletion
3     203  JANE      22:10  insertion

In this table I wanted to have the PCID to be unique, USERNAME to be unique. S.No is unique, since I have set it to autonumbering and it's the primary key. Now my question is:

If I add PCID and USERNAME as composite primary key, I still find duplicates in the table. There is no change. The same o/p. What should be done to remove the duplicate? Should I code in Perl to check for duplicates before insertion?

Please guide and provide assistance. Thanks in advance.

+2  A: 

I'm assuming that you want each column to be unique, rather than each composite key to be unique. Use a unique constraint on the columns that should be unique:

I'm not sure what happens if you add a unique constraint to MySQL on a column that doesn't have unique values already. You might have to perform manual cleanup before it will let you add the constraint.

You definitely shouldn't do this in Perl. Good data driven apps are about getting all of the logic of the app as close to the data model as possible. This one belongs database side.

masonk
i did a cleanup and gave this unique constraint.?But the problem is insertion is not allowed for the distinct fields in the same row..??How to tackle this
Sreeja
Post the error message.
masonk
DBD::mysql::st execute failed: Duplicate entry '203 ' for key 2 at function.pl line 55, <IN> line 17.
Sreeja
I don't get it. Isn't that what you wanted?
masonk
+1  A: 
ALTER TABLE MyTable DROP PRIMARY KEY

ALTER TABLE MyTable
    ADD PRIMARY KEY (`S.No`),
    ADD UNIQUE KEY `PCID_uk` (`PCID`),
    ADD UNIQUE KEY `USERNAME_uk` (`USERNAME`)

If the file you're importing from contains duplicate values and you want the duplicate values to be discarded, use the IGNORE keyword. If you're using LOAD DATA INFILE then this is achieved using syntax like this:

LOAD DATA INFILE 'file_name' IGNORE INTO TABLE ...

See this documentation page.

Hammerite
I have tried using unique key.The problem is only first row gets inserted into the table from the file.After that i get a error sayng that duplicate found....
Sreeja
S.No ,PCID,USERNAME, TIME INFORMATION. 1 203 JANE 22:08 updation 2 22:09 deletion3 22:12 modifyIt should not allow duplicates of pcid and name,but shld allow type and time to get inserted...
Sreeja
Have you looked through the file to check that there aren't, in fact, any duplicate values?
Hammerite
The file contains dupicate values..When i insert those into tablle i shold provide a restriction
Sreeja
I think I understand. Your dump file contains duplicate values and you want those to be discarded. I have edited my answer with more information.
Hammerite
+1  A: 

You want the S.No to remain the primary key and PCID + USERNAME to be unique, so close to what Hammerite said:

ALTER TABLE MyTable
    ADD PRIMARY KEY (`S.No`),
    ADD UNIQUE KEY `PCID_USER_uk` (`PCID`, `USERNAME`);
Eric Darchis