如何在 SQL Server 中的 VARCHAR 列上创建 CHECK 约束以指定最小数据长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4426005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:33:19  来源:igfitidea点击:

How can I create a CHECK constraint on a VARCHAR column in SQL Server specifying a minimum data length?

sqlsql-servertsqlcheck-constraints

提问by Jake Petroules

I have a VARCHAR(30)column in a Microsoft SQL Server database. I'd like to add a CHECK constraint that does not allow a value in the column to be less than 3 characters. What is the expression I must use?

VARCHAR(30)在 Microsoft SQL Server 数据库中有一个列。我想添加一个不允许列中的值少于 3 个字符的 CHECK 约束。我必须使用的表达方式是什么?

回答by OMG Ponies

use:

用:

ALTER TABLE [dbo].[YOUR_TABLE]
ADD CONSTRAINT [MinLengthConstraint] CHECK (DATALENGTH([your_column]) > 2)

Reference:

参考: