MySQL中两种快速创建空表的方式的区别

MySQL中两种快速创建空表的方式的区别,第1张

在MySQL中有两种方法

create table t_name select create table t_name like

第一种会取消掉原来表的有些定义 且引擎是系统默认引擎

手册上是这么讲的 Some conversion of data types might occur For example the AUTO_INCREMENT attribute is not preserved and VARCHAR columns can bee CHAR columns

第二种就完全复制原表

先建立测试表:

mysql>create database dbtestQuery OK row affected ( sec)mysql>use dbtestDatabase changedmysql>create table t_old >( >id serial >content varchar( ) not null >`desc` varchar( ) not null) >engine innodbQuery OK rows affected ( sec)mysql>show create table t_old+ + +| Table | Create Table |+ + +| t_old | CREATE TABLE `t_old` (`id` bigint( ) unsigned NOT NULL auto_increment `content` varchar( ) NOT NULL `desc` varchar( ) NOT NULL UNIQUE KEY `id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin |+ + + row in set ( sec)

第一种方式

mysql>create table t_select select * from t_old where = Query OK rows affected ( sec)Records: Duplicates: Warnings: mysql>show create table t_select+ + +| Table | Create Table + + +| t_select | CREATE TABLE `t_select` (`id` bigint( ) unsigned NOT NULL default `content` varchar( ) NOT NULL `desc` varchar( ) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin |+ + + row in set ( sec)

第二种方式

lishixinzhi/Article/program/MySQL/201311/29611

1.快速清空表中的数据(20.05.25)

区别:

a.不带where参数的delete语句可以删除mysql表中所有内容,使用truncate table也可以清空mysql表中所有内容。

b.效率上truncate比delete快,但truncate删除后不记录mysql日志,不可以恢复数据。

c.delete的效果有点像将mysql表中所有记录一条一条删除到删完,而truncate相当于保留mysql表的结构,重新创建了这个表,所有的状态都相当于新表。

USE information_schema 

-- Mysql 一个数据库所有有数据的表

SELECT table_schema,table_name,table_rows FROM TABLES WHERE TABLE_SCHEMA='test'  AND table_rows != 0 ORDER BY table_rows DESC

-- Mysql 一个数据库中所有为空的表

SELECT table_schema,table_name,table_rows FROM TABLES WHERE TABLE_SCHEMA='test'  AND table_rows = 0 ORDER BY table_name 


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6094980.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存