Re: Dropping all constraints in database

Поиск
Список
Период
Сортировка
От Thomas Kellerer
Тема Re: Dropping all constraints in database
Дата
Msg-id ilknvd$n3t$1@dough.gmane.org
обсуждение исходный текст
Ответ на Dropping all constraints in database  (Lukasz Brodziak <lukasz.brodziak@gmail.com>)
Список pgsql-admin
Lukasz Brodziak, 14.03.2011 10:26:
> Hello,
>
> Is there a way of disabling/dropping all constrainsts in a given
> database? I need to restore a db which has duplicate values in nearly
> half of its tables then remove duplicates and then add the constraints
> back. Is there a way to do that for each table in one
> statement/function? It may be even a java/perl script if it can do
> such a thing. Thank You all in advance for help.
>

Something like this?

DO $body$
DECLARE r record;
BEGIN
    FOR r IN SELECT table_name,constraint_name
             FROM information_schema.constraint_table_usage
    LOOP
       EXECUTE 'ALTER TABLE ' || quote_ident(r.table_name)|| ' DROP CONSTRAINT '|| quote_ident(r.constraint_name) ||
';';
    END LOOP;
END
$body$;


If you are not on 9.x yet, you can simply spool the output of a statement like this:

SELECT 'ALTER TABLE '||table_name||' DROP CONSTRAINT '||constraint_name||';'
FROM information_schema.constraint_table_usage

to a file, and then run that file to drop all constraints.

Regards
Thomas


В списке pgsql-admin по дате отправления:

Предыдущее
От: Lukasz Brodziak
Дата:
Сообщение: Dropping all constraints in database
Следующее
От: Glyn Astill
Дата:
Сообщение: Re: Dropping all constraints in database