MySQL的binary类型操作
⽰例数据表:
CREATE TABLE test_bin (
bin_id BINARY(16) NOT NULL
) Engine=InnoDB;
插⼊数据(内容是⼀个32位的UUID字符串值):
INSERT INTO test_bin(bin_id) VALUES(UNHEX('FA34E10293CB42848573A4E39937F479'));
INSERT INTO test_bin(bin_id) VALUES(UNHEX(?));
或
INSERT INTO test_bin(bin_id) VALUES(x'FA34E10293CB42848573A4E39937F479');
查询数据:
SELECT HEX(bin_id) AS bin_id FROM test_bin;
SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = UNHEX('FA34E10293CB42848573A4E39937F479');
SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = UNHEX(?);
SELECT HEX(bin_id) AS bin_id FROM test_bin WHERE bin_id = x'FA34E10293CB42848573A4E39937F479';
查询结果:
bin_id
--------------------------
FA34E10293CB42848573A4E39937F479
备注:使⽤MySQL内置的 UUID() 创建⼀个函数返回 BINARY(16)类型的UUID值
CREATE FUNCTION uu_id() RETURNS binary(16) RETURN UNHEX(REPLACE(UUID(),'-',''));
或mysql下载32位
CREATE FUNCTION uu_id() RETURNS binary(16) RETURN UNHEX(REVERSE(REPLACE(UUID(),'-','')));
使⽤:
INSERT INTO test_bin(bin_id) VALUES(uu_id());
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论