MySQL:限制两列,因此其中一列始终为NULL [英] MySQL: Constraint two columns so one of them always are NULL

查看:169
本文介绍了MySQL:限制两列,因此其中一列始终为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,该表的引用是从其两个列到另外两个表PK的引用.有什么办法可以限制这两个列都没有设置值?

I have a table with references from two of it's columns to two other tables PK's. Is there a way I can restrict both of those columns from having values set?

我只希望其中一个设置值,而另一个为NULL

I only want one of them to have a value set, while the other is NULL

收藏夹:

id
table_1_id
table_2_id

表_1:

id

表_2:

id

推荐答案

在SQL中,理想情况下,您可以使用检查约束.

In SQL, you'd ideally handle this with a CHECK constraint.

在MySQL中,没有直接的机制来施加这种CHECK约束.如果尝试包含一个,则

In MySQL, there is not a direct mechanism to impose such a CHECK constraint. If you try to include one, the MySQL Reference Manual states (since it's part of the standard):

CHECK子句被解析,但被所有存储引擎忽略.

The CHECK clause is parsed but ignored by all storage engines.

这篇关于MySQL:限制两列,因此其中一列始终为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆