postgresql 丢弃序列和级联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10001735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:24:53  来源:igfitidea点击:

Drop sequence and cascade

postgresql

提问by Chris Koston

I would like to drop the sequence used in table and the table itself in one statement using CASCADE, but I'm getting NOTICE and table is not dropped. For example:

我想使用 CASCADE 在一个语句中删除 table 中使用的序列和 table 本身,但是我收到 NOTICE 并且 table 没有删除。例如:

CREATE SEQUENCE seq1;
CREATE TABLE t1 (f1 INT NOT NULL DEFAULT nextval('seq1'));

And then when I do:

然后当我这样做时:

DROP SEQUENCE seq1 CASCADE;

I get following message, and the table is not dropped:

我收到以下消息,并且表没有删除:

NOTICE:  drop cascades to default for table t1 column f1

I'm definitely doing something wrong but these are my very first steps in PostgreSQL.

我肯定做错了什么,但这些是我在 PostgreSQL 中的第一步。

回答by Erwin Brandstetter

You have a misconception about dependencies. The table never is a depending object of an associated sequence and is neverdropped by a

您对依赖项有误解。该表永远不会是关联序列的依赖对象,并且永远不会被删除

DROP SEQUENCE ... CASCADE;

Only a DEFAULT value drawing from the sequence "depends" on the sequence and is set to NULL if the sequence is deleted with CASCADE.

只有从序列中抽取的 DEFAULT 值“取决于”序列,并且如果使用 删除序列,则将其设置为 NULL CASCADE

It is the other way round: if the sequence is owned by a table column it is dropped with a

反之亦然:如果序列由表列拥有,则它会被删除

DROP TABLE f1 CASCADE;

For a sequence to be owned by a table column you can either use the serialtype as Milen already suggested. Or you can ALTER an existing sequence:

对于表列拥有的序列,您可以使用serialMilen 已经建议的类型。或者您可以更改现有序列

ALTER SEQUENCE seq1 OWNED BY t1.f1;

回答by Milen A. Radev

I don't know why are you creating a sequence manually - maybe you have justification, or maybe it's due to habits working with another DBMS.

我不知道你为什么要手动创建一个序列——也许你有理由,或者可能是由于使用另一个 DBMS 的习惯。

But if you don't have a special need for it, use the SERIALpseudo-type and when you drop the table the sequence(s) behind the SERIALcolumn(s) will be dropped too.

但是如果您没有特殊需要,请使用SERIAL伪类型,当您删除表时,SERIAL列后面的序列也将被删除。

回答by kgrittn

You asked to drop the sequenceand cascade that action. While the defaultcan't exist without the sequence, and it is therefore dropped, the table and the column canexist without the sequence, so they remain.

您要求删除序列并级联该操作。虽然默认值在没有序列的情况下无法存在,因此被删除,但表和列可以在没有序列的情况下存在,因此它们仍然存在。

With the way you've specified this, dropping the table will not drop the sequence, although you can make the sequence depend on the column with which it is used, and therefore have itdrop automatically if you drop the table. You can do this by altering the owner of the sequence, or use SERIAL instead. Declaring a column to be type SERIAL automatically creates a sequence, makes it generate a default for the column, and makes that column the owner of the sequence.

按照您指定的方式,删除 table 不会删除序列,尽管您可以使序列取决于使用它的列,因此如果您删除table ,会自动删除。您可以通过更改序列的所有者来执行此操作,或者改用 SERIAL。将列声明为 SERIAL 类型会自动创建一个序列,使其生成该列的默认值,并使该列成为该序列的所有者。

回答by SR_Mehta

drop cascade table table_name; you can also use this... and i will also recommend you to use a serial with primary key so that you can identify a column uniquely..

删除级联表 table_name; 您也可以使用它...我还建议您使用带有主键的序列号,以便您可以唯一地标识一列..