在mysql中创建表约束 [英] create table constraint in mysql

查看:192
本文介绍了在mysql中创建表约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中有一些列 a,b,c ,并且每一列都有另一个列,例如 ,y,z),它分别依赖于 a,b,c



x,y,z 将具有值 1 if a,b,c 有任何值,如果 a,b,c有null p>

例如,比如说,
存储在 a 中的值是 2 x 是依赖于它的列。
因此 x 将具有 1 的值。



如果 a 中存储的值为 null ,则 x 将具有值 null



因此有一种方法,我们可以在

解决方案



p>如果 x y z 是简化一些查询,而不是 x y z 作为表格上的列,您也可以考虑使用视图来执行此操作,例如

 将视图创建为
选择a,b,c,
if(isnull(a),null,1)as x,
if(isnull(b),null,1)as y,
if isnull(c),null,1)as z
from mytable;

,然后将其他查询放在此视图上,而不是直接放在表格上。


I have a table in which I have some columns a,b,c and for each column there is another column ,say, (x,y,z) which is dependent on a,b,c respectively.

x,y,z will have value 1 if a,b,c has any value and will contain null if a,b,c has null.

For an example Lets say, The values stored in a is 2 and x is the column dependent on it. So x will have value as 1.

If the values stored in a is null then x will have value as null.

so is there a way in which we can declare this constraint at the time of table creation.

Please suggest anything other than triggers.

解决方案

If the purpose of x, y and z is to simplify some queries then rather than having x, y and z as columns on your table you could also consider using a view to do this e.g.

create view myview as
  select a, b, c,
  if (isnull(a), null, 1) as x,
  if (isnull(b), null, 1) as y,
  if (isnull(c), null, 1) as z
  from mytable;

and then base your other queries on this view instead of directly on the table.

这篇关于在mysql中创建表约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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