X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 06F7AD9778 for ; Mon, 26 Sep 2005 16:31:33 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 74210-09 for ; Mon, 26 Sep 2005 19:31:25 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id AF303D9538 for ; Mon, 26 Sep 2005 16:31:26 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.13.1/8.13.1) with ESMTP id j8QJVPbr078067 for ; Mon, 26 Sep 2005 19:31:25 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.13.1/8.13.1/Submit) id j8QJ43PP072062 for pgsql-sql@postgresql.org; Mon, 26 Sep 2005 19:04:03 GMT (envelope-from news) From: Chris Browne X-Newsgroups: pgsql.sql Subject: Re: Possible to delete record from all tables at the same time? Date: Mon, 26 Sep 2005 14:21:35 -0400 Organization: cbbrowne Computing Inc Lines: 44 Message-ID: <60oe6flmfk.fsf@dba2.int.libertyrms.com> References: <1127754749.860478.171440@g47g2000cwa.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: usenet@news.hub.org User-Agent: Gnus/5.1007 (Gnus v5.10.7) XEmacs/21.4.17 (Jumbo Shrimp, linux) Cancel-Lock: sha1:xBMNIQsARVxV9wAKqXvanJc5Fr8= To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=-2.477 required=5 tests=[ALL_TRUSTED=-2.82, AWL=0.343] X-Spam-Level: X-Archive-Number: 200509/252 X-Sequence-Number: 22799 mwsenecal@yahoo.com writes: > Is it possible to delete a record from all tables in the database at > the same time, without having to execute a separate "DELETE" statement > for each table? > > I have a situation where I need to delete user records from our system. > The user account information is spread across a handful of tables. Each > table has an "id" field that contains the user's id, so the tables do > have one field in common. When purging a user account from the system, > I'd like to be able to issue one command that will delete the user > account information from all tables, rather than issue separate delete > commands for each table, something along the lines of: > > DELETE FROM ALL_TABLES WHERE userId = "whatever"; > > Is this possible? Yes, it is, though not via that mechanism. This would be handled via a set of foreign keys of the "ON DELETE CASCADE" sort. Thus, you'd have one central "user" account, with the id field. A table associating users with privileges might look like the following: CREATE TABLE user_privileges ( privilege_no integer REFERENCES privileges ON DELETE RESTRICT, user_id integer REFERENCES user(id) ON DELETE CASCADE, primary key (privilege_no, user_id) ); Other tables would similarly reference "user(id) ON DELETE CASCADE"; whenever you delete from table user, the corresponding entries in those tables would automatically be deleted. -- (reverse (concatenate 'string "moc.enworbbc" "@" "enworbbc")) http://www.ntlug.org/~cbbrowne/lisp.html Rules of the Evil Overlord #100. "Finally, to keep my subjects permanently locked in a mindless trance, I will provide each of them with free unlimited Internet access.