本人在学习mysql数据库时,记录得简单基本语句使用方法
连接数据库
mysql -uroot -p mysql
mysql -uroot -p
退出
exit /quit/ctrl+d
sql语句最后需要有;结尾
---显示数据库版本
select version();
显示当前时间
select now();
查看所有数据库
show databases;
创建数据库
create database [name];
指定编码
create database [name] charset=utf8(不是UTF-8)
查看创建数据库的语句
show create database [name];
删除数据库
drop database [name];
使用数据库
use 【name】;
查看目前使用的数据库
select database();
显示所有的表
show tables;
创建表
create table students(
id int unsigned not null auto_increment primary key,
name varchar(30),
age tinyint unsigned default 0,
gender enum(“男”,“女”,"中性"),
查看表的信息
desc 表名;
修改表的结构
添加字段(birthday)
alter table students add birthday datetime;
修改表--修改字段( 不重命名版)
alter table students modify birthday date;
改字段名
alter table students change birthday birth date default "2000-01-01"
删除字段(删除birthday字段)
alter table students drop birthday;
删除表格
drop table 表名;
增删改查
插入内容
insert into students values(0,"老汪",18,“女”);
多行插入
insert into students values(列1),(列2);
查看所有内容
select * from students;
取别名
select name as 姓名 from students;
修改内容
update 表名 set 列1 = 值,列2=值 where 条件;
删除
物理删除 delete from 表名 where 条件;
逻辑删除(添加一个判断删除字段)
alter table students add is_delete bit default 0;
通过更新字段的值来表示已被删除。
条件运算
select * from students where age>18 and age<100;
模糊查询
% 模糊查询
_ 一个下划线表示一个字符
__两个下划线 表示两个字符
select *from students where name like "%小%" 查找包含小明
select * from students where name like "__" 查看两个字的名字
正则匹配模糊查询 rlike
select *from students where name rlike "[0-9A-Z]"
范围查询
范围内in (1,3,8)
select name, age from students where id in (1,3,8)
不在范围内 not in
select name, age from students where id not in (1,3,8)
between and 表示在一个连续的范围内
select name, age from students where id between 18 and 34
not between and 表示不在一个连续的范围内
select name, age from students where id not between 18 and 34
空判断
is null 为空 不为空 is not null
select name from students where age is null;
排序
order by DESC 降序
select * from students where (age between 18 and 34) and gender="女" order by id DESC
order by ASC 升序
select * from students order by id ASC
order by ASC降序
---order by 按个字段(先按照age ,如果age相同,则按照名字)
select * from students where id<19 order by age desc,name desc
聚合函数
总数 count
select count(*) from students where gender=1 ;
最大值(求年龄最大)
select max(age) from students;
最小值
select min(age) from students;
求和
select sum(age) from students;
--计算平均值
select avg(age) from students;
select sum(age)/count(*) from students;
四舍五入(将平均值四舍五入,)
round(数字,2)保留两位小数
select round(sum(age)/count(*),2) from students;
分组group by(取一个标志位来分组)
select gender,count(*) from students group by gender
group_concat(字段)查看组内包含的内容
--having
查看平均年了超过30岁的性别,以及姓名 having ave(age)>30
select gender,group_concat(name),avg(age) from student group by gender having avg(age)>30;
分页LIMIT
select * from student limit 5;查询前5个
select * from student limit 0,5; 从第一个开始,查询5个 (0表示从第1个开始)
连接查询inner join
select * from students inner join classes on students.id = classes.id;
子查询
select * from students where height = (select max(height) from students)
开启时间
set profilings=1
查看执行语句的时间
show profiles;
索引
create index from 表名(字段名(长度))
查看索引
show index from 表名;