views:

675

answers:

3

I want to add some constraint to my username VARCHAR in the sql table so that if a username exists, a duplicate username in a different case cannot be created. How can I do this? thanks

edit: I am using postgres sql, a little syntax help will be greatly appreicated

+3  A: 

If the tables are not yet populated you may consider simply converting to a standard upper or lower case prior to doing any insertions and making the field a primary key (or just have a unique constraint). If the user want to see his userid in the case he specified this could be another column in the database.

Update:Based on the updated tags I would still suggest the solution I have proposed as being less dependent on a particular DBMS.

ojblass
+1 Good idea. to ensure that usernames are in a single case.
Preet Sangha
-1 sorry. Only badness could arise out of storing a username in two different ways when one of them can be trivially computed from the other. There's no need to break normalisation here as Endlessdeath's answer shows.
j_random_hacker
+8  A: 

From the docs

CREATE UNIQUE INDEX lower_title_idx ON films ((lower(title)));
Endlessdeath
+1  A: 

Do note that PostgreSQL 8.4 (currently beta) will have a case-insensitive text type.

bortzmeyer