mysql使用语句记录

1、查询test数据库表引擎是Innodb的表

select table_catalog
,table_schema
,table_name
,engine
from information_schema.tables
where table_schema='test' and engine='Innodb';

select table_catalog
      ,table_schema
      ,table_name
      ,engine
from information_schema.tables
where table_schema='test' and engine='MyISAM';

2、查询字段前8位
select left(imei,8) from admin_test;

3、快速copy一张结构一致的表

CREATE TABLE admin_test1 LIKE admin_test;

4、显示数据库表前缀为‘t1_’的表
show tables like 't1_%';

5、修改表引擎

ALTER TABLE my_tmp_table ENGINE=InnoDB;

6、通过SQL修复MyISAM表:
CHECK TABLE t1; 检测
repair table t1; 修复
使用myisamchk修复MyISAM
myisamchk可以直接访问表文件,而无须启动MySQL服务器。 
进入datadir文件目录,执行基本命令:
myisamchk --backup --recover t1
--backup选项是在尝试修复表之前先进行数据文件备份

7、将一张b表的数据插入另一张a表
INSERT INTO a SELECT * FROM b;

8、创建与删除索引

ALTER TABLE test drop index `unique`;
alter table test add  UNIQUE KEY `unique` (`day`,`imei`,`ipv4`,`uapp_id`);

9、插入数据有则修改没有则新增

INSERT INTO logs(gl_id,uapp_id,day,name,num) VALUES (123,56,"2019-01-23","test",1) ON DUPLICATE KEY UPDATE num=num+1

注:使用该语句时,不做修改的其他字段必须是唯一索引  如下表结构:

CREATE TABLE `logs` (
  `gl_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'gl_id',
  `num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '数量', 
  `uapp_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '应用id',
  `day` date NOT NULL DEFAULT '0000-00-00' COMMENT '日期',
  UNIQUE KEY `unique` (`gl_id`,`uapp_id`,`day`),
  KEY `num` (`num`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

猜你喜欢

转载自blog.csdn.net/lctmei/article/details/86528270
今日推荐