如何清空 SQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2117708/
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
How to empty a SQL database?
提问by Damien
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.
我正在寻找一种从数据库中删除所有数据并保留结构(表、关系等)的简单方法。我使用 postgreSQL,但我认为,如果有一个命令可以做到这一点,它并不是 postgres 特有的。
Thanks,
谢谢,
Damien
达米安
回答by Adam Matan
Dump the schema using pg_dump
. drop the database, recreate it and load the schema.
使用 转储模式pg_dump
。删除数据库,重新创建它并加载模式。
Dump you database schema (the -s tag) to a file:
将您的数据库架构(-s 标记)转储到文件中:
pg_dump -s -f db.dump DB-NAME
Delete the database:
删除数据库:
dropdb DB-NAME
Recreate it:
重新创建它:
createdb DB-NAME
Restore the schema only:
仅恢复架构:
pg_restore db.dump > psql DB-NAME
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.
这应该适用于 PostgreSQL;其他 DBMS 可能有自己的工具。我不知道有什么通用工具可以做到这一点。
EDIT:
编辑:
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:
根据注释,您可能希望跳过该dropdb
命令,而只需使用转储模式创建另一个数据库。如果一切顺利,您可以删除旧数据库:
pg_dump -s -f db.dump DB-NAME
createdb DB-NEW-NAME
pg_restore db.dump > psql DB-NEW-NAME
At this point, you have the full database at DB-NAME, and an empty schema at DB-NEW-NAME. after you're sure everything is OK, use dropdb DB-NAME
.
此时,您在 DB-NAME 上有完整的数据库,在 DB-NEW-NAME 上有一个空模式。在确定一切正常后,使用dropdb DB-NAME
.
回答by Damien
You can do something like this:
你可以这样做:
export PGUSER=your_pg_user
export PGHOST=database.host
export PGPORT=port
export PGDATABASE=your_database
psql -qAtX -c "select 'TRUNCATE table ' || quote_ident(table_schema) || '.' || quote_ident(table_name) || ' CASCADE;' from information_schema.tables where table_type = 'BASE TABLE' and not table_schema ~ '^(information_schema|pg_.*)$'" | psql -qAtX
It will do what's necessary.
它会做必要的事情。
Of course these exports are not necessary, but they will make it simpler to run 2 copies of psql without having to givem them all standard -U, -d, and so on, switches.
当然,这些导出不是必需的,但是它们会使运行 2 个 psql 副本变得更简单,而不必为它们提供所有标准的 -U、-d 等开关。
One thing though - using TRUNCATE to do so, while faster than DELETE, has it's drowbacks - for example - it is not being replicated by Slony and any other replication system that works on triggers. Unless you are working on PostgreSQL 8.4, and your replication knows how to use triggers on TRUNCATE.
但是有一件事 - 使用 TRUNCATE 这样做,虽然比 DELETE 快,但它有缺点 - 例如 - 它不会被 Slony 和任何其他适用于触发器的复制系统复制。除非您正在使用 PostgreSQL 8.4,并且您的复制知道如何在 TRUNCATE 上使用触发器。
回答by ZombieSheep
I'm not a Postgres guy, but one option would be to iterate through the tables and issue a Truncatecommand against each one. You'll have to take otable relationships into account, though - you will not be able to delete reference data before data that refers to it, for example.
我不是 Postgres 的人,但一种选择是遍历表并对每个表发出Truncate命令。但是,您必须考虑可交换的关系 - 例如,您将无法在引用它的数据之前删除引用数据。
回答by mYnDstrEAm
In pgAdmin you can do:
在 pgAdmin 中,您可以执行以下操作:
- Right-click database -> backup, select "Schema only"
- Drop the database
- Create a new database and name it like the former
- Right-click the new database -> restore -> select the backup, select "Schema only"
- 右键数据库->备份,选择“Schema only”
- 删除数据库
- 创建一个新的数据库并像前者一样命名
- 右键新建数据库->恢复->选择备份,选择“Schema only”
回答by syed irfan
your can delete all records of your database without restriction of foreign keys by following three steps
您可以通过以下三个步骤不受外键限制地删除数据库的所有记录
- Take script of your Database
- Right Click on your database (your DB Name)
- click on task and then "Generate script"
- Specify location
- Delete your database base
- recreate a database with the same name and run you generated script
- 获取数据库的脚本
- 右键单击您的数据库(您的数据库名称)
- 单击任务,然后单击“生成脚本”
- 指定位置
- 删除您的数据库库
- 重新创建一个同名的数据库并运行你生成的脚本
This way you can empty all of your database
这样您就可以清空所有数据库