How do i copy data from one schema to another in mysql workbench?

If you plan to do database development with Java and MySQL, the first thing you’ll need to do is configure a database schema.

Technically speaking, the first thing to do before you configure a schema is actually install MySQL and the MySQL Workbench. But once those prerequisites are in place, the first database operation you’ll need to perform is schema creation.

Why is a database schema required?

In enterprise relational databases, schemas act as a top-level organizations structure. A schema typically maps to a single problem domain such as shipping or billing or user preferences. For cloud-native developers who follow the 12-Factor App guidelines, one schema typically maps to a single microservice.

Furthermore, most JDBC drivers require a schema name to create a JDBC connection to the database.

Schemas and the JDBC URL

The schema is part of the connection URL and the JDBC driver requires it at runtime to figure out which database tables are available for manipulation.

Without a database schema, your data-driven apps go nowhere.

How do i copy data from one schema to another in mysql workbench?

How to create a database schema in MySQL with the MySQL Workbench

Steps to create a schema in MySQL

Fortunately, it’s not hard to create a database schema in MySQL, especially if you have the MySQL Workbench tool at your disposal. Just follow these steps:

  1. Open the MySQL Workbench
  2. Click the Create Schema option
  3. Provide a schema name
  4. Click apply to create the MySQL scheme

The MySQL workbench executes the required SQL statement under the covers, and the schema is listed in the tool.

With the schema created, you can now provide any JDBC drivers, Hibernate config or JPA persistence.xml file the name of the database URL with the schema name attached. That allows your Java programs to connect to the database.

Once you’ve created the schema, you can then create database tables, add columns and rows, insert data and perform CRUD operations.

Your ability to manipulate relational data is only limited your knowledge of JPA, Hibernate and the JDBC APIs.

Summary: in this tutorial, you will learn how to copy table within the same database or from one database to another using CREATE TABLE and SELECT statements.

MySQL copy table to a new table

Copying data from an existing table to a new one is very useful in some cases such as backing up data and replicating the production data for testing.

To copy data from a table to a new table, you use CREATE TABLE and SELECT statements as follows:

CREATE TABLE new_table SELECT col, col2, col3 FROM existing_table;

Code language: SQL (Structured Query Language) (sql)

First, MySQL creates a new table with the name indicated in the CREATE TABLE statement. The structure of the new table is defined by the result set of the SELECT statement. Then, MySQL populates data that comes from the SELECT statement to the new table.

To copy partial data from an existing table to the new one, you use WHERE clause in the SELECT statement as follows:

CREATE TABLE new_table SELECT col1, col2, col3 FROM existing_table WHERE conditions;

Code language: SQL (Structured Query Language) (sql)

It is very important to check whether the table you want to create that already exists before creating it. To do so, you use IF NOT EXIST clause in the CREATE TABLE statement. The complete command of copying data from an existing table to the new one is as follows:

CREATE TABLE new_table SELECT col1, col2, col3 FROM existing_table WHERE conditions;

Code language: SQL (Structured Query Language) (sql)

Note that the statement above just copies the table and its data. It does not copy other database objects such as indexes, primary key constraint, foreign key constraints, triggers, etc., associated with the table.

To copy data from one table and also all the dependent objects of the table, you use the following statements:

CREATE TABLE IF NOT EXISTS new_table LIKE existing_table; INSERT new_table SELECT * FROM existing_table;

Code language: SQL (Structured Query Language) (sql)

We need to execute two statements. The first statement creates a new table new_table by duplicating the existing_table. The second statement inserts data from the existing table into the new_table.

MySQL copy table examples

The following statement copies data from the offices table to a new table named offices_bk in the classicmodels sample database.

CREATE TABLE IF NOT EXISTS offices_bk SELECT * FROM offices;

Code language: SQL (Structured Query Language) (sql)

We can verify the copy by querying data from the office_bk table as follows:

SELECT * FROM offices_bk;

Code language: SQL (Structured Query Language) (sql)
How do i copy data from one schema to another in mysql workbench?

In cases we want to copy the offices in the US only, we can add the WHERE clause to the SELECT statement as follows:

CREATE TABLE IF NOT EXISTS offices_usa SELECT * FROM offices WHERE country = 'USA'

Code language: SQL (Structured Query Language) (sql)

The following statement gets all data from the offices_usa table.

SELECT * FROM offices_usa;

Code language: SQL (Structured Query Language) (sql)
How do i copy data from one schema to another in mysql workbench?

Suppose, we want to copy not only the data but also all database objects associated with the offices table, we use the following statements:

CREATE TABLE offices_dup LIKE offices; INSERT office_dup SELECT * FROM offices;

Code language: SQL (Structured Query Language) (sql)

MySQL copy table to another database

Sometimes, you want to copy a table to a different database. In such cases you use the following statements:

CREATE TABLE destination_db.new_table LIKE source_db.existing_table; INSERT destination_db.new_table SELECT * FROM source_db.existing_table;

Code language: SQL (Structured Query Language) (sql)

The first statement creates a new table new_table in the destination database (destination_db) by duplicating the existing table (existing_table) from the source database (source_db).

The second statements copy data from the existing table in the source database to the new table in the destination database.

Let’s see the following example.

First, we create a database named testdb using the following statement:

CREATE DATABASE IF NOT EXISTS testdb;

Code language: SQL (Structured Query Language) (sql)

Second, we create the offices table in the testdb by copying its structure from the offices table in the classicmodels database.

CREATE TABLE testdb.offices LIKE classicmodels.offices;

Code language: SQL (Structured Query Language) (sql)

Third, we copy data from the classimodels.offices table to testdb.offices table.

INSERT testdb.offices SELECT * FROM classicmodels.offices;

Code language: SQL (Structured Query Language) (sql)

Let’s verify the data from the testdb.offices table.

SELECT * FROM testdb.offices;

Code language: SQL (Structured Query Language) (sql)
How do i copy data from one schema to another in mysql workbench?

In this tutorial, we have shown you various techniques to copy table within a database and from one database to another.

Was this tutorial helpful?

How do I copy data from one schema to another schema in MySQL?

Right-click on the database name then select "Tasks" > "Export data..." from the object explorer. 4. Provide authentication and select the source from which you want to copy the data; click "Next".

How do I copy data from one schema to another schema in SQL?

In SQL Management studio right click the database that has the source table, select Tasks -> Export data. You will be able to set source and destination server and schema, select the tables you wish to copy and you can have the destination schema create the tables that will be exported.

How do I copy a database schema in MySQL workbench?

5 Answers.
Open MySQL Workbench..
Create the old server's connection (if you haven't it).
Create the new server's connection (if you haven't it).
Go to Server Administration and click Manage Import / Export..
Select old server..
Select all schemas in Export to Disk tab..

How do I move data from one table to another in MySQL workbench?

The fastest way to copy a table in MySQL:.
Right-click the table you want to copy in Database Explorer and select Duplicate Object..
In the dialog that opens, select the destination db..
Select to copy the table data or structure only..
Specify the name of the new table, and click OK..