How to create CHECK constraints in Informix? - sql

I need to create validations in my fields (columns) in Informix tables. Inside SQL Server, the names are CHECK (for example: CHECK (Age>=18))
How to create in Informix, or, what's the similar syntax in Informix?

If you want add check constraint you could do it in two ways:
1) The next example adds a new unit_price column to the items table and includes a check constraint to ensure that the entered value is greater than 0:
ALTER TABLE items
ADD (unit_price MONEY (6,2) CHECK (unit_price > 0));
2) To create a constraint that checks values in more than one column, use the ADD CONSTRAINT clause. The following example builds a constraint on the column that was added in the previous example. The check constraint now spans two columns in the table.
ALTER TABLE items ADD CONSTRAINT CHECK (unit_price < total_price);
Have a look at the doc

Related

Adding constraint to check if the value contains specific characters- SQL Server

My Check constraint conflicts when adding values, any answer to why it is wrong will be much appreciated.
Here is the question:
Ensure that the Student’s NIC number contains 9 digits (0-9) and one
character which is “V” or “v”.
Here is the value I want to add to the table:
946785467v
Here is the constraint I used:
ALTER TABLE Student
ADD CONSTRAINT stdNIC CHECK(NIC LIKE '[0-9]{9}[V-v]');
Better to change the constraint as below.
ALTER TABLE Student ADD CONSTRAINT stdNIC CHECK(NIC LIKE '%[0-9]%[V-v]')
Example validated on db<>fiddle<>example

Using a 'not in' query inside of a check constraint in SQL

I have two tables, one of student and one of staff that look as such:
create table student (
id int not null primary key
)
create table staff (
id int not null primary key
)
I want the id in each to be unique. I know this isn't how it should be in production but I'm just trying to see why my check constraint doesn't work and I'm using a simpler example to explain.
I then alter the tables to include the check as follows:
alter table student add constraint not_staff check (id not in (select id from staff))
alter table staff add constraint not_student check (id not in (select id from student))
These checks seem to be invalid.
My question is whether we're allowed to have these kinds of SQL statements inside of a check constraint. If so, why is the above constraint invalid and how would I go about fixing it.
Thanks!
You can't use queries in a check constraint in Db2. Refer to the description of the CREATE TABLE statement.
CHECK (check-condition)
Defines a check constraint. The search-condition must be true or unknown for every row of the table.
search-condition
The search-condition has the following restrictions:
...
The search-condition cannot contain any of the following (SQLSTATE 42621):
Subqueries
The easiest way to achieve your goal is not to create constraints, but create a sequence and use it in before triggers on both tables.

SQL constraint to exclude certain values?

Is there an sql constraint that enables you to disallow certain values in a column?
e.g.
ALTER TABLE foo
ADD COLUMN code TEXT NOT NULL UNIQUE DISALLOW '<GENERATE>'
;
s.t. <GENERATE> could be used safely in the application to indicate that the value should be generated before persisting without risking its accidentally being pushed to DB?
You need a check constraint:
ALTER TABLE foo
ADD COLUMN code TEXT NOT NULL UNIQUE;
alter table foo
add constraint disallow_generate
check (code <> '<GENERATE>');
If you want to disallowed multiple values, use a NOT IN condition:
alter table foo
add constraint disallow_generate
check (code not in ('<GENERATE>', '<GENERATED>', 'foo');
You could use Check Constraints.
eg:
ALTER TABLE foo
ADD COLUMN code TEXT NOT NULL UNIQUE CHECK (code <> '<GENERATE>');

Can´t add field without foreign key

I have two database one local and other in production.
In one of them I have column IdNivelDominio without FK but if I open Constraints folder and I have something like:
DF_EvaluacionDetalleCompetenciasFuncionales_IdNivelDominio
So I want to reply this field into my another database as:
ALTER TABLE Reclutamiento.EvaluacionDetalleCompetenciasFuncionales
ADD IdNivelDominio INT NOT NULL;
But I get error:
ALTER TABLE only allows columns to be added that can contain nulls, or
have a DEFAULT definition specified, or the column being added is an
identity or timestamp column, or alternatively if none of the previous
conditions are satisfied the table must be empty to allow addition of
this column. Column 'IdNivelDominio' cannot be added to non-empty
table 'EvaluacionDetalleCompetenciasFuncionales' because it does not
satisfy these conditions.
Problem is that field is not linked with a foreign key (into original table) so I can´t add constraint.
Can anyone explain me how it occurs? or there any way to do constraint without foreign key? Regards
ALTER TABLE Reclutamiento.EvaluacionDetalleCompetenciasFuncionales
ADD IdNivelDominio INT NOT NULL DEFAULT 0;
You're trying to add a new column that should be NOT NULL to table with data. You need to specify what value will be in that new column because it's NOT NULL

SQL: Create new column with default, unique value

I have added a new column, called Ordinal, to a table called Activity. The problem is that I gave it a UNIQUE constraint, set it to allow NULL (though this I won't want in the end.. I just needed to set it to that to get a little farther with the script), and did not give it a default value. I'm now running a RedGate SQL Compare script that was generated by comparing this table to a version of the Activity table that does not have the column. But I'm getting the following error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'iwt.Activity' and the index name 'IX_Activity'. The duplicate key value is (1).
So based on my research, it's trying to create a unique key constraint on the Ordinal column, but NULL is not unique. So my next step was to give it a unique value of 1 just to let the script pass. But 1 isn't going to be unique either. So, finally, my question:
Preferably in SQL Server Management Studio, how do I set a column as having a unique default value? Isn't that what I would need to create this constraint?
Thanks.
try this:
NULL will be the first constraint when you create the column.
UNIQUE will be as add constraint, you should add the second constraint.
they can run on this order with no problem (tested):
--first constraint
alter table Table_Name
add Column_Name int null
--second constraint
alter table Table_Name
add constraint Constraint_Name unique (Column_Name)
In my example :
PaymentGatewayHash is column
Cart is a table
--first query
alter table Cart
add PaymentGatewayHash NVARCHAR(20) null
--second query
CREATE UNIQUE INDEX PaymentGatewayHashUnique
ON Cart (PaymentGatewayHash)
WHERE PaymentGatewayHash IS NOT NULL
I just tested that :D