当前位置:网站首页>Mysql, in the unique index of combination, handles the problem of null value
Mysql, in the unique index of combination, handles the problem of null value
2022-04-22 20:11:00 【bglmmz】
create table TEST
(
col_1 INT,
col_2 INT,
col_3 VARCHAR(128) not null,
col_4 VARCHAR(128) not null,
UNIQUE KEY (col_1, col_2, col_3),
CONSTRAINT FOREIGN KEY (col_1) REFERENCES T2(ID) ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY (col_2) REFERENCES T3(ID) ON DELETE CASCADE
)
comment=' The test table '
ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
The original intention is to require the following :
INSERT INTO TEST (col_1, col_2, col_3, col_4) VALUES (1, NULL, 'TEST', 'VALID'); //OK
INSERT INTO TEST (col_1, col_2, col_3, col_4) VALUES (1, NULL, 'TEST', 'VALID'); //ERROR
But actually MYSQL But you can insert data , No hint of violation of UNIQUE KEY (col_1, col_2, col_3), This is MYSQL Yes NULL Caused by the method of treatment . Although many users are giving MYSQL Mention the N About this BUG, however MYSQL Dead hug SQL specs Don't put , Just don't change or provide alternative solutions .
Is there any way to satisfy my original intention ? Someone has provided such a method :
create table TEST
(
ID INT not null AUTO_INCREMENT,
col_1 INT,
col_2 INT,
col_3 VARCHAR(128) not null,
col_4 VARCHAR(128) not null,
PRIMARY KEY (ID),
CONSTRAINT FOREIGN KEY (col_1) REFERENCES T2(ID) ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY (col_2) REFERENCES T3(ID) ON DELETE CASCADE
)
comment=' The test table '
ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
First , Remove the unique index of the combination , Instead, it adds a self growth PK:ID
secondly , Create a for this table triigger:
CREATE TRIGGER `TEST_BEFORE_INS`
BEFORE INSERT ON `TEST` FOR EACH ROW
BEGIN
SET NEW.id=(SELECT id FROM TEST WHERE col_1<=>NEW.col_1 AND col_2<=>NEW.col_2 AND col_3<=>NEW.col_3);
END
Let's test it again :
INSERT INTO TEST (col_1, col_2, col_3, col_4) VALUES (1, NULL, 'TEST', 'VALID'); //OK
INSERT INTO TEST (col_1, col_2, col_3, col_4) VALUES (1, NULL, 'TEST', 'VALID'); //ERROR:Duplicate entry '1' for key 'PRIMARY'
Report duplicate primary key error , It seems , It basically meets my original needs .
版权声明
本文为[bglmmz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221916328715.html
边栏推荐
- FPGA SEU problem and SEM core
- 微日记:那些看起来并不起眼的细节体验
- 【实习总结】
- PHP 零基础入门笔记(11):字符串 String
- What kind of headphones do you wear for sports? The best equipment for running and listening to music
- 使用ldap客户端创建zimbra ldap用户的格式
- 解决金仓数据库KingbaseES无法打开锁文件的问题
- 【Unity】可玩广告Luna Playable插件的踩坑记录
- Acrobat Pro DC 教程,如何使用密码保护 PDF 文件?
- 为什么说PuddingSwap或许为成为GameFi领域的一匹强势黑马?
猜你喜欢

Open source, free and best used 3 systems and 9 firewall software Amway gives you

嵌入式Web项目(一)——Web服务器的引入

Operation and maintenance (33) centos7 6 deploy kubernetes cluster through kubedm

Advanced IPC - DBUS details

What is the reason why the camera device with built-in 4G card of Haikang cannot register with easycvr platform?

行业趋势远比努力更重要--顶测科技总结

【H5】微信端H5页面制作

sys_ctl启动kingbase单机服务时报错:could not bind IPv4 address “0.0.0.0“: Address already in use

文件上传问题记录

【Leetcode-每日一题】旋转函数
随机推荐
What is browser homology policy?
手写一个网关服务,理解更透彻!
[practice summary]
【Unity】可玩广告Luna Playable插件的踩坑记录
【Leetcode-每日一题】旋转函数
How can I apply for new shares? Is it safe to apply for new shares?
如何提高PHP编程的效率?
The origin explanation and use example of image pre training model
Industry trends are far more important than efforts -- top test technology summary
Comparison and principle summary of golang local cache selection
Micro diary: Those seemingly insignificant details and experiences
Filebeat
2022-01-12 wechat applet debugging
What kind of headphones do you wear for sports? The best equipment for running and listening to music
C语言位域应用--一个字节存八个标志位
Format for creating Zimbra LDAP users using LDAP clients
Which brand of sports headphones is better and recommended for easy-to-use sports headphones
How can the "vanguard" and "Internet Aegis" effectively deploy defense for cities?
What is the reason why the camera device with built-in 4G card of Haikang cannot register with easycvr platform?
The most complete interpretation of redis