psql-如何刷新数据库内容而不删除表 [英] psql - how to flush database content without dropping table

查看:226
本文介绍了psql-如何刷新数据库内容而不删除表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个名为 mytable的表。我想清除它,以便我可以继续从中收集和分析新数据。



类似

  conn = psycopg2.connect(数据库= mydb_name,主机= mydb_server,用户= mydb_uname,密码= mydb_pwd)
cur = conn.cursor()
cur.execute( DROP TABLE mytable;)

对我来说不起作用,因为据我了解,这会破坏桌子。我不想破坏/重新创建...只是刷新所有数据。



我该如何解决?

解决方案

 截断表名

对于此操作很有用,表停留只是删除数据! / p>

如果您有外键,则需要使用以下

 截断tablename CASCADE 

对于许多表,这样做

 截断table1,table2,table3 

您的示例

  Cur.execute( truncate mytable;)


I have a table in my db called '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;")

这篇关于psql-如何刷新数据库内容而不删除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆