首页 > 技术分享 > MySQL
收藏

MySQL常用指令和结构说明2017-11-22 17:25:32

大潇博客 原创文章,转载请标明出处

查看数据表的结构,三种写法结果一样

desc tablename;

describe tablename;

show columns from tablename;


重命名表

rename table 旧表名 to 新表名


根据已知表结构创建新表

create table 新表名 like 旧表名


添加id字段为自增主键(注意换行,分开写两行是固定写法)

alter table 表名 add id int(8) not null

primary key auto_increment first


同张表中把B字段的内容复制到A字段,不加where修改表中的所有数据

update 表名 set A=B [where ...]


数据表增加一列

alter table 表名 add 列名 vrchar(80) not null


数据表删除一列

alter table 表名 drop column 列名


修改表列名

alter table 表名 change column 旧字段 新字段 varchar(100);


修改表列类型

alter table 表名 modify 列名 char(10);


根据两表id值一致,把表1对应的字段复制到表2对应的字段

update 表1,表2 set 表1.title=表2.title where 表1.id=表2.id;  

update 表1 inner join 表2 on 表1.id=表2.id set 表2.title=表1.title

/*set前的顺序可颠倒,set后的顺序决定修改哪张表的数据,需谨慎*/


查询数据库某列中是否有中文的数据

SELECT * FROM `表名` where 列名 REGEXP concat('[',char(0xE0),'-',char(0xEF),'][',char(0x80),'-',char(0xBF),'][',char(0x80),'-',char(0xBF),']');


定义一个新的命令结束符号

delimiter $$;   /*把语句结束符定义为“$$”,默认是“;”*/


mysql的insert into select语句用法:

/*首先创建一张测试表*/

create table test(id int(6) not null,text varchar(255) not null);

/*测试表添加数据*/

insert into test(1,'aa');

/*使用insert into select语句*/

insert into test select * from test;

/*使用insert into select语句复制某个字段*/

insert into test(text) select text from test;


修改mysql主键从1开始自增

/*执行下面这条SQL时需要此表为空表,否则执行不生效*/

ALTER TABLE 表名 AUTO_INCREMENT = 1;


获得指定表字段,注意需要在information_schema表中查询(mysql自带的数据表)

use information_schema;

select column_name from columns where table_name='tabllename';


mysql改变编码

set names utf8; set names gbk;



其它操作与结构说明:

mysql中 \G 表示以行的形式显示,不以表格形式显示


mysql的data文件夹存放数据库

.frm 表的数据结构

.MYD 表的数据

.MYI 表的索引

 

unsigned 无符号

无符号和有符号主要区别体现在:范围上

 

MySQL三种存储引擎对比


未完待续..


打赏

阅读排行

大家都在搜

博客维护不易,感谢你的肯定
扫码打赏,建议金额1-10元
  • 15601023311