Display names of all constraints for a table in Oracle SQL

You need to query the data dictionary, specifically the USER_CONS_COLUMNS view to see the table columns and corresponding constraints: SELECT * FROM user_cons_columns WHERE table_name=”<your table name>”; FYI, unless you specifically created your table with a lower case name (using double quotes) then the table name will be defaulted to upper case so ensure it … Read more

Postgresql: Conditionally unique constraint

PostgreSQL doesn’t define a partial (i.e. conditional) UNIQUE constraint – however, you can create a partial unique index. PostgreSQL uses unique indexes to implement unique constraints, so the effect is the same, with an important caveat: you can’t perform upserts (ON CONFLICT DO UPDATE) against a unique index like you would against a unique constraint. … Read more

How to drop all user tables?

BEGIN FOR cur_rec IN (SELECT object_name, object_type FROM user_objects WHERE object_type IN (‘TABLE’, ‘VIEW’, ‘MATERIALIZED VIEW’, ‘PACKAGE’, ‘PROCEDURE’, ‘FUNCTION’, ‘SEQUENCE’, ‘SYNONYM’, ‘PACKAGE BODY’ )) LOOP BEGIN IF cur_rec.object_type=”TABLE” THEN EXECUTE IMMEDIATE ‘DROP ‘ || cur_rec.object_type || ‘ “‘ || cur_rec.object_name || ‘” CASCADE CONSTRAINTS’; ELSE EXECUTE IMMEDIATE ‘DROP ‘ || cur_rec.object_type || ‘ “‘ || … Read more

SQL DROP TABLE foreign key constraint

No, this will not drop your table if there are indeed foreign keys referencing it. To get all foreign key relationships referencing your table, you could use this SQL (if you’re on SQL Server 2005 and up): SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id(‘Student’) and if there are any, with this statement here, you … Read more

Oracle find a constraint

select * from all_constraints where owner=”<NAME>” and constraint_name=”SYS_C00381400″ / Like all data dictionary views, this a USER_CONSTRAINTS view if you just want to check your current schema and a DBA_CONSTRAINTS view for administration users. The construction of the constraint name indicates a system generated constraint name. For instance, if we specify NOT NULL in a … Read more