zoukankan      html  css  js  c++  java
  • python 之 数据库(修改表、复制表、删除表、单表查询)

    10.8 修改表、复制表、删除表

    10.81 修改表 alter table

    1. 修改表名
    alter table 表名 rename 新表名;
    2. 增加字段
    alter table 表名 add 字段名  数据类型 [完整性约束条件…];
    alter table t1 add stu char(10) not null after name;     #添加到name字段之后
    alter table t1 add sex enum('male','female') default 'male' first;#添加到最前面                   
    3. 删除字段
    alter table t1 drop sex;
    4. 修改字段(增加主键)
    alter table t1 modify age int(3);
    alter table t1 modify id int(11) primary key auto_increment;      #修改为主键
    ​
    alter table t1 change 旧字段名 新字段名 新数据类型 [完整性约束条件…];
    5. 对已经存在的表增加复合主键
    alter table t1 add primary key(ip,port); 
    6. 删除主键
    a. 删除自增约束 alter table t1 modify id int(11) not null;
    b. 删除主键 alter table t1 drop primary key;
    7. 修改存储引擎 alter table it engine=innodb;
    8. 增加主键(设置索引) alter table t1 add primary key(id);

    10.82 复制表

    create table new_t1 select * from t1;                       # 复制表结构+记录,但是key和自增不会复制
    alter table new_t1 modify id int(11) primary key auto_increment; #添加主键和自增
                             #条件为假,查不到任何记录                                      
    create table new1_t1 select * from t1 where 1=2;                #只复制表结构,但是key和自增不会复制
    alter table new_t1 modify id int(11) primary key auto_increment; #添加主键和自增                                                                           
    create table t2 like t1;    #只完全复制表结构,不复制记录

    10.83 删除表

    drop table t1;

    10.9 单表查询

    语法
    select distinct 查询字段1,查询字段2......... from 表名
        where 分组之前的过滤条件
        group by 分组条件
        having 分组之后的过滤条件
        order by 排序字段1 asc,排序字段2 desc
        limit 5,5;

    10.91 where过滤

    select id,name from db39.emp where id >= 3 and id <= 6
    select *  from db39.emp where id between 3 and 6;
    ​
    select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
    select * from emp where salary in (20000,18000,17000);
    select * from emp where salary not in (20000,18000,17000);
    select * from emp where id not between 3 and 6;
    ​
    要求:查询员工姓名中包含i字母的员工姓名与其薪资
    select name,salary from db39.emp where name like '%i%'
    ​
    要求:查询员工姓名是由四个字符组成的的员工姓名与其薪资
    select name,salary from db39.emp where name like '____';
    select name,salary from db39.emp where char_length(name) = 4;
    ​
    要求:查询岗位描述为空的员工名与岗位名
    select name,post from db39.emp where post_comment is NULL;
    select name,post from db39.emp where post_comment is not NULL;

    10.92 group by分组

    #设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据
    mysql> set global sql_mode="strict_trans_tables,only_full_group_by";
    ​
    #聚合函数:每个部门的最高、最低、平均、总工资,计数
    select post,max(salary) from emp group by post;
    select post,min(salary) from emp group by post;
    select post,avg(salary) from emp group by post;
    select post,sum(salary) from emp group by post;
    select post,count(id) from emp group by post;

    group_concat (不能做中间结果)、concat 、concat_ws 、as

    #group_concat(分组之后使用):取出分组后,组内定制的详细信息
    select post,group_concat(name) from emp group by post;
    select post,group_concat(name,"_SB") from emp group by post;
    select post,group_concat(name,": ",salary) from emp group by post;
    select post,group_concat(name,":",age,":",sex) from emp group by post;
    +-----------------------------------------+--------------------------------------------------------
    | post                                    | group_concat(name,"_SB")                              |
    +-----------------------------------------+--------------------------------------------------------
    | operation                               | 程咬铁_SB,程咬铜_SB,程咬银_SB,程咬金_SB,张野_SB         |
    | sale                                    | 格格_SB,星星_SB,丁丁_SB,丫丫_SB,歪歪_SB                |
    |外交大使                                | egon_SB                                               |
    +-----------------------------------------+--------------------------------------------------------
    # concat(不分组时用):自定制取出的结果
    select name as 姓名,salary as 薪资 from emp;
    select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;
    +------------------+-----------------+
    | 姓名             | 薪资            |
    +------------------+-----------------+
    | NAME: egon       | SAL: 7300.33    |
    | NAME: alex       | SAL: 1000000.31 |
    | NAME: wupeiqi    | SAL: 8300.00    |
    +------------------+-----------------+
    # concat_ws (不分组时用):每个分组结果都用相同的分隔符时使用
    select concat_ws(":",name,age,sex,post) as info from emp;
    +------------------------------------------------------+
    | info                                                 |
    +------------------------------------------------------+
    | egon:18:male:外交大使                               |                                    
    | 程咬金:18:male:operation                             |
    | 程咬银:18:female:operation                           |
    | 程咬铜:18:male:operation                             |
    | 程咬铁:18:female:operation                           |
    +------------------------------------------------------+
    # 补充as语法
    mysql> select emp.id,emp.name from emp as t1; # 报错
    mysql> select t1.id,t1.name from emp as t1;
    # 查询四则运算
    select name,salary*12 as annual_salary from emp;

    10.93 having过滤

    having的语法格式与where一模一样,只不过having是在分组之后进行的进一步过滤,即where不能用聚合函数,而having是可以用聚合函数的

    #统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
    select post,avg(salary) from emp
            where age >= 30
            group by post
            having avg(salary) > 10000;
    +---------+---------------+
    | post    | avg(salary)   |
    +---------+---------------+
    | teacher | 255450.077500 |
    +---------+---------------+
    #强调:having必须在group by后面使用
    select * from emp having avg(salary) > 10000;#报错

    10.94 distinct去重

    select distinct post from emp;
    +-----------------------------------------+
    | post                                    |
    +-----------------------------------------+
    | 外交大使                               |
    | teacher                                 |
    | sale                                    |
    | operation                               |
    +-----------------------------------------+        

    10.95 order by 排序

    select * from emp order by salary (asc); #默认升序排
    select * from emp order by salary desc; #降序排
    select * from emp order by age desc,salary asc; #先按照age降序排,如果相同再按照薪资升序排
    ​
    # 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序
    select post,avg(salary) from emp
        where age > 10
        group by post
        having avg(salary) > 1000
        order by avg(salary);
    +-----------------------------------------+---------------+
    | post                                    | avg(salary)   |
    +-----------------------------------------+---------------+
    | sale                                    |   2600.294000 |
    |外交大使                                |   7300.330000 |
    | operation                               |  16800.026000 |
    | teacher                                 | 151842.901429 |
    +-----------------------------------------+---------------+

    10.96 limit 限制显示条数

    select * from emp limit 3;                      #从头开始只显示3条信息
    select * from emp order by salary desc limit 1;   #找到工资最大的一条信息
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    | id | name | sex  | age | hire_date  | post    | post_comment | salary     | office | depart_id |
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    |  2 | alex | male |  78 | 2015-03-02 | teacher | NULL         | 1000000.31 |    401 |         1 |
    +----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
    # 分页显示
    select * from emp limit 0,5;#从0开始显示5条信息(1-5)
    select * from emp limit 5,5;#从5开始显示5条信息(6-10)

    10.97 正则表达式

    select * from emp where name regexp '^jin.*(n|g)$';
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+
    | id | name  | sex  | age | hire_date  | post  |post_comment| salary  | office | depart_id |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+
    | 6 | jingliyang|female|18| 2011-02-11 | teacher | NULL    |  9000.00 |  401   |         1 |
    | 7 | jinxin    |male  |18| 1900-03-01 | teacher | NULL    | 30000.00 |  401   |         1 |
    +----+------------+--------+-----+------------+---------+--------------+----------+--------+
  • 相关阅读:
    vue中处理ie兼容性问题
    vue使用websocket
    vue-cli中使用sass的坑
    really_probe()
    ro.boot.bootreason property设置(androidboot.xxxx bootargs)
    kernel exception vector table
    compile/link misc
    user space syscall/library API misc
    LIUNX SHELL中-a 到-z的解释
    getenforce/setenforce
  • 原文地址:https://www.cnblogs.com/mylu/p/11291450.html
Copyright © 2011-2022 走看看