SQL CHECK CONSTRAINT 拒绝字母数字和特殊章程,只允许从 0 到 9 的数字
SQL CHECK CONSTRAINT to reject alphanumeric and special charters and allow only numeric from 0 to 9
我有以下约束,应该只允许插入从 0 到 9 的数字,而不是任何特殊字符和字母字符。但事实并非如此,例如使用此更新语句时:
update MyDB.dbo.MyTable
set MyTestPhoneExt = '23&' where ID = 1;
目标是确保源数据和写入 MyTable 的数据只有数字,但 MyTestPhoneExt 字段必须为 VARCHAR(15) NULL。
ALTER TABLE MyDB.dbo.MyTable WITH CHECK ADD CONSTRAINT [CHK_MyDB_MyTable _MyTestPhoneExt]
CHECK ((MyTestPhoneExt IS NULL OR LEN(MyTestPhoneExt)>=(1) AND
LEN(MyTestPhoneExt)<=(15) AND MyTestPhoneExt LIKE '%[0-9]%'
--OR len(MyTestPhoneExt)>=(1) AND len(MyTestPhoneExt)<=(15)
AND NOT MyTestPhoneExt LIKE '%[a-zA-Z]%'
AND NOT (MyTestPhoneExt=' ' OR MyTestPhoneExt='' OR MyTestPhoneExt='&' OR
MyTestPhoneExt='`' OR MyTestPhoneExt='~' OR MyTestPhoneExt='>' OR
MyTestPhoneExt='<' OR MyTestPhoneExt='.' OR MyTestPhoneExt=',' OR
MyTestPhoneExt=';' OR MyTestPhoneExt=':' OR MyTestPhoneExt='?' OR
MyTestPhoneExt='_' OR MyTestPhoneExt='=' OR MyTestPhoneExt='+' OR
MyTestPhoneExt='!' OR MyTestPhoneExt='@' OR MyTestPhoneExt='#' OR
MyTestPhoneExt='%' OR MyTestPhoneExt='$' OR MyTestPhoneExt='^' OR
MyTestPhoneExt='*' OR MyTestPhoneExt=',' OR MyTestPhoneExt='}' OR
MyTestPhoneExt='{' OR MyTestPhoneExt=')' OR MyTestPhoneExt='(' OR
MyTestPhoneExt=']' OR MyTestPhoneExt='[' OR MyTestPhoneExt='|' OR
MyTestPhoneExt='\' OR MyTestPhoneExt='/' OR MyTestPhoneExt='-' OR MyTestPhoneExt='@')))
要仅允许数值,您可以使用带有 check 约束
的 TRY_CONVERT()
函数
ALTER TABLE table
ADD CONSTRAINT CHK_MyDB_MyTable _MyTestPhoneExt
CHECK(TRY_CONVERT(BIGINT, MyTestPhoneExt) IS NOT NULL)
你也可以使用系统函数ISNUMERIC()
ALTER TABLE table
ADD CONSTRAINT CHK_MyDB_MyTable _MyTestPhoneExt
CHECK(ISNUMERIC(MyTestPhoneExt)=1)
这不是更简单的只接受数字的方法吗?
patindex('%[^0-9]%', MyTestPhoneExt) = 0
尝试在 CHECK CONSTRAINT
中使用 PATINDEX
CREATE TABLE Mytable
(
MyCol NVARCHAR(50) CHECK(PATINDEX('%[^0-9]%',MyCol)=0 AND ISNUMERIC(MyCol) = 1)
)
INSERT INTO Mytable
(
MyCol
)
VALUES(1),(2)
INSERT INTO Mytable
(
MyCol
)
VALUES('1B'),('2A')
INSERT INTO Mytable
(
MyCol
)
VALUES('1.0'),('2.5')
INSERT INTO Mytable
(
MyCol
)
VALUES('1 '),('2 x')
SELECT
*
FROM Mytable
我有以下约束,应该只允许插入从 0 到 9 的数字,而不是任何特殊字符和字母字符。但事实并非如此,例如使用此更新语句时:
update MyDB.dbo.MyTable
set MyTestPhoneExt = '23&' where ID = 1;
目标是确保源数据和写入 MyTable 的数据只有数字,但 MyTestPhoneExt 字段必须为 VARCHAR(15) NULL。
ALTER TABLE MyDB.dbo.MyTable WITH CHECK ADD CONSTRAINT [CHK_MyDB_MyTable _MyTestPhoneExt]
CHECK ((MyTestPhoneExt IS NULL OR LEN(MyTestPhoneExt)>=(1) AND
LEN(MyTestPhoneExt)<=(15) AND MyTestPhoneExt LIKE '%[0-9]%'
--OR len(MyTestPhoneExt)>=(1) AND len(MyTestPhoneExt)<=(15)
AND NOT MyTestPhoneExt LIKE '%[a-zA-Z]%'
AND NOT (MyTestPhoneExt=' ' OR MyTestPhoneExt='' OR MyTestPhoneExt='&' OR
MyTestPhoneExt='`' OR MyTestPhoneExt='~' OR MyTestPhoneExt='>' OR
MyTestPhoneExt='<' OR MyTestPhoneExt='.' OR MyTestPhoneExt=',' OR
MyTestPhoneExt=';' OR MyTestPhoneExt=':' OR MyTestPhoneExt='?' OR
MyTestPhoneExt='_' OR MyTestPhoneExt='=' OR MyTestPhoneExt='+' OR
MyTestPhoneExt='!' OR MyTestPhoneExt='@' OR MyTestPhoneExt='#' OR
MyTestPhoneExt='%' OR MyTestPhoneExt='$' OR MyTestPhoneExt='^' OR
MyTestPhoneExt='*' OR MyTestPhoneExt=',' OR MyTestPhoneExt='}' OR
MyTestPhoneExt='{' OR MyTestPhoneExt=')' OR MyTestPhoneExt='(' OR
MyTestPhoneExt=']' OR MyTestPhoneExt='[' OR MyTestPhoneExt='|' OR
MyTestPhoneExt='\' OR MyTestPhoneExt='/' OR MyTestPhoneExt='-' OR MyTestPhoneExt='@')))
要仅允许数值,您可以使用带有 check 约束
的TRY_CONVERT()
函数
ALTER TABLE table
ADD CONSTRAINT CHK_MyDB_MyTable _MyTestPhoneExt
CHECK(TRY_CONVERT(BIGINT, MyTestPhoneExt) IS NOT NULL)
你也可以使用系统函数ISNUMERIC()
ALTER TABLE table
ADD CONSTRAINT CHK_MyDB_MyTable _MyTestPhoneExt
CHECK(ISNUMERIC(MyTestPhoneExt)=1)
这不是更简单的只接受数字的方法吗?
patindex('%[^0-9]%', MyTestPhoneExt) = 0
尝试在 CHECK CONSTRAINT
PATINDEX
CREATE TABLE Mytable
(
MyCol NVARCHAR(50) CHECK(PATINDEX('%[^0-9]%',MyCol)=0 AND ISNUMERIC(MyCol) = 1)
)
INSERT INTO Mytable
(
MyCol
)
VALUES(1),(2)
INSERT INTO Mytable
(
MyCol
)
VALUES('1B'),('2A')
INSERT INTO Mytable
(
MyCol
)
VALUES('1.0'),('2.5')
INSERT INTO Mytable
(
MyCol
)
VALUES('1 '),('2 x')
SELECT
*
FROM Mytable