PostgreSQL: Script to Drop Foreign Keys
PostgreSQL: Script to Drop Foreign Keys
Sometimes you may need to drop foreign key constraints while restructuring tables. PostgreSQL allows you to do this easily.
Drop a Specific Foreign Key
ALTER TABLE orders
DROP CONSTRAINT orders_customer_id_fkey;
Drop All Foreign Keys in a Schema
SELECT 'ALTER TABLE ' || tc.table_name ||
' DROP CONSTRAINT ' || tc.constraint_name || ';'
FROM information_schema.table_constraints tc
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema = 'public';
Run the generated statements to remove all foreign keys.
Comments
Post a Comment