0

我正在尝试向现有数据库添加一列以存储 UUID,然后检索具有该记录的 UUID 的行,但我一直得到一个空集。我将列创建为 BINARY(64) 是因为当我按照某些网站的建议尝试 BINARY(16) 时,它不足以容纳数据。这是我的创建声明:

ALTER TABLE `my_table`  ADD `email_uuid` BINARY(64) NULL,  ADD UNIQUE (`email_uuid`)

然后我填充它:

UPDATE `my_table` SET `email_uuid` = UUID();

为了测试它,我查询了表以获取 UUID:

mysql> select emailaddr,email_uuid FROM my_table WHERE emailaddr = 'myemail@mydomain.com';
+------------------------------------------+------------------------------------------+
| emailaddr                                | email_uuid                               |
+------------------------------------------+------------------------------------------+
| myemail@mydomain.com | a332f4d4-ddb2-11e2-88f9-80ee731fd7a1     |
+------------------------------------------+------------------------------------------+

我尝试了几种不同的方法来恢复该行:

mysql> select emailaddr,email_uuid FROM my_table WHERE email_uuid = 'a332f4d4-ddb2-11e2-88f9-80ee731fd7a1';
Empty set (0.00 sec)

mysql> select emailaddr,email_uuid FROM my_table WHERE email_uuid = '{a332f4d4ddb211e288f980ee731fd7a1}';
Empty set (0.00 sec)

mysql> select emailaddr,email_uuid FROM my_table WHERE email_uuid = BIN('{a332f4d4ddb211e288f980ee731fd7a1}');
Empty set (0.02 sec)

mysql> select emailaddr,email_uuid FROM my_table WHERE email_uuid = '61333332663464342d646462322d313165322d383866392d38306565373331666437613100000000';
Empty set (0.00 sec)

mysql> select emailaddr,email_uuid FROM my_table WHERE email_uuid = UNHEX(REPLACE('a332f4d4-ddb2-11e2-88f9-80ee731fd7a1','-',''));
Empty set (0.02 sec)

有什么我想念的吗?

4

1 回答 1

0

UUID 字符串长 36个字节。有了这个微小的变化,它对我来说就像一个魅力:

mysql> create table U (`email_uuid` BINARY(36)):
Query OK, 0 rows affected (0.34 sec)

mysql> insert into U VALUES ( 'a332f4d4-ddb2-11e2-88f9-80ee731fd7a1' ):
Query OK, 1 row affected (0.01 sec)

mysql> select * from U where `email_uuid` = 'a332f4d4-ddb2-11e2-88f9-80ee731fd7a1':
+--------------------------------------+
| email_uuid                           |
+--------------------------------------+
| a332f4d4-ddb2-11e2-88f9-80ee731fd7a1 |
+--------------------------------------+
1 row in set (0.00 sec)
于 2013-06-25T18:41:34.913 回答