day47 Pyhton 数据库Mysql 04

# 表结构
# 建表  - 表的增加
    # create table
# 删表  -  表的删除
    # drop table
# 改表  - 表的修改
    # alter table 表名
        # rename 新表名
        # add 字段 类型(宽度) 约束 first
        # add 字段 类型(宽度) 约束 after 另一个字段
        # modify 字段 新类型(新宽度) 新约束 first/after
        # change 原字段 新字段 新类型(新宽度) 新约束 first/after
# 查看表结构 - 表的查
    # desc 表名;              只能查字段的信息
    # show create table 表名; 可以查看整个表的设置 编码 引擎 自增字段的offset

# 基础的数据类型 帮助我们刚好的决定表结构中用到的数据类型
# 约束
# not null 非空约束
    # default
# unique   唯一约束
    # int auto_increment
# primary key 主键 一张表有且只有一个主键
# foreign key 外键

# not null + unique = primary key
# unique + int -> auto_increment
# 外表中的字段是 unique -> foreign key
# 一个表中可以有多个非空 + 唯一
# 一个表中不可以有多个主键
# 一个表中可以有多个外键
# 联合
    # 联合主键
    # 联合唯一

二.今日内容

# 数据的插入
# 数据的删除
# 数据的修改
# 数据的查询
    # 单表查询
        # 很多语法和练习

三.数据的增删改

# create table t1(
#     id int primary key auto_increment,
#     name char(12) not null,
#     age int not null,
#     sex enum('male','female') default 'male'
# );


# 一 \ 数据的插入
# 表结构
# id name age sex
# insert into 表名 values (1,'alex',83,null);
# insert into 表名 (name,age) values ('alex',83);
# insert into t1 (name,age) values ('alex',83),
#                                    ('wusir',25),
#                                    ('yuan',25);

# 二 \ 数据的删除
# 表结构
# id name age sex
# 1  alex 83  male

# 删除数据 找到要删除的数据
# delete from 表名 where 条件
# delete from 表 where sex = 'male';
# delete from 表 where name = 'alex';
# delete from 表 where name = 'alex' and sex = 'male';

# 三 \ 数据的更新
# id name age sex
# 1  alex 83  male

# 更新数据 先找到要更新的数据
# update 表 set 字段名=值 where 条件
# update 表 set age = 84 where name = alex;
# update 表 set age = null where name = alex;
# update 表 set age = 84,
#               sex = 'female'
#               where id = 1;

# 所有的用户信息都在mysql的user表中
# 如果我们需要删除用户或者修改用户的密码,也可以使用数据的删改来操作user表
'''
company.employee
    员工id      id                  int
    姓名        emp_name            varchar
    性别        sex                 enum
    年龄        age                 int
    入职日期    hire_date           date
    岗位        post                varchar
    职位描述    post_comment        varchar
    薪水        salary              double
    办公室       office              int
    部门编号     depart_id           int
'''
# 一 \ 词法分析
#  select distinct 要查的字段 from 表
#                            where 条件
#                            group by 分组
#                            having 过滤
#                            order by 排序
#                            limit 取前n个
# 二 \ 简单查询
# 查询所有的字段\单个字段\给字段重命名\给字段去重
# select * from 表;
# select 字段名1,字段名2 from 表;
# select distinct 字段名1 from 表;
# select 字段名 as 新的临时名字 from 表;

# 查询数据的四则运算
# select emp_name,salary*12 from 表;
# select emp_name,salary*12 as annua_salary from employee;

# concat/concat_ws
# select emp_name,salary from employee;
# 姓名 : alex, 薪资:100000
# select concat('姓名 :',emp_name,', 薪资 :',salary) from employee;
# select concat('姓名 :',emp_name),concat('薪资 :',salary) from employee;
# select concat_ws(':',emp_name,salary) from employee;
# select concat_ws(':',emp_name,salary) as annual_salary from employee;

# case语句
# SELECT
#        (     # if条件判断
#        CASE                                # 一个if条件判断句的开始
#            WHEN emp_name = 'jingliyang'    # if
#            THEN emp_name                   # then if条件成立之后做的事儿
#            WHEN emp_name = 'alex'          # # elif 另一个条件
#            THEN CONCAT(emp_name,'_BIGSB')  #
#            ELSE                            # else
#                concat(emp_name, 'SB')      # 没有then 直接就是上述条件不满足都走这个分支
#        END                                 # end 就表示这个case语句结束了
#        ) as new_name
#    FROM
#        employee;
# 三 \ where 约束
# 1.比较运算符: > < >= <= <> != =
# select * from employee where id>10;
    # > < >= <= 一般和数字打交道
    # = != 和所有数据类型打交道
# 2.between a and b  # 表示范围在[a,b]之间
# 3.in
# 4.like
    # 通配符
        # '%' 表示任意长度任意字符
        # '_' 表示一个任意字符
# 5.and or not

# 薪资在8000-10000之间的男人
# select * from employee where salary between 8000 and 10000 and sex = 'male';
# 找到所有的 operation部门或者teacher部门
# select * from employee where post = 'operation' or post = 'teacher';
# select * from employee where post in ('operation','teacher');


# 函数
# now() 获取当前时间
# user() 获取当前用户
# password('密码') 摘要密码
# concat('','','','')
# concat_ws('拼接符号','','','')
# GROUP_CONCAT(字段名) 一定适合group by 连用的
# count 计数器
# sum
# max
# min
# avg
# 四 \ group by 约束
# 根据某些条件进行分组
# 按照部门分组
# 按照性别分组

# 每个部门的平均工资
# select post,avg(salary) from employee group by post;

# 五 \ having 约束  必须写在group by之后,而且不能脱离group by单独存在
# 需求 平均工资大于10000的部门有哪些
# 求部门的平局工资 只有在分组之后才能计算平均工资
# select post,avg(salary),group_concat(emp_name) from employee group by post having avg(salary) > 10000;
# select post,group_concat(emp_name) from emp group by post having 所有条件都是以组为单位的

# 六 \ order by 排序
# select * from employee order by salary;
# select * from employee order by salary desc;


# 七 \ limit 前n条
# limit n
# limit start,n  # 从start+1开始 ,取n条

 

posted @ 2018-12-19 20:50  Python张梦书  阅读(146)  评论(0编辑  收藏  举报