Check constraint on table in liquibase - liquibase

I want to create a check constraint on a table using liquibase, this is the check constaint :
alter table userprefs add constraint chk_null CHECK (updatedate IS NOT NULL OR updateuser IS NOT NULL);
I googled about it but all I can find is how to create the check constraint on a column.
How this is can be done on liquibase ?

Liquibase does not support check constraints "natively". You need to put that into a <sql> tag:
<changeSet author="ichigo" id="1">
<sql>
alter table userprefs add constraint chk_null
CHECK (updatedate IS NOT NULL OR updateuser IS NOT NULL);
</sql>
</changeSet>

Related

Liquibase - change on delete type of foreign key for a table

Is it possible to change the foreign key on delete method from NO ACTION to SET NULL in liquibase?
Yes, it's possible. There's onDelete attribute in <addForeignKeyConstraint> which you can set to:
CASCADE
SET NULL
SET DEFAULT
RESTRICT
NO ACTION
If you already have a foreign key, then you can drop existing constraint and recreate it with the settings you need.
<changeSet id="changeset-id" author="changeset-author">
<preConditions onFail="MARK_RAN">
<foreignKeyConstraintExists foreignKeyName="fk_foo_bar"/>
</preConditions>
<dropForeignKeyConstraint baseTableName="table_foo" constraintName="fk_foo_bar"/>
<addForeignKeyConstraint baseTableName="table_foo" baseColumnNames="foo_col"
constraintName="fk_foo_bar"
referencedTableName="table_bar" referencedColumnNames="bar_col"
onDelete="SET NULL"/>
</changeSet>

Altering constraint in Derby

I don't understand the logic in Derby syntax.
I'm new to SQL, I'm learning now.
I needed to alter a column constraint. I needed to add "not null".
Since I already did an alter on a column for the constraint unique, I thought the syntax will be the same since they are both constraints.
I finally found for not null this link Cannot alter column in existing table in java Derby database
and as I say the same syntax doesn't work for adding a unique constraint to the same column, so my question is: how is the syntax changed for each constraint?
This works:
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME NOT NULL;
So why does this not work?
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME unique;
And why does this work:
alter table customer add constraint cu1 unique (cust_name);
but this does not:
alter table customer add constraint not null (cust_name);

Check if the unique constraint exists and drop it using liquibase

I have a changeset wherein I initially check if the unique constraint exists and then if it does it will drop the constraint.
<changeSet author="loren"
id="DROP_UNIQUE_CONSTRAINT_RULEPRIORITY_ORACLE_v1" dbms="oracle">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">
SELECT COUNT(*)
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_NAME='UC_RULES_PRIORITY'
</sqlCheck>
</preConditions>
<dropUniqueConstraint constraintName="UC_RULES_PRIORITY"
schemaName="${main.schema}"
tableName="RULES"/>
</changeSet>
The issue with this is it does not seem to pass the precondition. It always says MARK_RAN meaning there was no constraint found. In turn, the constraint will never be dropped.
I have tried executing the SELECT statement in my db and it returns 1.
Is this the correct way or is there an alternate solution for this?

Check constraint on existing column with PostgresQL

I'm trying to put check constraint on existing column.
Is there any way to achieve this from PostgreSQL?
Use alter table to add a new constraint:
alter table foo
add constraint check_positive check (the_column > 0);
More details and examples are in the manual:
http://www.postgresql.org/docs/current/static/sql-altertable.html#AEN70043
Edit
Checking for specific values is done in the same way, by using an IN operator:
alter table foo
add constraint check_positive check (some_code in ('A','B'));
If you are okay with (or want) Postgres to generate a constraint name you can use the following shorthand syntax.
ALTER TABLE foo
ADD CHECK (column_1 > 2);
You can add a new constraint with with alter table command. From documentation this example:
ALTER TABLE distributors
ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
You will have to replace constraint name as well as table name and content by your local requirements.
This should do it:
create table test (
id serial
);
alter table test
add constraint id_not_null
check (id is not null);

How to add a default value to an already existing column?

I have an existing column in my SQL Server database. I have tried about everything I can think of but can not get a default value to be added to the column. What works in every other database is
alter table mytable
alter column mycolumn set default(now()) --mycolumn is a datetime
How do I do this in SQL Server?
The error I get for that exact syntax is incorrect syntax near the keyword 'set'
Use:
ALTER TABLE dbo.mytable
ADD CONSTRAINT def_mycolumn DEFAULT GETDATE() FOR mycolumn
For more info, see: Working with Default Constraints
If you want to change the default value of an already existing column. You need to first drop the constraint and then add the constraint all over again as below
ALTER TABLE <TABLE>
DROP CONSTRAINT <CONSTRAINT NAME>
ALTER TABLE <TABLE>
ADD CONSTRAINT <CONSTRAINT NAME> DEFAULT <VALUE> for <COLUMN>
If you are not having the Constraint details of the table, you can use the below query
sp_helpconstraint <TABLE>