pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedDelete a table automatic?
7+ messages / 7 participants
[nested] [flat]
* Delete a table automatic?
@ 2022-11-01 01:44 黄宁 <huangning0722@gmail.com>
2022-11-01 09:31 ` Re: Delete a table automatic? jian he <jian.universality@gmail.com>
2022-11-01 14:35 ` Re: Delete a table automatic? Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 2 replies; 7+ messages in thread
From: 黄宁 @ 2022-11-01 01:44 UTC (permalink / raw)
To: pgsql-general
I now have two tables named A and B. Table B is calculated based on the
data of table A. I wonder if table B can be automatically deleted when
table A is deleted?
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Delete a table automatic?
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
@ 2022-11-01 09:31 ` jian he <jian.universality@gmail.com>
2022-11-01 13:41 ` Re: Delete a table automatic? Rob Sargent <robjsargent@gmail.com>
1 sibling, 1 reply; 7+ messages in thread
From: jian he @ 2022-11-01 09:31 UTC (permalink / raw)
To: 黄宁 <huangning0722@gmail.com>; +Cc: pgsql-general
On Tue, Nov 1, 2022 at 2:33 PM 黄宁 <huangning0722@gmail.com> wrote:
> I now have two tables named A and B. Table B is calculated based on the
> data of table A. I wonder if table B can be automatically deleted when
> table A is deleted?
>
Your question seems not that specific.
You can use https://dbfiddle.uk/btGcOH30 to showcase your specific
problem/question.
you can use DROP TABLE CASCADE.
DROP TABLE manual:
https://www.postgresql.org/docs/current/sql-droptable.html
--
I recommend David Deutsch's <<The Beginning of Infinity>>
Jian
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Delete a table automatic?
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
2022-11-01 09:31 ` Re: Delete a table automatic? jian he <jian.universality@gmail.com>
@ 2022-11-01 13:41 ` Rob Sargent <robjsargent@gmail.com>
2022-11-01 14:16 ` Re: Delete a table automatic? Peter J. Holzer <hjp-pgsql@hjp.at>
0 siblings, 1 reply; 7+ messages in thread
From: Rob Sargent @ 2022-11-01 13:41 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 11/1/22 03:31, jian he wrote:
>
>
> On Tue, Nov 1, 2022 at 2:33 PM 黄宁 <huangning0722@gmail.com> wrote:
>
> I now have two tables named A and B. Table B is calculated based
> on the data of table A. I wonder if table B can be automatically
> deleted when table A is deleted?
>
>
> Your question seems not that specific.
> You can use https://dbfiddle.uk/btGcOH30 to showcase your specific
> problem/question.
>
> you can use DROP TABLE CASCADE.
> DROP TABLE manual:
> https://www.postgresql.org/docs/current/sql-droptable.html
>
Only If B has a foreign key reference to A
> --
> I recommend David Deutsch's <<The Beginning of Infinity>>
>
> Jian
>
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Delete a table automatic?
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
2022-11-01 09:31 ` Re: Delete a table automatic? jian he <jian.universality@gmail.com>
2022-11-01 13:41 ` Re: Delete a table automatic? Rob Sargent <robjsargent@gmail.com>
@ 2022-11-01 14:16 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-11-01 15:11 ` Re: Delete a table automatic? MuraliPD@GMail <murali.pd@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Peter J. Holzer @ 2022-11-01 14:16 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 2022-11-01 07:41:14 -0600, Rob Sargent wrote:
> On 11/1/22 03:31, jian he wrote:
>
> On Tue, Nov 1, 2022 at 2:33 PM 黄宁 <huangning0722@gmail.com> wrote:
>
> I now have two tables named A and B. Table B is calculated based on the
> data of table A. I wonder if table B can be automatically deleted when
> table A is deleted?
[...]
> you can use DROP TABLE CASCADE.
> DROP TABLE manual: https://www.postgresql.org/docs/current/
> sql-droptable.html
>
>
>
> Only If B has a foreign key reference to A
And even then it only drops the constraint, not the table (or the data):
hjp=> create table a (id serial primary key, t text);
CREATE TABLE
hjp=> create table b (id serial primary key, a int references a, t text);
CREATE TABLE
hjp=> \d a
Table "public.a"
╔════════╤═════════╤═══════════╤══════════╤═══════════════════════════════╗
║ Column │ Type │ Collation │ Nullable │ Default ║
╟────────┼─────────┼───────────┼──────────┼───────────────────────────────╢
║ id │ integer │ │ not null │ nextval('a_id_seq'::regclass) ║
║ t │ text │ │ │ ║
╚════════╧═════════╧═══════════╧══════════╧═══════════════════════════════╝
Indexes:
"a_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "b" CONSTRAINT "b_a_fkey" FOREIGN KEY (a) REFERENCES a(id)
hjp=> \d b
Table "public.b"
╔════════╤═════════╤═══════════╤══════════╤═══════════════════════════════╗
║ Column │ Type │ Collation │ Nullable │ Default ║
╟────────┼─────────┼───────────┼──────────┼───────────────────────────────╢
║ id │ integer │ │ not null │ nextval('b_id_seq'::regclass) ║
║ a │ integer │ │ │ ║
║ t │ text │ │ │ ║
╚════════╧═════════╧═══════════╧══════════╧═══════════════════════════════╝
Indexes:
"b_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"b_a_fkey" FOREIGN KEY (a) REFERENCES a(id)
[some inserts later]
hjp=> select * from b;
╔════╤═══╤══════╗
║ id │ a │ t ║
╟────┼───┼──────╢
║ 1 │ 1 │ foo1 ║
║ 2 │ 1 │ foo2 ║
║ 3 │ 2 │ bar1 ║
╚════╧═══╧══════╝
(3 rows)
hjp=> drop table a cascade;
NOTICE: drop cascades to constraint b_a_fkey on table b
DROP TABLE
hjp=> \d b
Table "public.b"
╔════════╤═════════╤═══════════╤══════════╤═══════════════════════════════╗
║ Column │ Type │ Collation │ Nullable │ Default ║
╟────────┼─────────┼───────────┼──────────┼───────────────────────────────╢
║ id │ integer │ │ not null │ nextval('b_id_seq'::regclass) ║
║ a │ integer │ │ │ ║
║ t │ text │ │ │ ║
╚════════╧═════════╧═══════════╧══════════╧═══════════════════════════════╝
Indexes:
"b_pkey" PRIMARY KEY, btree (id)
As you can see, the table is still there, but the foreign key constraint
is gone.
hjp=> select * from b;
╔════╤═══╤══════╗
║ id │ a │ t ║
╟────┼───┼──────╢
║ 1 │ 1 │ foo1 ║
║ 2 │ 1 │ foo2 ║
║ 3 │ 2 │ bar1 ║
╚════╧═══╧══════╝
(3 rows)
And the data in the table is also unchanged.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Delete a table automatic?
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
2022-11-01 09:31 ` Re: Delete a table automatic? jian he <jian.universality@gmail.com>
2022-11-01 13:41 ` Re: Delete a table automatic? Rob Sargent <robjsargent@gmail.com>
2022-11-01 14:16 ` Re: Delete a table automatic? Peter J. Holzer <hjp-pgsql@hjp.at>
@ 2022-11-01 15:11 ` MuraliPD@GMail <murali.pd@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: MuraliPD@GMail @ 2022-11-01 15:11 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
I too agree with Adrian Klaver, the trigger is the only option
Thanks,
V Muralidharan
+91 9940302900
On Tue, 1 Nov 2022, 19:46 Peter J. Holzer, <hjp-pgsql@hjp.at> wrote:
> On 2022-11-01 07:41:14 -0600, Rob Sargent wrote:
> > On 11/1/22 03:31, jian he wrote:
> >
> > On Tue, Nov 1, 2022 at 2:33 PM 黄宁 <huangning0722@gmail.com> wrote:
> >
> > I now have two tables named A and B. Table B is calculated based
> on the
> > data of table A. I wonder if table B can be automatically
> deleted when
> > table A is deleted?
> [...]
> > you can use DROP TABLE CASCADE.
> > DROP TABLE manual: https://www.postgresql.org/docs/current/
> > sql-droptable.html
> >
> >
> >
> > Only If B has a foreign key reference to A
>
> And even then it only drops the constraint, not the table (or the data):
>
> hjp=> create table a (id serial primary key, t text);
> CREATE TABLE
> hjp=> create table b (id serial primary key, a int references a, t text);
> CREATE TABLE
> hjp=> \d a
> Table "public.a"
> ╔════════╤═════════╤═══════════╤══════════╤═══════════════════════════════╗
> ║ Column │ Type │ Collation │ Nullable │ Default ║
> ╟────────┼─────────┼───────────┼──────────┼───────────────────────────────╢
> ║ id │ integer │ │ not null │ nextval('a_id_seq'::regclass) ║
> ║ t │ text │ │ │ ║
> ╚════════╧═════════╧═══════════╧══════════╧═══════════════════════════════╝
> Indexes:
> "a_pkey" PRIMARY KEY, btree (id)
> Referenced by:
> TABLE "b" CONSTRAINT "b_a_fkey" FOREIGN KEY (a) REFERENCES a(id)
>
> hjp=> \d b
> Table "public.b"
> ╔════════╤═════════╤═══════════╤══════════╤═══════════════════════════════╗
> ║ Column │ Type │ Collation │ Nullable │ Default ║
> ╟────────┼─────────┼───────────┼──────────┼───────────────────────────────╢
> ║ id │ integer │ │ not null │ nextval('b_id_seq'::regclass) ║
> ║ a │ integer │ │ │ ║
> ║ t │ text │ │ │ ║
> ╚════════╧═════════╧═══════════╧══════════╧═══════════════════════════════╝
> Indexes:
> "b_pkey" PRIMARY KEY, btree (id)
> Foreign-key constraints:
> "b_a_fkey" FOREIGN KEY (a) REFERENCES a(id)
>
> [some inserts later]
>
> hjp=> select * from b;
> ╔════╤═══╤══════╗
> ║ id │ a │ t ║
> ╟────┼───┼──────╢
> ║ 1 │ 1 │ foo1 ║
> ║ 2 │ 1 │ foo2 ║
> ║ 3 │ 2 │ bar1 ║
> ╚════╧═══╧══════╝
> (3 rows)
>
> hjp=> drop table a cascade;
> NOTICE: drop cascades to constraint b_a_fkey on table b
> DROP TABLE
>
> hjp=> \d b
> Table "public.b"
> ╔════════╤═════════╤═══════════╤══════════╤═══════════════════════════════╗
> ║ Column │ Type │ Collation │ Nullable │ Default ║
> ╟────────┼─────────┼───────────┼──────────┼───────────────────────────────╢
> ║ id │ integer │ │ not null │ nextval('b_id_seq'::regclass) ║
> ║ a │ integer │ │ │ ║
> ║ t │ text │ │ │ ║
> ╚════════╧═════════╧═══════════╧══════════╧═══════════════════════════════╝
> Indexes:
> "b_pkey" PRIMARY KEY, btree (id)
>
> As you can see, the table is still there, but the foreign key constraint
> is gone.
>
> hjp=> select * from b;
> ╔════╤═══╤══════╗
> ║ id │ a │ t ║
> ╟────┼───┼──────╢
> ║ 1 │ 1 │ foo1 ║
> ║ 2 │ 1 │ foo2 ║
> ║ 3 │ 2 │ bar1 ║
> ╚════╧═══╧══════╝
> (3 rows)
>
> And the data in the table is also unchanged.
>
> hp
>
> --
> _ | Peter J. Holzer | Story must make more sense than reality.
> |_|_) | |
> | | | hjp@hjp.at | -- Charles Stross, "Creative writing
> __/ | http://www.hjp.at/ | challenge!"
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Delete a table automatic?
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
@ 2022-11-01 14:35 ` Adrian Klaver <adrian.klaver@aklaver.com>
2022-11-01 15:23 ` Re: Delete a table automatic? Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 7+ messages in thread
From: Adrian Klaver @ 2022-11-01 14:35 UTC (permalink / raw)
To: 黄宁 <huangning0722@gmail.com>; pgsql-general
On 10/31/22 18:44, 黄宁 wrote:
> I now have two tables named A and B. Table B is calculated based on the
> data of table A. I wonder if table B can be automatically deleted when
> table A is deleted?
The only thing I can think of is a sql_drop event trigger:
https://www.postgresql.org/docs/current/functions-event-triggers.html#PG-EVENT-TRIGGER-SQL-DROP-FUNC...
9.29.2. Processing Objects Dropped by a DDL Command
Though this will not be specific to one table.
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Delete a table automatic?
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
2022-11-01 14:35 ` Re: Delete a table automatic? Adrian Klaver <adrian.klaver@aklaver.com>
@ 2022-11-01 15:23 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 7+ messages in thread
From: Tom Lane @ 2022-11-01 15:23 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: 黄宁 <huangning0722@gmail.com>; pgsql-general
Adrian Klaver <adrian.klaver@aklaver.com> writes:
> On 10/31/22 18:44, 黄宁 wrote:
>> I now have two tables named A and B. Table B is calculated based on the
>> data of table A. I wonder if table B can be automatically deleted when
>> table A is deleted?
> The only thing I can think of is a sql_drop event trigger:
I wonder whether the OP's purposes wouldn't be better served by
making B a view or materialized view, instead of a separate table.
Then the cascaded-drop behavior would be automatic.
regards, tom lane
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2022-11-01 15:23 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-11-01 01:44 Delete a table automatic? 黄宁 <huangning0722@gmail.com>
2022-11-01 09:31 ` jian he <jian.universality@gmail.com>
2022-11-01 13:41 ` Rob Sargent <robjsargent@gmail.com>
2022-11-01 14:16 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2022-11-01 15:11 ` MuraliPD@GMail <murali.pd@gmail.com>
2022-11-01 14:35 ` Adrian Klaver <adrian.klaver@aklaver.com>
2022-11-01 15:23 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox