表中插入值时,SQl约束中的问题 [英] problem in SQl Constraint at the time inserting values in table

查看:115
本文介绍了表中插入值时,SQl约束中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它创建一个表,并且该表可以正确执行:-
_______________________________________________________

i am create an table using this and it is execute properly:-
_______________________________________________________

CREATE TABLE Persons
(
P_Id int NOT NULL check(p_id between 1 and 100),
LastName varchar(255) NOT NULL check (lastname like'[char,int,char]' ),
FirstName varchar(255),
Address varchar(255)check (address in('uttam nagar','janak puri','vikas puri')),
City varchar(255) not null DEFAULT 'Sandnes'
)


_______________________________________________________
现在我尝试使用以下代码在其中插入值:-
____________________________________________________________


_______________________________________________________
and now i am try to insert value in that using this code:-
____________________________________________________________

insert into persons values(1,'['jha' ,1, 'd']','deepak','delhi',default)



__________________________________________________________
但是它将引发异常,请帮帮我.



__________________________________________________________
but it will throw an exception in that please help me

推荐答案

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/[^]


这篇关于表中插入值时,SQl约束中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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