开发者

Unique index not based on words order?

Is it possibile to have a开发者_Go百科 mysql UNIQUE index on a varchar field not based on the words order?

I mean if there is a row with key1 key2, and I try to insert key2 key1 mysql should throw an error.


You could achieve this effect by adding before insert and before update triggers to the table; they could check whether a row with the fields reversed exists or not before inserting/updating if it doesn't, or forcing an error if it does.

See here for more information.


If you set the indexed to UNIQUE, you will not be able to enter the identical data twice - numbers, words or otherwise.

If you wish to achieve something otherwise, you'll have to set a new non-unique field in your database then you'll be able achieve it using external code and queries (PHP, C#).


Create a trigger to do this.

i.e.

CREATE TRIGGER trigger_name BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
   IF ((SELECT COUNT(*) FROM table WHERE col2=NEW.col1 OR col1=NEW.col2) <> 0)
   THEN
     CALL this_procedure_does_not_exist();
   END IF
END
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜