python 之 数据库(修改表、复制表、删除表、单表查询)

摘要:
10.8修改表、复制表、删除表10.81修改表altertable1.修改表名altertable表名rename新表名;2.增加字段altertable表名add字段名数据类型[完整性约束条件…

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

10.81 修改表 alter table

1. 修改表名
alter table表名 rename 新表名;
2. 增加字段
alter table 表名 add 字段名  数据类型 [完整性约束条件…];
alter table t1 add stu char(10) not nullafter name;     #添加到name字段之后
alter table t1 add sex enum('male','female') default 'male'first;#添加到最前面                   
3. 删除字段
alter table t1 dropsex;
4. 修改字段(增加主键)
alter table t1 modify age int(3);
alter table t1 modify id int(11) primary keyauto_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 * fromt1;                       # 复制表结构+记录,但是key和自增不会复制
alter table new_t1 modify id int(11) primary keyauto_increment; #添加主键和自增
                         #条件为假,查不到任何记录                                      
create table new1_t1 select * from t1 where 1=2;                #只复制表结构,但是key和自增不会复制
alter table new_t1 modify id int(11) primary keyauto_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 bypost;
select post,min(salary) from emp group bypost;
select post,avg(salary) from emp group bypost;
select post,sum(salary) from emp group bypost;
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 bypost;
select post,group_concat(name,"_SB") from emp group bypost;
select post,group_concat(name,": ",salary) from emp group bypost;
select post,group_concat(name,":",age,":",sex) from emp group bypost;
+-----------------------------------------+--------------------------------------------------------
| 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 薪资 fromemp;
select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 fromemp;
+------------------+-----------------+
| 姓名             | 薪资            |
+------------------+-----------------+
| 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 fromemp;
+------------------------------------------------------+
| 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 ast1; # 报错
mysql> select t1.id,t1.name from emp ast1;
# 查询四则运算
select name,salary*12 as annual_salary from emp;

10.93 having过滤

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

#统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
select post,avg(salary) fromemp
        where age >= 30
        group bypost
        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 fromemp;
+-----------------------------------------+
| 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) fromemp
    where age > 10
    group bypost
    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 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+

免责声明:文章转载自《python 之 数据库(修改表、复制表、删除表、单表查询)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java 显示读取properties 乱码解决方案软件产品案例分析(团队)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

oracle 排序后分页查询

demo: select * from ( select * from DEV_REG_CFG_CAMERA where 1 = 1 order by unid asc) where rownum < 3001 minus select * from ( select * from DEV_REG_CFG_CAMERA where 1 = 1 ord...

安装pysqlite2

1. 从https://github.com/msabramo/pysqlite2 下载源码。 2.安装python-dev:sudo apt-get install python-dev否则在 编译或安装过程中,可能会出现 错误:error:Python.h No such file or directory 。注意,不同的Linux发行版的安装命令可...

python 连接oracle -- sqlalchemy及cx_Oracle的使用详解

python连接oracle -- sqlalchemy import cx_Oracle as orcl import pandas as pd from sqlalchemy import create_engine # 数据库连接 db = create_engine('oracle://qmcb:qmcb@localhost:1521/tqmcb...

linux路由表配置

一、原理说明 1、路由表(table)从0到255进行编号,每个编号可以对应一个别名,编号和别名的对应关系在linux下放在/etc/iproute2/rt_tables这个文件里,一般0编号的table对应的别名为upspec,255编号对应的别名为local,254和253对应的别名分别为main和default,我们通常用route命令配置和查看的...

ubuntu 18.04安装PIL(Python Imaging Library )

本人也是个小萌新 安装过程也是曲折 现附上我的安装过程 1.百度教的 sudo apt-get install python-imaging sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev 2.发现报错,其实是我自己机器的原因 然后又去配置源 我是ubun...

sql学习笔记--存储过程

 存储过程(stored procedure)有时也称sproc,它是真正的脚本,更准确地说,它是批处理(batch),但都不是很确切,它存储与数据库而不是单独的文件中。   存储过程中有输入参数,输出参数以及返回值等。 一、创建存储过程   创建存储过程的方法和创建数据库中任何其他对象一样,除了他使用AS关键字外。存储过程的基本语法如下:   CRE...