
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Insert data from one schema to another in MySQL?
To insert data from one scheme to another, the syntax is as follows. Here, we have two databases “yourDatabaseName1” and “yourDatabaseName2” −
insert into yourDatabaseName2.yourTableName2 select *from yourDatabaseName1.yourTableName1;
To understand the above syntax, let us create a table. We are creating a table in database “web” −
mysql> create table DemoTable2020 -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2020 values(101,'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2020 values(102,'David'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable2020 values(103,'Mike'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2020;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 101 | Chris | | 102 | David | | 103 | Mike | +------+-------+ 3 rows in set (0.00 sec)
Here is the query to create second table in another database “test” −
mysql> use test; Database changed mysql> create table DemoTable2021 -> ( -> StudentId int, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.60 sec)
Here is the query to MySQL inserting data from one schema to another −
mysql> insert into test.DemoTable2021 select *from web.DemoTable2020; Query OK, 3 rows affected (0.18 sec) Records: 3 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select *from DemoTable2021;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 101 | Chris | | 102 | David | | 103 | Mike | +-----------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- Insert data from one table to another in MySQL?
- MySQL statement to copy data from one table and insert into another table
- Insert from one table with different structure to another in MySQL?
- MySQL query to insert data from another table merged with constants?
- How to SELECT fields from one table and INSERT to another in MySQL?
- Take all records from one MySQL table and insert it to another?
- Update data in one table from data in another table in MySQL?
- How to insert values from one table into another in PostgreSQL?
- Simplest way to copy data from one table to another new table in MySQL?
- How to copy data from one field to another on every row in MySQL?
- Move rows from one table to another in MySQL?
- How to subtract one data frame from another in R?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Send data from one Fragment to another using Kotlin?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?

Advertisements