问题描述
我有一个表,其中有一些列 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中创建表约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!