MySQL复制表结构和表数据的SQL语句和时间函数
MySQL复制表结构和表数据的语句:完成表结构和表数据同时复制(创建表并复制数据)
1、MySQL复制表结构和表数据的语句:完成表结构和表数据同时复制(创建表并复制数据)
mysql> CREATE TABLE tmp_table SELECT * FROM dede_news;
Query OK, 628 rows affected (2.04 sec)
Records: 628 Duplicates: 0 Warnings: 0
说明:这种方法的一个最不好的地方就是新表中没有了旧表的primary key、Extra(auto_increment)等属性,,需要自己手动添加。
2、MySQL复制表结构但不复制表数据的语句:(创建表并复制数据,通过条件语句控制数据为空)
mysql> CREATE TABLE tmp_table SELECT * FROM dede_news WHERE 1=2;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
说明:通过给定1=2这个where条件,使最终选择到的数据为空,而是只复制表结构。
3、还可以通过下面的方法只复制表结构
mysql> CREATE TABLE tmp_table like dede_news;
Query OK, 0 rows affected (0.06 sec)
4、使用INSERT INTO 表1 SELECT 字段 FROM 表2可以将指定的或者全部的表2中的数据插入到表1中(前提是数据类型一致)
mysql> INSERT INTO tmp_news SELECT * FROM dede_news;
Query OK, 628 rows affected (0.57 sec)
Records: 628 Duplicates: 0 Warnings: 0
其他:
1)同时删除对个表:
DROP TABLE tmp_news,tmp_table;
2)时间函数 date_sub() 和 date_add()
mysql> SELECT now() AS now,date_add(now(),interval 5 day) AS add5d,date_add(now(),interval 5 month) AS add5m,date_add(now(),interval 5 year) AS add5y,date_sub(now(),interval 5 day) AS sub5d,date_sub(now(),interval 5 month) AS sub5m,date_sub(now(),interval 5 year) AS sub5y\G
*************************** 1. row ***************************
now: 2014-03-19 21:35:24
add5d: 2014-03-24 21:35:24
add5m: 2014-08-19 21:35:24
add5y: 2019-03-19 21:35:24
sub5d: 2014-03-14 21:35:24
sub5m: 2013-10-19 21:35:24
sub5y: 2009-03-19 21:35:24
1 row in set (0.00 sec)
UPDATE dede_news SET createtime=date_sub(createtime,interval 1 year) WHERE createtime > now();
说明:INSERT xxx INTO xxx SELECT xxx FROM xxx 和 SELECT xxx INTO xxx FROM xxx 两个语句的对比
在MySQL中 INSERT xxx INTO xxx SELECT xxx FROM xxx 语句可以将查询的结果插入到数据库表中
而使用SELECT xxx INTO xxx FROM xxx 语句,在是用在存储函数中将查询结果赋值给变量的

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.
