如何清空SQL数据库? [英] How to empty a SQL database?

查看:255
本文介绍了如何清空SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一种简单的方法来删除数据库中的所有数据,并保留结构(表,关系等)。
我使用postgreSQL但我想,如果有一个命令,这不是特定于postgres。

I'm searching for a simple way to delete all data from a database and keep the structure (table, relationship, etc...). I using postgreSQL but I think, if there a command to do that, it's not specific to postgres.

谢谢,

Damien

推荐答案

使用 pg_dump转储模式

将数据库模式(-s标记)转储到一个文件中:

Dump you database schema (the -s tag) to a file:

pg_dump -s -f db.dump DB-NAME


$ b b

删除数据库:

Delete the database:

dropdb DB-NAME

重新创建它:

createdb DB-NAME

只还原模式:

pg_restore db.dump > psql DB-NAME

这应该适用于PostgreSQL;其他DBMS可能有自己的工具。我不知道有什么通用工具可以做到。

This should work on PostgreSQL; Other DBMS might have their own tools for that. I do no know of any generic tool to do it.

编辑

以下注释,您可能需要跳过 dropdb 命令,只需使用转储模式创建另一个数据库。如果一切顺利,您可以删除旧数据库:

Following comments, you might want to skip the dropdb command, and simply create another database with the dumped schema. If all went through well, you can drop the old database:

pg_dump -s -f db.dump DB-NAME
createdb DB-NEW-NAME
pg_restore db.dump > psql DB-NEW-NAME



此时,您在DB-NAME处具有完整的数据库, DB-NEW-NAME上的空模式。确定一切正常后,使用 dropdb DB-NAME

这篇关于如何清空SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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