day47 Pyhton 数据库Mysql 04
# 表结构
# 建表 - 表的增加
# create table
# 删表 - 表的删除
# drop table
# 改表 - 表的修改
# alter table 表名
# rename 新表名
# add 字段 类型(宽度) 约束 first
# add 字段 类型(宽度) 约束 after 另一个字段
# modify 字段 新类型(新宽度) 新约束 first/after
# change 原字段 新字段 新类型(新宽度) 新约束 first/after
# 查看表结构 - 表的查
# desc 表名; 只能查字段的信息
# show create table 表名; 可以查看整个表的设置 编码 引擎 自增字段的offset # 基础的数据类型 帮助我们刚好的决定表结构中用到的数据类型
# 约束
# not null 非空约束
# default
# unique 唯一约束
# int auto_increment
# primary key 主键 一张表有且只有一个主键
# foreign key 外键 # not null + unique = primary key
# unique + int -> auto_increment
# 外表中的字段是 unique -> foreign key
# 一个表中可以有多个非空 + 唯一
# 一个表中不可以有多个主键
# 一个表中可以有多个外键
# 联合
# 联合主键
# 联合唯一
二.今日内容
# 数据的插入
# 数据的删除
# 数据的修改
# 数据的查询
# 单表查询
# 很多语法和练习
三.数据的增删改
# create table t1(
# id int primary key auto_increment,
# name char(12) not null,
# age int not null,
# sex enum('male','female') default 'male'
# ); # 一 \ 数据的插入
# 表结构
# id name age sex
# insert into 表名 values (1,'alex',83,null);
# insert into 表名 (name,age) values ('alex',83);
# insert into t1 (name,age) values ('alex',83),
# ('wusir',25),
# ('yuan',25); # 二 \ 数据的删除
# 表结构
# id name age sex
# 1 alex 83 male # 删除数据 找到要删除的数据
# delete from 表名 where 条件
# delete from 表 where sex = 'male';
# delete from 表 where name = 'alex';
# delete from 表 where name = 'alex' and sex = 'male'; # 三 \ 数据的更新
# id name age sex
# 1 alex 83 male # 更新数据 先找到要更新的数据
# update 表 set 字段名=值 where 条件
# update 表 set age = 84 where name = alex;
# update 表 set age = null where name = alex;
# update 表 set age = 84,
# sex = 'female'
# where id = 1; # 所有的用户信息都在mysql的user表中
# 如果我们需要删除用户或者修改用户的密码,也可以使用数据的删改来操作user表
'''
company.employee
员工id id int
姓名 emp_name varchar
性别 sex enum
年龄 age int
入职日期 hire_date date
岗位 post varchar
职位描述 post_comment varchar
薪水 salary double
办公室 office int
部门编号 depart_id int
'''
# 一 \ 词法分析
# select distinct 要查的字段 from 表
# where 条件
# group by 分组
# having 过滤
# order by 排序
# limit 取前n个
# 二 \ 简单查询
# 查询所有的字段\单个字段\给字段重命名\给字段去重
# select * from 表;
# select 字段名1,字段名2 from 表;
# select distinct 字段名1 from 表;
# select 字段名 as 新的临时名字 from 表; # 查询数据的四则运算
# select emp_name,salary*12 from 表;
# select emp_name,salary*12 as annua_salary from employee; # concat/concat_ws
# select emp_name,salary from employee;
# 姓名 : alex, 薪资:100000
# select concat('姓名 :',emp_name,', 薪资 :',salary) from employee;
# select concat('姓名 :',emp_name),concat('薪资 :',salary) from employee;
# select concat_ws(':',emp_name,salary) from employee;
# select concat_ws(':',emp_name,salary) as annual_salary from employee; # case语句
# SELECT
# ( # if条件判断
# CASE # 一个if条件判断句的开始
# WHEN emp_name = 'jingliyang' # if
# THEN emp_name # then if条件成立之后做的事儿
# WHEN emp_name = 'alex' # # elif 另一个条件
# THEN CONCAT(emp_name,'_BIGSB') #
# ELSE # else
# concat(emp_name, 'SB') # 没有then 直接就是上述条件不满足都走这个分支
# END # end 就表示这个case语句结束了
# ) as new_name
# FROM
# employee;
# 三 \ where 约束
# 1.比较运算符: > < >= <= <> != =
# select * from employee where id>10;
# > < >= <= 一般和数字打交道
# = != 和所有数据类型打交道
# 2.between a and b # 表示范围在[a,b]之间
# 3.in
# 4.like
# 通配符
# '%' 表示任意长度任意字符
# '_' 表示一个任意字符
# 5.and or not # 薪资在8000-10000之间的男人
# select * from employee where salary between 8000 and 10000 and sex = 'male';
# 找到所有的 operation部门或者teacher部门
# select * from employee where post = 'operation' or post = 'teacher';
# select * from employee where post in ('operation','teacher'); # 函数
# now() 获取当前时间
# user() 获取当前用户
# password('密码') 摘要密码
# concat('','','','')
# concat_ws('拼接符号','','','')
# GROUP_CONCAT(字段名) 一定适合group by 连用的
# count 计数器
# sum
# max
# min
# avg
# 四 \ group by 约束
# 根据某些条件进行分组
# 按照部门分组
# 按照性别分组 # 每个部门的平均工资
# select post,avg(salary) from employee group by post; # 五 \ having 约束 必须写在group by之后,而且不能脱离group by单独存在
# 需求 平均工资大于10000的部门有哪些
# 求部门的平局工资 只有在分组之后才能计算平均工资
# select post,avg(salary),group_concat(emp_name) from employee group by post having avg(salary) > 10000;
# select post,group_concat(emp_name) from emp group by post having 所有条件都是以组为单位的 # 六 \ order by 排序
# select * from employee order by salary;
# select * from employee order by salary desc; # 七 \ limit 前n条
# limit n
# limit start,n # 从start+1开始 ,取n条
day47 Pyhton 数据库Mysql 04的更多相关文章
- day45 Pyhton 数据库Mysql 02
一.前期回顾 数据库 mysql的安装 配置环境 为什么要用数据库? 稳定性 一致性 并发 存取数据效率高 数据库的分类 关系型数据库 mysql oracle sqlserver 非关系型数据库 r ...
- day49 Pyhton 数据库Mysql 06
多表查询 连表查询 要进行连接,那一定涉及两个表,两个表中要有关联条件才能进行连接 内连接 只有表一和表二中的连接条件都满足的时候才能显示出来 inner join on /where 条件 sele ...
- day46 Pyhton 数据库Mysql 03
一内容回顾 存储引擎:主要描述的是数据存储的不同方式 innodb 支持事务\支持外键\行级锁\聚焦索引 myisam 不支持事务\不支持外键\表级锁\非聚焦索引 memory 只能在内存中存储表数据 ...
- day44 Pyhton 数据库Mysql
内容回顾 什么是进程? 就是为了形容执行中的程序的一种称呼 它是操作系统中资源分配的最小单位 进程之间是数据隔离的,占用操作系统资源相对多 独立存在的 谈谈你对并发的理解 同时有多个任务需要执行,但是 ...
- day48 Pyhton 数据库Mysql 05
一内容回顾 insert insert into 表名 (字段名) values (值) insert into 表名 values (有多少个字段写多少个值) insert into 表名 val ...
- 创建本地数据库mySQL并连接JDBC
转自: http://blog.csdn.net/wei_chong_chong/article/details/44830491 如何创建本地数据库MySQL并连接JDBC 转载 2015年04月0 ...
- 数据库MySQL——初识
认识数据库—MySQL 楔子 假设现在你已经是某大型互联网公司的高级程序员,让你写一个火车票购票系统,来hold住十一期间全国的购票需求,你怎么写? 由于在同一时段抢票的人数太多,所以你的程序不可能写 ...
- 数据库mysql的常规操作
1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...
- 数据库MySQL之 视图、触发器、存储过程、函数、事务、数据库锁、数据库备份、事件
数据库MySQL之 视图.触发器.存储过程.函数.事务.数据库锁.数据库备份.事件 浏览目录 视图 触发器 存储过程 函数 事务 数据库锁 数据库备份 事件 一.视图 1.视图概念 视图是一个虚拟表, ...
随机推荐
- Java的ArrayList实现随机生成N-M之间N个不重复的随机数
在此之前我使用Java的数组实现了产生N-M之间的不重复的随机数,下面是使用数列ArrayList实现同样的功能,代码如下: /** * 随机生成 N--M,N个不重复随机数 使用ArrayList ...
- Appium之常用API
Appium常用API解析 1.current_activity:获取当前页面的activity名,比如com.taobao.tao.TBMainActivity 或 com.taobao.brows ...
- golang开发:CSP-WaitGroup Mutex
CSP 是 Communicating Sequential Process 的简称,中文可以叫做通信顺序进程,是一种并发编程模型,最初于Tony Hoare的1977年的论文中被描述,影响了许多编程 ...
- 轻松上手SpringBoot Security + JWT Hello World示例
前言 在本教程中,我们将开发一个Spring Boot应用程序,该应用程序使用JWT身份验证来保护公开的REST API.在此示例中,我们将使用硬编码的用户和密码进行用户身份验证. 在下一个教程中,我 ...
- [LeetCode]364. 加权嵌套序列和 II (DFS)
题目 给一个嵌套整数序列,请你返回每个数字在序列中的加权和,它们的权重由它们的深度决定. 序列中的每一个元素要么是一个整数,要么是一个序列(这个序列中的每个元素也同样是整数或序列). 与 前一个问题 ...
- charles常用功能 request和response(简单的操作)
先介绍一个修改request请求参数值的方法吧 第一步: 拷贝完成后还需要配置一下: 先添加一个: 然后下一步: 最后点击OK,就可以开始操作request和response数据了 先修改reques ...
- IDEA使用 live template添加groovy脚本给方法,类,js方法添加注释(转载)
IDEA添加Live Template: File->Setting->Editor->Live Templates Abbreviation: * Template text: * ...
- Java沙箱安全机制介绍【转载】
沙箱安全机制的应用层面:360沙箱.win10沙箱.包括VMware Workstation.Oracle VM VirtualBox都可以充当沙箱去使用,沙箱中的操作与本机无关,进而保证本机的安全性 ...
- Spring使用@Async实现异步
使用场景 在实际项目中,一个接口如果需要处理很多数据,如果是同步执行,通过网络请求接口可能会出现请求超时.这时候就需要使用异步执行处理了. 使用经验 代码 异步服务类 @Service // Spri ...
- vue项目初始化自定义webpack与eslint
文章目录 问题 简化步骤 问题 // main.js import Antd from "ant-design-vue"; import "ant-design-vue/ ...