0

我希望为我的 SQL Server 数据库创建三个单独的检查约束,以确保密码字段至少约束一个数字、一个大写字符和一个特殊字符。

我认为最好的方法是创建单独的检查约束,例如我创建了以下约束以确保密码长度至少为 8

(len([password]) <= (8))

任何人都可以提出一种方法来建立所需的验证。

谢谢

4

3 回答 3

3

You can do this with one constraint, something like:

check password like '%[0-9]%' and password like '%[A-Z]%' and password like '%[!@#$%a^&*()-_+=.,;:'"`~]%' and len(password) >= 8

Just a note about the upper case comparison: this requires that the collation being used be case sensitive. You may need to specify an explicit COLLATE in the comparison to be sure that it is case sensitive.

于 2014-04-20T15:00:42.070 回答
0

我不直接知道你的问题的答案,但经过一些研究,我发现了这种方法:

1)检查给定的字符串是否包含大写字符

SELECT CASE WHEN BINARY_CHECKSUM([password]) = BINARY_CHECKSUM(LOWER([password])) THEN 0 ELSE 1 END AS DoesContainUpperCase
GO

来源:http ://www.sqlservercentral.com/Forums/Topic800271-338-1.aspx

2) 检查给定的字符串是否包含至少一个特殊字符

我们可以做简单的陈述

IF ([password] LIKE '%[^a-zA-Z0-9]%')

来源:如何检测字符串是否包含特殊字符?

希望我的回答对你有所帮助。亲切的问候,

拉法尔

于 2014-04-20T15:05:30.550 回答
0

我不得不删除特殊字符集合中的“a”并添加上面提到的排序规则。谢谢你的帮助!

([password] like '%[0-9]%' AND [password] like '%[A-Z]%' collate Latin1_General_BIN2 AND [password] like '%[!@#$%^&*()-_+=.,;:~]%' AND len([password])>=(8))
于 2019-08-23T07:18:10.763 回答