Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何根据另一列中的数据添加NULL与NOT NULL约束?_Sql_Sql Server - Fatal编程技术网

Sql 如何根据另一列中的数据添加NULL与NOT NULL约束?

Sql 如何根据另一列中的数据添加NULL与NOT NULL约束?,sql,sql-server,Sql,Sql Server,对于我的学期项目,我们正在创建自己的数据库。我目前正在创建数据库和表。我正在Microsoft SQL Server Management Studio中使用SQL 如果在类型列中选择了Lot/Land(如果选择了任何其他选项,则需要不为空),则我只想使列Beds、Baths、Sqft为空 解决方案需要在ALTER函数中,因为我现在正在创建表 提前谢谢 您需要对以下内容进行单独的检查约束: CREATE TABLE Property ( PropertyID int IDENTITY(1

对于我的学期项目,我们正在创建自己的数据库。我目前正在创建数据库和表。我正在Microsoft SQL Server Management Studio中使用SQL

如果在类型列中选择了
Lot/Land
(如果选择了任何其他选项,则需要不为空),则我只想使列
Beds、Baths、Sqft
为空

解决方案需要在ALTER函数中,因为我现在正在创建表


提前谢谢

您需要对以下内容进行单独的
检查
约束:

CREATE TABLE Property (
    PropertyID int IDENTITY(1,1) PRIMARY KEY,
    StreetAddress1 varchar(n) NOT NULL,
    StreetAddress2 varchar(n) NULL,
    City varchar(n) NOT NULL,
    State varchar(2) NOT NULL,
    Type varchar(32) NOT NULL CHECK (Type IN 'Single Family', 'Apartment', 'Condo', 'Townhome', 'Manufactured', 'Lot/Land'),
    Beds varchar(2) NULL,
    Baths decimal(3,1) NULL,
    Sqft varchar(5) NULL,
    Acreage decimal(10,2) NOT NULL
    constraint chk_property_lotland
        check ( (type = 'Lot/Land' and Beds is null and Baths is null and Sqft is null) or
                (type <> 'Lot/Land' and Beds is not null and Baths is not null and Sqft is not null) 
              )               
);
创建表属性(
PropertyID int IDENTITY(1,1)主键,
StreetAddress1 varchar(n)不为空,
StreetAddress2 varchar(n)NULL,
城市varchar(n)不为空,
状态varchar(2)不为空,
输入varchar(32)非空检查(输入“单户”、“公寓”、“公寓”、“联排别墅”、“制造”、“地块/土地”),
varchar(2)为空,
巴斯十进制(3,1)空,
Sqft varchar(5)空,
面积小数(10,2)不为空
约束chk_财产_地块
检查((类型=‘地块/土地’,床为空,浴室为空,Sqft为空)或
(类型“地块/土地”和床不为空,浴室不为空,Sqft不为空)
)               
);

您希望对此设置单独的
检查
约束:

CREATE TABLE Property (
    PropertyID int IDENTITY(1,1) PRIMARY KEY,
    StreetAddress1 varchar(n) NOT NULL,
    StreetAddress2 varchar(n) NULL,
    City varchar(n) NOT NULL,
    State varchar(2) NOT NULL,
    Type varchar(32) NOT NULL CHECK (Type IN 'Single Family', 'Apartment', 'Condo', 'Townhome', 'Manufactured', 'Lot/Land'),
    Beds varchar(2) NULL,
    Baths decimal(3,1) NULL,
    Sqft varchar(5) NULL,
    Acreage decimal(10,2) NOT NULL
    constraint chk_property_lotland
        check ( (type = 'Lot/Land' and Beds is null and Baths is null and Sqft is null) or
                (type <> 'Lot/Land' and Beds is not null and Baths is not null and Sqft is not null) 
              )               
);
创建表属性(
PropertyID int IDENTITY(1,1)主键,
StreetAddress1 varchar(n)不为空,
StreetAddress2 varchar(n)NULL,
城市varchar(n)不为空,
状态varchar(2)不为空,
输入varchar(32)非空检查(输入“单户”、“公寓”、“公寓”、“联排别墅”、“制造”、“地块/土地”),
varchar(2)为空,
巴斯十进制(3,1)空,
Sqft varchar(5)空,
面积小数(10,2)不为空
约束chk_财产_地块
检查((类型=‘地块/土地’,床为空,浴室为空,Sqft为空)或
(类型“地块/土地”和床不为空,浴室不为空,Sqft不为空)
)               
);

1我不明白为什么这会被否决。OP已经解释了这个问题,似乎已经做出了尝试。不仅如此,这是一个合理的问题。我个人不明白为什么这会被否决。OP已经解释了这个问题,似乎已经做出了尝试。不仅如此,这是一个合理的问题。