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. js中引入js

    第一个js文件(被引入的js文件),文件名one.js,内容如下 function alertInOne(){    alert('in one');} 第二个js文件,文件名two.js,内容如下 ...

  2. C格式字符串转为二叉树

    最近在LeetCode做题,二叉树出现错误时不好排查,于是自己写了一个函数,将前序遍历格式字串转换成二叉树. 形如 "AB#D##C##" 的字符串,"#"表示 ...

  3. 在Windows下编译Cef3.2623并加入mp3、mp4支持(附带源码包和最终DLL)《转》

    https://blog.csdn.net/zhuhongshu/article/details/54193842 源码包下载地址:点我下载 最终Dll.Lib.PDB.头文件下载地址(release ...

  4. java项目代码上线

    java项目代码上线   1:java项目代码上线架构图 ip地址及主机名规划 10.0.0.11 deploy 10.0.0.12 tomcat-web01 10.0.0.13 git.oldboy ...

  5. PHP 类属性

    属性 (Properties) 类的变量成员叫做“属性”,或者叫“字段”.“特征”,在本文档统一称为“属性”.属性声明是由关键字 public,protected或者 private 开头,然后跟一个 ...

  6. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. 【426】C 传递数组给函数

    参考:C 传递数组给函数 参考:C语言二维数组作为函数参数传递 参考:二维数组作为函数参数传递剖析(C语言)(6.19更新第5种) 总结: 一维数组参数,可以是地址.arr[].arr[n] 二维数组 ...

  8. (十三)class文件结构:常量池(转)

    Class类文件的结构 全局规范 1.任何一个Class文件都对应着唯一一个类或接口的定义信息,但反过来说,类或接口并不一定都得定义在文件里(譬如类或接口也可以通过类加载器直接生成).本章中,只是通俗 ...

  9. DevOps - 持续集成(Continuous Integration)

    1 - 持续集成简介 持续集成(Continuous integration,简称CI)是软件的开发和发布标准流程中最重要的部分. 作为一种开发实践,在CI中可以通过自动化等手段高频率地去获取产品反馈 ...

  10. php 常用的常量

    /* php 常用的常量 */ 1.系统常量 * FILE 当前PHP文件的相对路径 * LINE 当前PHP文件中所在的行号 * FUNCTION 当前函数名,只对函数内调用起作用 * CLASS ...