Postgres drop exclusion constraint - postgresql

I ran this DDL to add an exclusion constraint to a table:
ALTER TABLE recurring_charges
ADD EXCLUDE USING GIST (period WITH &&);
Now I want to remove the constraint - how do I do that? I tried some variations of ALTER TABLE ... DROP EXCLUDE and DROP CONSTRAINT but nothing seems to work.

Figured it out - looks like it generated a name for the constraint that I have to use.
ALTER TABLE recurring_charges
DROP CONSTRAINT recurring_charges_period_excl;
And now I've updated my original DDL to use the full ADD CONSTRAINT syntax so that I can name my constraint rather than relying on automatic naming behavior:
ALTER TABLE recurring_charges
ADD CONSTRAINT recurring_charges_period_excl EXCLUDE USING GIST (period WITH &&);

To add to the above answer, to find out the constraint name, you can find it in psql using:
\d+ tableName

Related

Postgres ALTER TABLE ADD CONSTRAINT IF NOT EXISTS not working

I need to add a Constraint if not exists and am hitting the following error. Note that a similar if not exists for a new Column, right above it, does work. There's some syntax error when adding a Constraint, am I missing something?
alter table requests_t
add constraint if not exists
valid_bias_check CHECK (bias_flag::text = ANY (ARRAY['Y'::character varying::text, 'N'::character varying::text]));
Error
ERROR: syntax error at or near "not"
LINE 2: add constraint if not exists
Since Postgres doesn't support this syntax with constraints (see a_horse_with_no_name's comment), I rewrote it as:
alter table requests_t
drop constraint if exists valid_bias_check;
alter table requests_t
add constraint
valid_bias_check CHECK (bias_flag::text = ANY (ARRAY['Y'::character varying::text, 'N'::character varying::text]));

With Check Option Postgresql

I have an ALTER TABLE statement, written in T-SQL (SQL Server):
ALTER TABLE myTable WITH CHECK ADD CONSTRAINT [FK_myTable_myColumn] FOREIGN KEY(myColumn) REFERENCES otherTable (Column)
If I want to translate this statement in Postgresql, how can I make this? Paying attention to WITH CHECK ADD CONSTRAINT
You need to
remove WITH CHECK - I don't know what this is supposed to do, but you can't have a "check constraint" together with a foreign key constraint in Postgres
use standard compliant identifiers (without the square brackets)
ALTER TABLE my_table
ADD CONSTRAINT fk_mytable_mycolumn
FOREIGN KEY(my_column) REFERENCES other_table (column)

equivalent for enable and disable validate and novalidate in db2

I want to enable constraint but don't validate existing record. only validate future record in db2.
This should be apply for check constraint.
alter table table_name add check (column_name <> '')
DB2 hast the ENFORCED and NOT ENFORCED options for check constraints. The syntax for constraints is part of the CREATE TABLE and ALTER TABLE statements. A sub-option is to either set TRUSTED or NOT TRUSTED depending on the quality of your data.
Something like the following should work:
alter table mytable add check (column1>myvalue) not enforced trusted
try Something like this
ALTER TABLE YOURLIB.YOURTABLE DROP CONSTRAINT YOURLIB.YOURCONSTRAINTNAME1;
ALTER TABLE YOURLIB.YOURTABLE ADD CONSTRAINT YOURLIB.YOURCONSTRAINTNAME1 CHECK (YOUCOLUMNNAME<>'') NOT ENFORCED TRUSTED;

ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]

If I script a table with a foreign key, it looks like this:
GO
ALTER TABLE [dbo].[MyTable] WITH CHECK ADD CONSTRAINT [FK_MyTable_SomeCol] FOREIGN KEY([SomeCol])
REFERENCES [dbo].[MyOtherTable] ([SomeCol])
GO
ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]
GO
What is the second part for (ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol])?
It's an artifact of the way that the constraint is scripted - although it's unnecessary to specify these options (since they're the defaults for new constraints), the same generator can also generate NOCHECK options in exactly the same manner.
Documentation for ALTER TABLE indicates two distinct uses of CHECK/NOCHECK:
WITH CHECK | WITH NOCHECK
Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.
And:
{ CHECK | NOCHECK } CONSTRAINT
Specifies that constraint_name is enabled or disabled.
So one option is saying "check the current contents of the table", the other is saying "Validate new data as it is added".
This is a way of implementing referential integrity for your tables.

T-SQL foreign key check constraint

When you create a foreign key constraint in a table and you create the script in MS SQL Management Studio, it looks like this.
ALTER TABLE T1 WITH CHECK ADD CONSTRAINT FK_T1 FOREIGN KEY(project_id)
REFERENCES T2 (project_id)
GO
ALTER TABLE T1 CHECK CONSTRAINT FK_T1
GO
What I don't understand is what purpose has the second alter with check constraint.
Isn't creating the FK constraint enough? Do you have to add the check constraint to assure reference integrity ?
Another question: how would it look like then when you'd write it directly in the column definition?
CREATE TABLE T1 (
my_column INT NOT NULL CONSTRAINT FK_T1 REFERENCES T2(my_column)
)
Isn't this enough?
First it creates the constraint and here you can specify whether data allready in the table should be checked or not against your new constraint. WITH { CHECK | NOCHECK }
The second part specifies that the constraint is enabled. ALTER TABLE TableName { CHECK | NOCHECK } CONSTRAINT ConstraintName
The second statement is compelled by the "WITH CHECK" in the first statement. There is a setting you can toggle to not do this.