John
John

Reputation: 4944

Forcing a field in a MySQL table to be lower-case

I have the field below in a MySQL table. How can I force it to be in lower-case letters only?

`username` varchar(30) NOT NULL,

Upvotes: 3

Views: 6510

Answers (3)

Cyril F
Cyril F

Reputation: 1882

You could use an 'before insert' trigger on the field you want to lowercased. More infos here : MySQL Trigger

(possible duplicate from here : mySQL phpmyadmin - lowercase data by default)

Upvotes: 1

Mike Wade
Mike Wade

Reputation: 1746

Rather then implementing it at application level you could write a database trigger that would change the content of the insert/update to lowercase as and when the insert/update happens, independent of the application.

Upvotes: 0

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

In the database use the MySQL LOWER() function around the inserts and/or the retrieval. You could also consider just doing it on the application side (meaning you still store the original...

Example

INSERT INTO mytable (username) VALUES(LOWER('TestUser'))

SELECT LOWER(username) FROM mytable

Upvotes: 4

Related Questions