MySQL的基础sql语句语法格式

新建库: create database 库名;
删除库: drop database 库名;
使用数据库: use 库名;
展示所有库: show databases;
查看当前库名: select database();

创建表格: create table
显示数据库下所有数据表:show tables
显示数据表结构:desc 表名
对数据表进行重命名:rename 表名 to 新表名
删除数据表:drop table
查询表里所有数据:select * from 表名
添加字段:alter table 表名 add 字段类型 属性
修改字段:alter table 表名 change 类型 属性
删除字段:alter table 表名 drop

主键: primary key
自增: auto_increment
非空: not null
增: insert into 表名() values ()
查: select * from 表名 where 条件
修: update 表名 set 字段=值 where 条件
删: delete from 表名 where 条件
中文乱码: set names gbk


区间查询: select * from 表名 where 字段 between 值1 and 值2
模糊查询: select * from 表名 where 条件 like '%关键字%'(%一个或多个,_一个)
分段查询: select * from 表名 where 条件 limit 开始值 个数
排序:select * from 表名 order by 字段 asc(降序)||desc(升序)

赋予权限: grant all on 库名.* to '用户名'@'localhost'
剥夺权限: revoke all on 库名.* from '用户名'@'localhost'

聚合函数
计数: select count(字段或*) from 表名
求和: select sum(字段) from 表名
平均: select avg(字段) from 表名
最大值: select max(字段) from 表名
最小值: select min(字段) from 表名

分组查询: select 字段 from 表名 group by 字段
select 字段,字段 from 表名 group by 字段 having 条件

两表联查:
内连接:select 字段 from 表名1 inner join 表名2 on 连接条件 where 条件
隐式内连接:select 字段 from 表1,表2 where 表1.条件=表2.条件
左外连接: select 字段 from 表1 lift join 表2 on 连接条件 where 条件
右外连接: select 字段 from 表1 right join 表2 on 连接条件 where 条件

克隆(数据蠕虫): insert into 表名(字段,字段) select 字段, 字段 from 表名

猜你喜欢

转载自www.cnblogs.com/lishudong/p/11866912.html