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 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+

python 之 数据库(修改表、复制表、删除表、单表查询)的更多相关文章

  1. mysql之字段的修改,添加、删除,多表关系(外键),单表详细操作(增删改)

    字段的修改.添加和删除 create table tf1( id int primary key auto_increment, x int, y int ); #修改 alter table tf1 ...

  2. SqlServer设置特定用户操作特定表(插入、删除、更新、查询 的权限设置)

    目录 一.需求场景: 二.操作步骤: 表上右键选择[属性],选择[权限]选项卡: 点击[搜索],在弹出的框中点击[浏览],选择需要设置的用户: 在上面点击[确定]后,就可以在[权限]选项卡中看到权限列 ...

  3. mariadb数据库(2)增删改与 单表查询

    一.数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:decimal ...

  4. 【Excle数据透透视表】如何删除数据透视表

    选中区域A4:C17,在键盘上按DELETE键删除,结果提示: 那么如何删除呢? 解决方案 选中整个数透视表,再删除 具体操作: 选中整个数据透视表→DELETE 注意:删除之后,源数据不会受到影响

  5. 阶段3 1.Mybatis_09.Mybatis的多表操作_2 完成account表的建立及实现单表查询

    mybatis中的多表查询:         示例:用户和账户             一个用户可以有多个账户             一个账户只能属于一个用户(多个账户也可以属于同一个用户)    ...

  6. day03 mysql外键 表的三种关系 单表查询 navicat

    day03 mysql navicat   一.完整性约束之     外键 foreign key     一个表(关联表: 是从表)设置了外键字段的值, 对应的是另一个表的一条记录(被关联表: 是主 ...

  7. python将数据库修改,数据库操作同步到数据库中

    *****************数据库迁移(同步)命令******************************** 1.python manage.py makemigrations 将数据库的 ...

  8. Mysql教程:(六)修改语句、、删除语句、字符查询like

    1.修改语句 update 表名 set  where 条件 mysql> update student set birth=1988,department='中文系' where id=901 ...

  9. demo Django-基础书籍添加删除(单表)

    小demo使用---- 1.pycharm-2019.2 2.python-3.7.2 3.mysql-5.7.25 4.django-2.2.4 使用过程中的一些注意事项和出现的常见错误的解决地址 ...

  10. MySQL数据库之-foreign key 外键(一对多、多对多、一对一)、修改表、复制表

    摘要: 外键 一对多 外键 多对多 外键 一对一 --------------------------------------------------------------------------- ...

随机推荐

  1. 北漂IT男返乡2年的三线楼市观察(宜昌夷陵篇)-原创

    一直想写点什么,这段时间总算有空,好嘞,正好有兴致来写一写楼市相关的文章以饕读者和粉丝朋友. 宜昌?说宜昌您可能不知道,但是说三峡大坝您就知道了 最近这两年,因为小宝的降临,我多半时间待在老家宜昌陪伴 ...

  2. Cesium原理篇:Material【转】

    https://www.cnblogs.com/fuckgiser/p/6171245.html Shader 首先,在本文开始前,我们先普及一下材质的概念,这里推荐材质,普及材质的内容都是截取自该网 ...

  3. tx1 gpio

  4. Unity制作王者荣耀商业级手游

    <王者荣耀>这种现象级手机游戏是如何制作出来的呢?本文以<王者荣耀>MOBO类型的多人在线战术竞技游戏为入口,覆盖Unity游戏制作开发前端与Node.js服务器端的开发必备知 ...

  5. (基因功能 & 基因表达调控)研究方案

    做了好久的RNA-seq分析,基因表达也在口头溜了几年了,但似乎老是浮在表面. 对一件事的了解程度决定了你的思维深度,只想做技工就不用想太多,想做大师就一定要刨根问底. 老是说基因表达,那么什么是基因 ...

  6. R scholar和rentrez | NCBI和Google scholar文献数据挖掘

    主要会用到两个R包: rentrez: 'Entrez' in Rscholar: Analyse Citation Data from Google Scholar RISmed 包可以查询 Pub ...

  7. 前端 img标签显示 base64格式的 图片

    本文链接:https://blog.csdn.net/kukudehui/article/details/80409522在做项目的时候,我从后端返回了一个base64格式的图片文件,想把它渲染在前端 ...

  8. elementUI 列表里面含有多选框,当翻页的时候依然保持之前页多选不变

    el-table的type="selection"的使用 场景:el-table,type="selection"时,重新请求后,设置列表更新前的已勾选项 踩坑 ...

  9. Java获取系统基础参数信息

    一.JVM已经帮我们读取了系统信息 public void testSystemParam(){ Properties p = System.getProperties(); Iterator it ...

  10. disruptor 单生产者多消费者

    demo1 单生产者多消费者创建. maven 依赖 <!-- https://mvnrepository.com/artifact/com.lmax/disruptor --> < ...