Can we use "if else" in "Check" constraint in sql server - sql

Can i use if else under a check constraint.
Can i use check constraint using a variable
need xplanation with eg.

Your question is a bit vague. What are you trying to do with the IF...ELSE? Check constraints aren't processed code, they're part of the table definition - there is no control flow and no variables. You can use a user-defined function in check constraints, which may be what you're after, but it's hard to tell from your question.

You cannot use IF/ELSE, but you can use inline conditionals: CASE WHEN

Related

How can I use regexp (or equivalent) in MS SQL Server to solve this problem?

I have searched online and in SO to try and find a solution to my problem but I can't find anyone that has solved this.
When creating a table in MS SQL Server, I have a column called composite_name. I want to add a constraint to this column so that it only accepts values of the form:
COMPOSITE1, COMPOSITE2, ..., COMPOSITE[x], ...
where [x] is any integer.
I have tried to do this by adding the below constraint to my table creation statement:
CONSTRAINT [check_composite_name] CHECK (composite_name LIKE 'COMPOSITE[0-9]+')
the table creates, but then when I insert data that has the form mentioned above I get an error. Clearly something is wrong with the string pattern but I'm not sure how to fix this.
I stress that I am using Microsoft SQL Server so there are no functions like REGEXP_LIKE. I thought I had followed the documentation correctly on this given here.
Any help would be greatly appreciated. Thanks in advance
You can use like and not like:
check (composite like 'COMPOSITE[0-9]%' and
composite not like 'COMPOSITE%[^0-9]%'
)
Note: This may also match lower case, depending on the collation of the column.

Struggling with input validation/check constraint

I'm trying to create a table with an email address column and want to make sure that only addresses in the correct format (contains "#") are allowed. I know how to use the LIKE operator in queries but not how to put a value constraint on a column.
You can add the check constraint like this For your basic example:
alter table t add contraint chk_email
(check (email like '%#%') );
Of course, that is really only a betting. Perhaps something more like this:
alter table t add contraint chk_email
(check (email like '%_#[^.]%' and -- one # and something before and after
email not like '%#%#%' and -- not more than one #
email not like '%[^-.a-zA-Z0-9_]%' -- valid characters)
);
This still will allow invalid emails, but it is at least closer.
This would be better to do in a programming language that supports regular expressions or already has a built in isValidEmail method.
Validating an email address using a regular expression or a simple like pattern is pretty darn hard to get right, if not nearly impossible.
You can read more about it on I Knew How To Validate An Email Address Until I Read The RFC - And to quote the part I think illustrates the problem best:
These are all valid email addresses!
Abc#def#example.com
Fred\ Bloggs#example.com
Joe.\Blow#example.com
"Abc#def"#example.com
"Fred Bloggs"#example.com
customer/department=shipping#example.com
$A12345#example.com
!def!xyz%abc#example.com
_somename#example.com
Attempting to get all the logic needed to validate such a wide range of possibilities in a T-SQL statement is like attempting to climb the Everest blind-folded with your hands tied behind your back.
You could have a simple validation that might return a lot of false-positives or false-negatives using a simple like pattern like the one suggested in How to Validate Email Address in SQL Server? by Pinal Dave: '%_#__%.__%', but that's really just a naive attempt to clog a dam with a band-aid.
Having said all that, you might be able to use a CLR Scalar-Valued Function to validate your email addresses, using code like the one from this SO post.
Personally, I have no experience with CLR functions, so it would probably be irresponsible of me to try and write you a code example (especially since I don't really have a test environment to check it before I post this answer), but I hope what I've written so far was helpful enough, and with the help of the links in this answer and some web searches you will be able to solve the problem.

How to update a constant term of a constraint in Gurobi

I am facing a problem where I must call the Gurobi engine iteratively. Between runs, I have to update the constant term in some of the constraints. I check the manual book in which I find GRBModel::chgCoeff() could be used to change the coefficient of variables in the constraint but there is no function related to changing the constant term. Any idea to do that? Thank you!
I think that you have to set the RHS attribute of the Linear Constraint object. So, if constr is your constraint, something like
constr.Set(GRB.DoubleAttr.RHS, 329);
(this is C#, I guess you use C++, so the case of some of the letters may be different...).

How to make an efficient check constraint for an e-mail field in Firebird

How to make an efficient check constraint for an e-mail field in Firebird (if field value not null)?
thanks, Wilfried
You could use the SIMILAR TO operator to test against regular expression pattern, something like
CHECK(emailfield SIMILAR TO '[[:ALNUM:]._%+-]+#[[:ALNUM:].-]+\.[[:ALPHA:]]+')
IMHO this kind of validation should take place in the application code, and not in the database. Anyway, the suggestion approach with a regular expression seems nice !

SQL CASE statements on Informix - Can you set more than one field in the END section of a case block?

Using IBM Informix Dynamic Server Version 10.00.FC9
I'm looking to set multiple field values with one CASE block. Is this possible? Do I have to re-evaluate the same conditions for each field set?
I was thinking of something along these lines:
SELECT CASE WHEN p.id = 9238 THEN ('string',3) END (varchar_field, int_field);
Where the THEN section would define an 'array' of fields similar to the syntax of
INSERT INTO table (field1,field2) values (value1,value2)
Also, can it be done with a CASE block of an UPDATE statement?
UPDATE TABLE SET (field1,field2) = CASE WHEN p.id=9238 THEN (value1,value2) END;
Normally, I'd ask for the version of Informix that you're using, but it probably doesn't matter much this time. The simple answer is 'No'.
A more complex answer might discuss using a row type constructor, but that probably isn't what you want on the output. And, given the foregoing, then the UPDATE isn't going to work (and would require an extra level of parentheses if it was going to).
No, a CASE statement resolves to an expression (see IBM Informix Guide to SQL: Syntax CASE Expressions) and can be used in places where an expression is permitted. An expression is a single value.
from http://en.wikipedia.org/wiki/Expression_%28programming%29
An expression in a programming
language is a combination of explicit
values, constants, variables,
operators, and functions that are
interpreted according to the
particular rules of precedence and of
association for a particular
programming language, which computes
and then produces (returns, in a
stateful environment) another value.
Found an easy way to do it located here:
how to have listview row colour to change based on data in the row
Solution was just adding the case statement to my sql statement. Just maid my life much easier.