python day39 数据库——增删改查 where条件 分组聚合 过滤排序和limit

一、内容回顾

存储引擎

# Innodb mysql5.6之后的默认存储引擎
  # 2个文件,4个支持(支持事务,行级锁,表级锁,外键)
# Myisam mysql5.5之前的默认存储引擎
  # 3个文件 支持表级锁
# Memory
  # 1个文件 数据断电消失

数据类型

# 数字 : bool int float(7,2)
# 日期 : date time datetime year
# 字符串 :
  # char   定长 效率高浪费空间 255
  # varchar 变长 效率低节省空间 65535
# enum 和 set :
  # 单选和多选

约束

# unsigned 无符号的
# not null 非空
# default 设置默认值
# unique   唯一,不能重复
  # unique(字段1,字段2,字段3) 联合唯一
# auto_increment 自增
  # int 必须至少unique字段,自带not null
# primary key 主键
  # not null + unique
  # 一张表只能有一个主键
# foreign key 外键
  # a表中有一个字段关联b表中的一个unique
  # a表中的是外键

建表

# create table 表名(
#   字段名1 类型(长度) 约束,
#   字段名1 类型(选项) 约束,
# );

修改表结构

# alter table 表名 rename 新名字;
# alter table 表名 add 字段名 类型(长度) 约束 after 某字段;
# alter table 表名 drop 字段名;
# alter table 表名 modify 字段名 类型(长度) 约束 first;
# alter table 表名 change 旧字名 新名字 类型(长度) 约束;

表之间的关系

# 一对一
# 一对多
# 多对多

删除表

# drop table 表名;

二、今日内容

create table t1(

id int primary key auto_increment,

username char(12) not null,

sex enum('male','female') default 'male',

hobby set('上课','写作业','考试') not null);

增 insert into 表(字段,...) values (值,...);

insert into t1 value (1,'大壮','male','上课,写作业');

insert into t1 values(2,'杜相玺','male','写作业,考试');

insert into t1 values(3,'b哥','male','写作业'),(4,'庄博','male','考试');

insert into t1(username,hobby) values ('杨得港','上课,写作业,考试'),('李帅','考试')

insert into t2(id,name) select id,username from t1;

清空表

# delete from 表;
  # 会清空表,但不会清空自增字段的offset(偏移量)值
# truncate table 表;
  # 会清空表和自增字段的偏移量

删除某一条数据

# delete from 表 where 条件;

# update 表 set 字段=值 where 条件;
# update 表 set 字段=值,字段=值 where 条件;

10个查询 1一个增删改

1.select语句

最简单的select

# select * from 表;
# select 字段,... from 表;

重命名字段

# select 字段 as 新名字,... from 表;
# select 字段 新名字,... from 表;

去重

# select distinct 字段 from 表;
# select distinct age,sex from employee;

使用函数

# concat
# concat_ws

四则运算的

#  select emp_name,salary*12 from employee; 乘法
#  select emp_name,salary*12 as annual_salary from employee;

使用判断逻辑

# case when语句 相当于 if条件判断句

where 筛选所有符合条件的行

# 比较运算符
    # > < >= <= <> !=
# 范围
    # between 10000 and 20000 要1w-2w之间的
    # in (10000,20000)   只要10000或者20000的
# 模糊匹配
    # like
        # % 通配符 表示任意长度的任意内容
        # _ 通配符 一个字符长度的任意内容
    # regexp
        # '^a'
        # 'g$'
# 逻辑运算
    # notandor

查看岗位描述不为NULL的员工信息

# is
# select * from employee where post_comment is not null;

查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资

# select emp_name, age, salary
# from employee wherepost = 'teacher' and salary not in(10000,9000,30000)

查看岗位是teacher且名字是jin开头的员工姓名、年薪

#  select emp_name,salary*12 from employee where post = 'teacher' and emp_name like 'jin%';

分组 group by 根据谁分组,可以求这个组的总人数,最大值,最小值,平均值,求和 但是这个求出来的值只是和分组字段对应

# 并不和其他任何字段对应,这个时候查出来的所有其他字段都不生效.

聚合函数

# count 求个数
# max  求最大值
# min  求最小值
# sum  求和
# avg  求平均

# SELECT post,emp_name FROM employee GROUP BY post;
# SELECT post,GROUP_CONCAT(emp_name) FROM employee GROUP BY post;

having 过滤语句

# 在having条件中可以使用聚合函数,在where中不行
# 适合去筛选符合条件的某一组数据,而不是某一行数据
# 先分组再过滤 : 求平均薪资大于xx的部门,求人数大于xx的性别,求大于xx人的年龄段

查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数

group by post having count(id) < 2;

排序 order by

# 默认是升序  asc
# 降序 desc
# order by age ,salary desc
  # 优先根据age从小到大排,在age相同的情况下,再根据薪资从大到小排

limit m,n

# 从m+1项开始,取n项
# 如果不写m,m默认为0

# limit n offset m
原文地址:https://www.cnblogs.com/iaoyuyuyuhuanghuang/p/14436515.html