Introducing several ways to replicate tables in MySQL

coldplay.xixi
Release: 2021-04-28 09:42:28
forward
4333 people have browsed it

Introducing several ways to replicate tables in MySQL

Several ways to copy a table

  • Copy onlytable structure

create table tableName like someTable;
Only the table structure, including primary keys and indexes, will be copied, but the table data will not be copied

  • Only the table data will be copied
##create table tableName select * from someTable;

Copy the general structure of the table and all data, and will not copy the primary key, index, etc.

  • Complete copy
create table tableName like someTable;

insert into tableName select * from someTable;
Complete in two steps, copy first Table structure, then insert data

Related free learning recommendations:

mysql video tutorial

Example

    Connect to the database, use the student database and view all data tables
  1. USE student;SHOW TABLES;
    Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL2. Create the t1 data table and insert two items Record and set the index for the name field

CREATE TABLE t1( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) COMMENT '姓名');INSERT INTO t1(name) VALUES('张三');INSERT INTO t1(name) VALUES('李四');CREATE INDEX idx_name ON t1(name);
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL3. View all records of the t1 data table

SELECT * FROM t1;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL4. View the index of the t1 data table

SHOW INDEX FROM t1\G;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL5. Create the t2 data table (only copy the table structure)

CREATE TABLE t2 LIKE t1;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL6. View the records of the t2 data table

SELECT * FROM t2;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL7. View the index of the t2 data table

SHOW INDEX FROM t2\G;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL8. View the structure of the t2 data table

SHOW CREATE TABLE t2\G;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL9. Create the t3 data table ( Only copy table data)

CREATE TABLE t3 SELECT * FROM t1;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL10. View the table structure of the t3 data table

SHOW CREATE TABLE t3\G;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL11. View the index of the t3 data table

SHOW INDEX FROM t3;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL12. View all records of the t3 data table

SELECT * FROM t3;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL13. Create t4 data table (complete copy)

CREATE TABLE t4 LIKE t1;INSERT INTO t4 SELECT * FROM t1;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL14. View the structure of t4 data table

SHOW CREATE TABLE t4\G;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL15. View the index of the t4 data table

SHOW INDEX FROM t4\G;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL16. View all records of the t4 data table

SELECT * FROM t4;
Copy after login
Rendering:


Introducing several ways to replicate tables in MySQL

Note: This article is a summary of the blogger’s MySQL learning and does not support any commercial use. Please indicate the source when reprinting! If you also have a certain interest and understanding in MySQL learning, you are welcome to communicate with bloggers at any time~

Related free learning recommendations:

mysql database(video)

The above is the detailed content of Introducing several ways to replicate tables in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!