Home » Community » Newbie corner » SQL : how to prevent duplicates ?
Re: SQL : how to prevent duplicates ? [message #42378 is a reply to message #42377] |
Mon, 10 March 2014 21:43   |
|
Hi jibe,
Unique index can be made simply with UNIQUE:
TABLE_(CUSTOMERS)
INT_ (CODE) PRIMARY_KEY
STRING_ (LASTNAME, 40) UNIQUE
STRING_ (FIRSTNAME, 40)
END_TABLE
It should work for most of the supported engines, including sqlite, I believe.
For two unique columns, there is DUAL_UNIQUE:
TABLE_(CUSTOMERS)
INT_ (CODE) PRIMARY_KEY
STRING_ (LASTNAME, 40)
STRING_ (FIRSTNAME, 40) DUAL_UNIQUE(FIRSTNAME, LASTNAME)
END_TABLE
If you need even more columns to be unique, there is UNIQUE_LIST(u, l), where u stands for identifier and l is list of columns. I never used it but I think it should work like this:
TABLE_(CUSTOMERS)
INT_ (CODE) PRIMARY_KEY
STRING_ (LASTNAME, 40)
STRING_ (FIRSTNAME, 40)
STRING_ (CITY, 40) UNIQUE_LIST(address_key, FIRSTNAME, LASTNAME, CITY)
END_TABLE
If you have some troubles, it might be a good idea to check the schema of the created table, from command line (or other) client. Sometimes there are subtle bugs that prevent some parts of the schema to be created properly, and they're simply missing. Also any error messages and or logs might tell you if there is any hidden problem. Hope that helps 
Best regards,
Honza
|
|
|
Goto Forum:
Current Time: Sun Apr 27 21:15:03 CEST 2025
Total time taken to generate the page: 0.04050 seconds
|