Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does import of a .sql file to MySQL overwrite the existing db or append to it?

Tags:

mysql

In some cases, I've had to do a mysqldump to produce a .sql file. After making some changes to the MySQL database for testing and development I want to restore it to the way it was before. So I import the .sql file. In the past I have delete the db and re-created it, and then did te import. Is that necessary? Does import from a .sql file overwrite and totally re-create the database and it's tables, or does it append to it? Thanks!

like image 985
Edward Avatar asked Sep 27 '12 11:09

Edward


People also ask

How do I import a .SQL file into MySQL workbench?

To import a file, open Workbench and click on + next to the MySQL connections option. Fill in the fields with the connection information. Once connected to the database go to Data Import/Restore. Choose the option Import from Self-Contained File and select the file.


2 Answers

Appends to it, unless the import file specifically treats tables differently.

like image 86
L0j1k Avatar answered Oct 11 '22 08:10

L0j1k


It depends on what commands the SQL file contains. Commands of interest are:

DROP DATABASE xxx;            # will delete the whole database
DROP TABLE xxx;               # unconditionally deletes a table
CREATE TABLE [IF NOT EXISTS]  # if IF NOT EXISTS adds the table, does nothing if exists
                              # otherwise, it adds the table, gives an error if it exists
like image 38
Jon Avatar answered Oct 11 '22 08:10

Jon