如何在 NVARCHAR 列上约束没有空字符串 [英] How to constraint no empty strings on an NVARCHAR column

查看:38
本文介绍了如何在 NVARCHAR 列上约束没有空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 NVARCHAR 列上添加了一个 NOT NULL 约束,因此它不能允许任何空值.但不幸的是,SQL Server 将 NULL 和空值作为两个单独的值处理!

I add a NOT NULL constraint on my NVARCHAR column so it couldn't allow any empty values. but unfortunately SQL Server deals with NULL and empty values as two separate values!

那么如何在不向列中插入任何内容时让服务器抛出异常?

So how do I make the server throw an exception when inserting nothing to the column?

我正在考虑使用约束 CHECK 但我没有找到任何样本与 NVARCHAR 列一起使用时!

I'm thinking using the constraint CHECK but I didn't find any samples when used with NVARCHAR columns!

推荐答案

您可以添加检查约束以确保字符串不为空.

You could add a check constraint that ensures that the string isn't empty.

CREATE TABLE [dbo].[Foo](
    [bar] [nvarchar](50) NOT NULL
)

ALTER TABLE [dbo].[Foo] WITH CHECK 
ADD  CONSTRAINT [CK_Foo] CHECK  (([bar]<>N''))

这篇关于如何在 NVARCHAR 列上约束没有空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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