MySQL基本语句大囊集,可当作字典来翻看

1.DDL语句

create database xxxx 创建名为xxxx的数据库
use xxx 选择数据库xxx
show tables 查看数据库里的所有数据表
drop database dbname 删除数据库

create table tablename(
	name1  type1  constraints,
	name2  type2  constraints,
	name3  type3  constraints,
	....
	namex  typex  contraints
) 创建一张表, name表示列名, type表示数据类型, contraints表示约束条件

desc tablename 查看表的定义
show create table xxx 全面查看xxx表的定义信息
drop table xxx 删除表xxx
alter table xxx modify stu_name varchar(20) 修改xxx表的stu_name属性为varchar(20)
alter table xxx drop stu_name 删除一个表的一个字段,删除stu_name这一列

alter table xxx change age age1 int(10) 字段改名, 吧age字段改为age1 类型为int(10)
alter table xxx add stu_age int(10) after stu_name 在stu_name后插入一个字段(默认的插入都是从最后)
alter table xxx modify stu_age int(10) first 修改age字段位置放到最前边
alter table xxx rename xxxx 把xxx表改名为xxxx

2.DML语句

1.插入操作

insert into xxx (name, age) values ('zhangsan', 18)插入语句
insert into xxx values (1, 'zhangsan', 18, ....)若所有列都添加那么就可以不写
insert into xxx (name, age) values ('zhangsan', 18), ('lisi', 19), ('wangwu', 20) 可一次插入多条数据

2. 更新操作

update xxxx set name='liuliu', age=12 where id=1更新语句
update xx1 a, xx2 b set a.name='aaa', b.name='bbb' where xxxxxxxxx同时更新两个表的name

3.删除操作

dalete from xxx where name='zhangsan'删除xxx表中name是张三的记录
delete a, b from xx1 a, xx2 b where a.name=b.name同时删除2个表中的数据

4. 查询操作

select * from xxx查询表中所有数据
select * form xxx where id = 1 带条件查询
select * from xxx where id != 1 不等于查询, 也可以有 <,>, <=, >=等符号
select * from xxx where name like '%nike%'模糊查询
select distinct age from xxx 假如表中很多人年龄相同,我们要查都有几岁的人,就用distinct去重

排序查询
select * from xxx order by id 按照id排序(默认从小到大)
select * from xxx order by id desc 反转排序
select * from xxx order by id, score 如果id相同, 按照score排序
select * from xxx limit 3查询显示三条数据
select * from xxx limit 1, 3 从第二条数据开始显示三条数据

聚合查询
select count(*) from xxx 查询总数
select class, count(1) from xxx group by class 查询每个班的数, (没错就是写作count(1) )
select class, count(1) from xxx group by class with rollup 更细致的查询(包括null)
select class, count(1) from xxx group by class having count(1) > 1 查询总数大于一的结果
select sum(score), max(score), min(score), avg(score) from xxx 查询总分, 最大,最小和平均分
连接查询
select ename, departname from emp, dept where emp.deptno = dept.deptno查询emp和dept两个表中no相同的记录
select a.ename, b.departname from emp a, dept b where a.deptno=b.deptno 就是起了别名, 用a, b 替代emp和dept
内连接
select * from xx1 inner join xx2 where xx1.id = xx2.id 依旧查询两个表, 其中inner可以省去可以只写join
外连接(左连接)
select * from xx1 left join xx2 where xx1.id = xx2.id 查询两个表, 左链接以左边的表为主, 去关联右边
外连接(右连接)
select * from xx1 right join xx2 where xx1.id = xx2.id 这次是以右边的表为主
有关连接详细解释戳这里mysql查询之连接查询
子查询
select * from xxx where xxid in (select id in xxx) 查询某个东西是否在一个集合里边
select * from xxx where xxid = (select id in xxx limit 1)如果查询结果唯一,可以用等于连接
联合查询
select name from xxx union select name from yyy 这个是去重后的姓名
select name from xxx union all select name from yyy 这个是不去重的, 有多少就是多少

3.DCL语句

grant select, insert on dbname.* to 'zhangsan'@'localhost' identified by '123456' 创建一个叫做zhangsan的用户, 密码是123456, 并赋予zhangsan查询和插入权限
revoke insert on dbname.* from 'zhangsan'@'localhost' 剥夺zhangsan的插入权限

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章