ALTER TABLE .. DROP CONSTRAINT IF EXISTS
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
A popular subclause of DDL statements that jOOQ can usually emulate, is the IF EXISTS
clause:
Dialect support
This example using jOOQ:
alterTable("t").dropConstraintIfExists("c")
Translates to the following dialect specific expressions:
Aurora Postgres, CockroachDB, H2, MariaDB, Postgres
ALTER TABLE t DROP CONSTRAINT IF EXISTS c
BigQuery, ClickHouse
ALTER TABLE t DROP PRIMARY KEY
DB2
BEGIN DECLARE CONTINUE HANDLER FOR SQLSTATE '42703' BEGIN END; DECLARE CONTINUE HANDLER FOR SQLSTATE '42704' BEGIN END; EXECUTE IMMEDIATE ' ALTER TABLE t DROP CONSTRAINT c '; END
Firebird
EXECUTE BLOCK AS BEGIN EXECUTE STATEMENT ' ALTER TABLE t DROP CONSTRAINT c '; WHEN sqlcode -607 DO BEGIN END END
Hana
DO BEGIN DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 257 BEGIN END; DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 260 BEGIN END; DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 397 BEGIN END; EXECUTE IMMEDIATE ' ALTER TABLE t DROP CONSTRAINT c '; END;
Oracle
BEGIN EXECUTE IMMEDIATE ' ALTER TABLE t DROP CONSTRAINT c '; EXCEPTION WHEN others THEN IF sqlerrm LIKE 'ORA-00904%' THEN NULL; ELSIF sqlerrm LIKE 'ORA-02443%' THEN NULL; ELSE RAISE; END IF; END;
SQLServer
BEGIN TRY EXEC sp_executesql N' ALTER TABLE t DROP CONSTRAINT c '; END TRY BEGIN CATCH IF error_number() != 4924 and error_number() != 3728 and error_number() != 3727 THROW; END CATCH
YugabyteDB
DO $$ BEGIN ALTER TABLE t DROP CONSTRAINT c; EXCEPTION WHEN SQLSTATE '42704' THEN NULL; END $$
ASE, Access, Aurora MySQL, Derby, DuckDB, Exasol, HSQLDB, Informix, MemSQL, MySQL, Redshift, SQLDataWarehouse, SQLite, Snowflake, Sybase, Teradata, Trino, Vertica
/* UNSUPPORTED */
Generated with jOOQ 3.20. Translate your own SQL on our website
Feedback
Do you have any feedback about this page? We'd love to hear it!