mysql

##### 增删改查

增 :  insert into 表名(字段名,...) value (值,.....)

删: delete from 表名 where 条件  (删除记录)

​ delete from 表名  (删除所有记录)

​ drop 表名  (删除表)

改: update 表名 set 字段=值 where 条件 (有条件则修改符合条件的  无则是全部)

查: select * from 表名  (显示所有字段)

​ select * from 表名 where 条件

​ 关系运算符:= != > < <= >=

​ select * from 表名 where 字段名 (not)in (元素,...) (不在 或 在)

​ select * from 表名 where 字段名 (not)between 值1 and 值2(不在...里 或 在...里)

​ select * from 表名 where 字段名 is(not) null  (是否为空)

​ select distinct 字段名 from 表名  (去重)

​ select * from 表名 where 字段名 (not)like '匹配字符串'  (关键字查询)

​ select * from 表名 where like '%a%'  (模糊查询  包含a的)

​ select * from 表名 where 条件1 and 条件2 ......  (多条件精准查询)

​ select * from 表名 where 条件1 or 条件2    (或查询)

##### 高级查询

##### 聚合函数

*count()记录总条数*

select count(*) from 表名

*sum()所有值的总和*

select sum(字段名) from 表名

*AVG()平均值*

select AVG(字段名) from 表名

*max()最大值*

select max(字段名) from 表名

*min()最小值*

select min(字段名) from 表名

##### 排序

select 字段,... from 表名 order by 字段 asc  (升序)

select 字段,... from 表名  order by 字段 desc  (降序)

##### 分组

select 字段,.. from 表名  group by 字段,...

select sum(字段名) from 表名 group by 字段名 having sum(字段名) < 10 (查询某字段总和小于10的)

##### 分页

select * from 表名 limit 0,2  (每页显示2个,第一个页面)

##### 连接查询

inner join ... on  等值连接

left join ... on 左连接

right join ... on 右连接

select 表A.字段1,表B.字段1 from 表A [inner | left | right] join 表B on 表A.字段2 = 表B.字段2

##### 子查询

select * from 表A where 字段1 > (select AVG(字段) from 表B)

##### 约束

int unsigned 无符号整形

auto_increment 表示自动增长

not null 表示不能为空

primary key 表示主键 

default 默认值

##### 外键

建表时:  foreign key(a_id) references 表B(id)

取消外键约束: show create table 表名  (获取名称之后 根据名称删除)

添加外键约束: alter table 要连接表名 add foreign key (a_id) references 被连接的表名(id)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • @[toc] MySQL操作表中数据 MySQL 提供了功能丰富的数据库管理语句,包括向数据库中插入数据的 INS...
    夜雨流云阅读 533评论 0 1
  • 数据库的相关概念 数据库的好处 1、持久化数据到本地2、可以实现结构化查询,方便管理 数据库的常见概念☆ 1、DB...
    lzh_1阅读 586评论 0 3
  • /* Windows服务 */-- 启动MySQLnet start mysql-- 创建Windows服务sc ...
    阿登20阅读 201评论 0 0
  • https://www.cnblogs.com/best/p/6517755.html /* 启动MySQL */...
    程序媛大姐阅读 535评论 0 0
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,550评论 16 22