且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

表中插入值时,SQl约束中的问题

更新时间:2023-01-22 17:07:42

Whenever您使用like运算符时,它将在尝试插入时比较字符串.


如果您的检查条件是
WHenever You use like operator, it will compare the strings when trying to insert.


if your check condition is
check (lastname like 'char,int,char' )




那么您只能插入以下值.




then you can insert only the following value.

insert into persons values(1,'char,int,char','deepak','delhi',default)



在上面的插入语句中,由于特殊字符[]而失败.

在检查条件下无法验证这样的数据类型.您必须编写一个函数并将其用作检查约束.为此,您还需要确定何时应该出现整数.

您可以检查以下链接以了解如何实现检查功能.



http://connectsql.blogspot.in/2011/01/sql-server- function-based-check.html [ ^ ]

http://sqljourney.wordpress.com /2010/06/25/check-constraint-with-user-defined-function-in-sql-server/ [



In the above insert statement it fails because of special character [].

Its not possible to validate datatype like this in check condition. You have to write a function and use it as check constraint. For that also you need to fix when should integer comes.

You can check the following links to know how to achieve check through functions.



http://connectsql.blogspot.in/2011/01/sql-server-function-based-check.html[^]

http://sqljourney.wordpress.com/2010/06/25/check-constraint-with-user-defined-function-in-sql-server/[^]