Mysql数据库常用命令,mysql速学实用教程。

说明:对mysql数据库常用命令的整理
适用:mysql5.5+

一、Mysql的基本操作命令
查看所有数据库

show databases;

使用数据库

use mysql;

显示数据库中的表

show tables;

创建数据库 编码为utf8

create database 数据库英文名称 charset utf8;

删除数据库

drop database 数据库英文名称;

创建表 需先use数据库库名

use 表所在的数据库名;

创建a表,包含字段sid

create table a(sid tinyint);

向表a插数据

insert into a set sid=1;
insert into a  set sid=2;

查询表a的数据

select * from a;

删除表a

drop table a;

创建新a表

create table a(sid tinyint,money decimal(5,2));

插入数据

insert into a set sid=1,money=288;
insert into a set sid=1,money=18;
insert into a set sid=1,money=99.235;

字段类型说明:
1.char(数字)字符串类型,只能存多少个字符
2.char和varchar区别

   查询速度,char优于varchar
   	char(5) 1个字符串,那么也是占5个 定长
   存储大小,varchar优于char
   	varchar(5) 1个字符,就占一个 变长

3.前导零 zerofill 偶尔会把类型变正数范围

tinyint(10)  存1位数字,会补充9个零

4.非负 unsigned,把字段类型变正数范围

create table a(sid tinyint unsigned);

5 enum 枚举 单选

create table a(sid tinyint unsigned,nickname char(20),sex enum('男','女'));

6.set 多选

create table stu(
	sid tinyint unsigned
	,nickname char(20)
	sex enum('男','女'),
	play set('篮球','排球','抖音','PHP')
);

单条数据存入实例:

insert into a set sid=1,nickname ='chtml.cn',sex=1,hobby='篮球,排球,PHP';

二、Mysql的进阶操作命令
查看表结构:

desc stu;

default 字段默认值,一般not null 和 default 和结合起来使用的

create table b(
	uid smallint unsigned not null default 0,
	nickname char(20) not null default ''
);

unique 非重

create table b(
	uid int unsigned,
	author_num char(20) not null default '' unique,
	password char(32) not null default ''
)

主键 primary key,自增 auto_increment

create table c(
	arc_id int unsigned primary key auto_increment,
	title char(200) not null default '',
	content text,
	click smallint unsigned not null default 0
);

删除arc_id为2的数据

delete from c where arc_id=2;

复制b表为b_bak
1.不复制内容

create table b_bak like b;

2.把b表的数据复制到b_bak

insert into b_bak select * from b;

3.创建nb表同时复制数据

create table nb_bak1 select * from b;

查询所有

select * from arc;

查询arc_id

select arc_id from arc;

查询指定字段

select 字段名,字段名 from 表名;

别名 as

select 字段1,字段2 as 新字段2 from 表名;

where 代表条件

select * from 表名  where 字段id>1 and 字段2>10;

concat连接字符串

select concat(字段1,'-',字段2) as 新字段2 from 表名;

±-------------------------------------------------+
| 新字段2 |
±-------------------------------------------------+
| 字段1的内容-字段2的内容 |
±-------------------------------------------------+

查询结构与自身比较

select 字段1,字段1>50  as 新字段2 from 表名;

±------±–+
| 字段1| 新字段2 |
±------±–+
| 100| 1 |
| 30| 0 |
| 30| 0 |
±------±–+

把字段重复的值去掉

select distinct(字段) from 表名;

查找set类型数据

create table a(
	sid int unsigned primary key auto_increment,
	nickname char(200) not null default '',
	hobby set('篮球','足球','PHP','抖音')
);

查找hobby中的篮球
1.find_in_set() 不常用

select * from 表名 where find_in_set('篮球',hobby);

2 &1 &2 不推荐

select * from 表名 where hobby &1;

3.like 匹配 推荐

select * from 表名 where hobby like '%篮球%';

查询字段 为null的数据

select * from 表  where 字段  is null;

查询并赋值一个新的数据的if使用

 select click,if(click>5,'多','少') as tips from 表名;

±------±----+
| click | tips |
±------±----+
| 10 | 多 |
| 1| 少 |
| 1 | 少 |
±------±----+

order 排序 【desc 降序 asc 升序】
创建bd表

create table bd(
	bid int unsigned primary key auto_increment,
	name char(100) not null default '',
	sort smallint unsigned not null default 0
);

插入测试数据

insert into bd set name='腾讯搜索',sort=9;
insert into bd set name='360搜索',sort=10;
insert into bd set name='google搜索',sort=9;

修改bid为1的数据中的sort变为112

update bd set sort=112 where bid=1;

排序,升序

select * from bd order by sort asc;

降序

select * from bd order by sort desc;

limit 截取

1.limit 1 从0截取1条
select * from bd limit 1;
2.imit 1,2 从1截取2条
select * from bd limit 1,2;

between 10 and 20 【查找10到20之间的值,包括10和20】

select * from bd where sort between 10 and 20;

查询10或者15的数据

select * from bd where sort in(10,15);

查找以特定字符“x”开头的数据

select * from 表名 where 字段名 like "x%";

查找包含特定字符“x”的数据

select * from 表名 where 字段名 like "%x%";

查找包含特定字符“x”结尾的数据

select * from 表名 where 字段名 like "%x";

包含特定字符“x”开头后面跟一个字符的数据

select * from 表名 where 字段名 like 'x1';

把字段从左边截取一位字符

select left(字段,1) from 表名;

把字段从第二个位置截取一个字符串

select mid(字段,2,1) from 表名;

把字段从右边截取一位字符

select right(字段,1) from 表名;

随机查询一条数据

select * from 表名 order by rand() limit 1;

设置字符集utf8

set names utf8;

查看字符集变量

show variables like "%character%";

要查看的选项

character_set_client     | gbk    //客户端
character_set_connection | gbk    //连接端
character_set_results    | gbk    //结果端

三、mysql批量操作、统计与分组
录入多条数据;

insert into 表名 (字段1,字段2) values ('第一条数据1','1'),('第二条数据2','2');

清空表的所有数据 【一般不使用,通常需要做好安全机制过滤这条命令】

truncate 表名;

字段数据为1的记录,如果没有就添加,如果有就替换;

replace into 表名 (字段,字段2,字段3) values (1,'原有数据','新数据');

修改 【where可不加,可以修改全部数据,谨慎操作】

update 表名 set 字段='新的数据' where 字段=条件值;

删除【where可不加,可以删除全部数据,谨慎操作】

delete from 表名 where 字段=条件值;

把a表改名为b【修改表的名字】

alter table a rename b;

change 命令可以 改名同时改类型

把s改成sn,然后类型改为char(150)
alter table 表名 change s  sn char(150) not null default ''; 

modify命令改类型

alter table 表名 modify sn char(13) not null default '' first;

modify命令 修改sn为char(30)并且放在id字段的后面

alter table 表名 modify sn char(30) not null default '' after id;

add命令追加新字段

给表追加sx字段,并且在sn字段的后面
alter table 表名 add sx enum('1','2') not null default '1' after sn;

删除字段

alter table 表名 drop 字段名;

增加表的主键

alter table 表名 add primary key(id);

增加主键字段的自增

alter table 表名 modify id int unsigned auto_increment;

主键删除需要先删自增

alter table 表名 modify id int unsigned;

然后再删除主键

alter table 表名 drop primary key;

date类型说明:日期格式的数据存储。如2022-7-10

mysql输出现在的时间

select now();

统计表的数据总数

select count(*) from 表名;

查找字段中最小的数据

select min(字段名称) from 表名;

查找字段中最大的数据

select max(字段名称) from 表名;

取得字段总和

select sum(字段名) from 表名;

取得字段平均数

select avg(字段名) from 表名;

字段分组,筛选字段【group by 、 having组合使用】

select * from 表名 group by 字段名 having 字段名='筛选条件值';

end:Mysql多表查询

查询多个表简单命令 【笛卡尔积(无意义的)】

select * from 表a,表b;

加限制条的 【笛卡尔积(有意义的)】

select * from 表名a as a,表名b as c where a.id=c.id;

查询指定字段

select id,字段2,字段3 from 表名a as a,表名b as c where a.id=c.id;

内部连接 inner join

select * from 表名a as a join 表名b as c on a.id=c.id;

inner join 关联之后的where的使用

select * from 表名a as a join 表名b as c on a.id=c.id where a.id=20;

右连接 right join 【右表有数据就会有数据】

select * from 表名a as a right join 表名b as c on a.id=c.id;

多表查询命令说明:

与单表查询一直,在表连接后,拼接条件即可查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任聪聪

创作不易,你的打赏是我的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值