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. 关于密码重用参数PASSWORD_REUSE_TIME,PASSWORD_REUSE_MAX之间的关系及其演示

    转自: https://blog.51cto.com/carefree/1382811 测试环境:10.2.0.2.0测试用户:SCOTT测试用的三组密码:oracle1 oracle2 oracle ...

  2. Understanding glibc malloc

    https://wooyun.js.org/drops/深入理解%20glibc%20malloc.html https://sploitfun.wordpress.com/2015/02/10/un ...

  3. Android通过ksoap2这个框架调用webservice大讲堂

    昨天有人问我Android怎么连接mysql数据库,和对数据库的操作呀,我想把,给他说说json通信,可是他并不知道怎么弄,哎算了吧,直接叫他用ksoap吧,给他说了大半天,好多零碎的知识,看来还是有 ...

  4. leetcode 50. Pow(x, n) 、372. Super Pow

    50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...

  5. python制作简单excel统计报表1之with的简单用法

    # coding=utf-8 def open_file(): """使用with打开一个文件""" # 普通操作文件方法 # f = op ...

  6. 虚拟机的Vmtools

    安装了虚拟机之后,文件共享不方便,安装VMTools可以在windows上直接拖文件到linux上. 安装方法: 1.进入linux把CD弹出 2.打开虚拟机之后 3.下载完成可以在linux的CD设 ...

  7. C++构造函数以及何时被调用

    using namespace std; class A { public: A() { cout << "默认无参构造函数" << endl; } #if ...

  8. python web开发Django连接mysql

    需要MYSQL-python,一般情况下直接pip install window系统会报错,所以要去 https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml  ...

  9. Anti Pattern - ThreadLocal variables with Thread Pool(转)

    In a previous post, I wrote the usage and benefits of ThreadLocal based instance variables in concur ...

  10. Top 10 Machine Learning Algorithms For Beginners

    Linear Regression Logistic regression KNN Classification Support Vector Machine (SVM) Decision Trees ...