简体   繁体   English

psql - 如何在不丢弃表的情况下刷新数据库内容

[英]psql - how to flush database content without dropping table

I have a table in my db called 'mytable'. 我的数据库中有一个名为'mytable'的表。 I'd like to clear it so that I can continue to collect and analyze 'fresh data' from it. 我想清除它,以便我可以继续收集和分析它的“新数据”。

Something like 就像是

conn = psycopg2.connect(database = mydb_name, host = mydb_server, user = mydb_uname, password = mydb_pwd)
cur = conn.cursor()
cur.execute("DROP TABLE mytable;")

Isn't going to work for me, because as far as I understand it, this destroys the table. 不会为我工作,因为据我所知,这会摧毁桌子。 I don't want to destroy/re-create... Just to flush all data. 我不想破坏/重新创建......只是为了清除所有数据。

How can I work this out? 我该如何解决这个问题?

 Truncate tablename

Is useful for this, table stays just dropping the data! 对此有用,表只停留数据!

If you have foreign keys you need to use the following 如果您有外键,则需要使用以下内容

 Truncate tablename CASCADE

For many tables do like this 很多桌子都喜欢这样

 Truncate table1, table2, table3

Your example 你的榜样

 Cur.execute("truncate mytable;")

这个SQL查询应该删除表中的所有记录...

DELETE FROM mytable; // not DELETE * FROM mytable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM