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?
Related
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>
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>
I need to alter an existing foreign key from "on delete restrict" to "on delete cascade". Unfortunaltey this bug sneaked through Q/A.
In my database I have several forign key relationships that were automatically named (INTEG_1, INTEG_2, ...). The name of the constraint I have to fix is another in a new installation than in an Update from Version 2 and even another than when this Version 2 previously has been updated from Version 1.
As the referencing table only has one foreign key, this statement gives me the name of the constraint:
SELECT RDB$CONSTRAINT_NAME
FROM RDB$RELATION_CONSTRAINTS
where RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
and RDB$RELATION_NAME = 'MY_TABLE_NAME'
then I can drop and afterwards recreate the foreign key (with a "real" name this time)
alter table MY_TABLE_NAME
drop constraint <result from above>;
alter table MY_TABLE_NAME
add constraint fk_my_table_name_purpose foreign key (other_id)
references other_table(id) on delete cascade;
However, I try to avoid working directly with system tables and I'd like to know whether there is a better / more elegant way to alter my foreign key.
There's no better way, the system tables are the only way to figure out the constraint name.
Should the second ALTER TABLE CHECK CONSTRAINT be in an IF (NOT) EXISTS so that the script can be executed repeatedly?
IF NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_NAME ='fk_RoleId'
)
BEGIN
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[webpages_Roles] ([RoleId])
END
GO
-- Put me in an IF ... () BEGIN ... END?
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_RoleId]
GO
It doesn't cause any errors, but if it should be in an IF clause, how do you test if it has been ran (what can the if contain)?
If you do want a check (it's not necessary), sys.foreign_keys has a column is_not_trusted:
FOREIGN KEY constraint has not been verified by the system.
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>