day46 Pyhton 数据库Mysql 03
一内容回顾
存储引擎:主要描述的是数据存储的不同方式
innodb 支持事务\支持外键\行级锁\聚焦索引
myisam 不支持事务\不支持外键\表级锁\非聚焦索引
memory 只能在内存中存储表数据\存取速度快\断电数据丢失
blackhole 无论写入什么数据都不存储在表中\但是照常生成binlog日志\用于数据的分流
表结构的增删改查
创建表结构
create table 表名 (字段名 字段类型[(宽度) 约束条件],...)
查看表结构
desc 表名;
show create table 表名\G
mysql 中的数据类型
数字
有多种表示数字的数据类型,分为小数和整数
整数根据不同的数据类型,能表示的数据范围也不同
小数根据不同的数据类型,对数据小数位的精准控制不同
定义整数类型的时候,后面的宽度,约束的不是数字的位数,而是显示的宽度
# int(11)
# float(7,2),一共7位数,小数部分占2位,整数部分占5位
# bigint 能放更大的数据
# double 比float更精准(255,30)
# decimal 超级精准,默认为整数,(65,30)
# 字符串
# char(11) 定长 \浪费空间\存取速度快\(用户名\性别\手机号\银行卡号\身份证\密码\车牌号)
# varchar(11) 不定长\节省空间\存取速度慢\(地址\评论)
# 时间
# date 年月日
# datetime 年月日时分秒 可以为空
# timestamp 年月日时分秒 不能为空 默认值是当前时间 update的时候更新时间字段 能表示的时间范围短
# insert into 表 values ('2018-12-18 09:06:23')
# insert into 表 values ('2018/12/18 09+06+23')
# insert into 表 values ('20181218090623')
# insert into 表 values (20181218090623)
# 集合和枚举
# set 多选 自动去重,不在set中的不写入表
# enum单选 不在enum单选中的不写入表
二今日内容
# 一 表结构操作
# 表结构的操作
# 修改表结构
# 删除表结构 # create table 表名 (字段名 字段类型[(宽度) 约束条件],...)
# 约束条件 # 二 数据操作
# 增 insert数据
# 删 delete数据
# 改 update数据
# 查 select数据
# 单表查询
# 连表查询
表的操作
一\修改表结构
# 增加字段
# 1. alter table 表名 add 字段 新的数据类型 ;
# 2. alter table 表名 add 字段 新的数据类型 first;
# 3. alter table 表名 add 字段 新的数据类型 after 某字段; # 删除字段
# 1. alter table 表名 drop 字段; # 修改字段
# 1. alter table 表名 modify 字段 新的数据类型;
# 2. alter table 表名 modify 字段 数据类型 first;
# 3. alter table 表名 modify 字段 数据类型 after 某字段; # 1. alter table 表名 change 旧字段 新字段 数据类型;
# 2. alter table 表名 change 字段 字段 数据类型 first;
# 3. alter table 表名 change 字段 字段 数据类型 after 某字段; # 二\删除表结构
# drop table 表名;
约束条件
# 什么是约束
# not null 不能为空的
# unique 唯一 = 不能重复
# primary key 主键 = 不能为空 且 不能重复
# foreign key 外键约束 # 为什么要约束
# 是因为一个表中的数据要想完整规范,就必须对一些字段有基础的约束
# 一般情况下,我们都会根据程序的需求个特点对数据库进行约束
# 在你的开发生涯中,你应该对数据有一个基础的判断 # 在mysql中不区分大小写
# 一个关键字 :NULL null关键字 表示 空(相当于PYTHON中的None) # 创建表
# create table 表名 (字段名 数据类型(宽度) 约束条件,) # 约束一 not null
# create table t (id int not null,
# sex enum('male','female') not null default 'male') # 约束二 unique 唯一,允许多个字段为null
# 设置某一个字段的内容必须是唯一的
# create table t3 (id int unique); # 你设置了唯一,就不能插入两个相同的内容 测试:除了null之外
# create table t3 (id int not null unique); # 你设置了唯一+非空,就不能插入两个相同的内容,也不能插入NULL,就相当于设置了一个主键primary key
# create table t3 (id int, name char(12), unique(id),unique(name));
# create table t4 (pid int, pname char(12),ip char(15),port int,unique(ip,port));
# create table t4 (pid int, pname char(12),
# ip char(15) not null,
# port int not null ,
# unique(ip,port)); # 记录每一个程序的信息
# pid 程序名 ip地址 端口号
# 111 pycharm
# 123 pycharm
# 234 mysqld 192.168.11.44 3306
# 234 mysqld 192.168.11.22 3306
# 235 kugou 192.168.11.44 8000 # 两个或者多个字段的内容 = 联合唯一 # 约束三
# 主键 每一张表只能有一个主键
# primary key = not null + unique
# create table t5 (id int primary key);
# 能设置多个主键么 ? 不能
# create table t5 (id int primary key,name char(12) primary key);
# 能不能设置多个非空 + 唯一 ? 能
# create table t6 (id int not null unique,name char(12) not null unique);
# 联合主键
# create table t7 (pid int, pname char(12),
# ip char(15),
# port int,
# primary key(ip,port)); # auto_increment 自增
# create table t8 (id int unique auto_increment,name char(12));
# create table t9 (id int primary key auto_increment,name char(12));
# 对于自增id来说,删除数据并不会影响自增
# 设置为自增,用户最好不要自己插入这个字段 # 约束四 外键
# 部门id 部门名称 部门办公室号
# create table department (id int unique,dep_name char(20),dep_num int);
# alter table department modify id int unique; # 员工id name 年龄 工资 部门id(外键)
# create table employee (id int,name char(12),age int,salary int,dep_id int,
# foreign key(dep_id) references department(id)); # insert into department (id,dep_name) values
# (1,'教质部'),
# (2,'技术部'),
# (3,'人力资源部'); # 如果添加了外键约束,外键是employee(dep_id),那么employee(dep_id)和department (ide)都会受到约束
# update department set id = 3 where id = 2; # create table employee2 (id int,name char(12),age int,salary int,dep_id int,
# foreign key(dep_id) references department(id) on delete cascade on update cascade ); # insert into department (id,dep_name) values
# (3,'人力资源部');
#
# insert into employee2 (id,name,dep_id) values
# (1,'yuan',1),
# (2,'nezha',2),
# (3,'egon',2),
# (4,'alex',2),
# (5,'wusir',3),
# (6,'李沁洋',3),
# (7,'皮卡丘',3),
# (8,'程咬金',3),
# (9,'程咬银',3)
# ; # 约束 4个
# not null # 不允许为空
# default # 设置默认值
# unique # 唯一,不能约束null
# 联合唯一
# auto_increment # 自增
# primary key # 主键 = not null + unique (同一张表不能有两个主键)
# 联合主键
# foreign key # 本表中的字段关联另一张表中的"唯一"字段 ,本表中的字段是 外键,外表中的字段必须唯一/主键 # create table 表名 (
# 字段名1 字段类型(宽度) not null default 默认值,
# 字段名2 字段类型(宽度) not null unique,
# 字段名3 字段类型(宽度) primary key,
# 字段名4 int(宽度) unique auto_increment,
# ) # create table 表名 (
# 字段名1 字段类型(宽度) not null,
# 字段名2 字段类型(宽度) not null,
# 字段名3 字段类型(宽度),
# 字段名4 int(宽度),
# unique(字段名2),
# primary key(字段名3),
# unique(字段名4) auto increment,
# ) # create table 表名 (
# 字段名1 字段类型(宽度) not null,
# 字段名2 字段类型(宽度) not null,
# 字段名3 字段类型(宽度),
# 字段名4 int(宽度) auto increment,
# unique(字段名1,字段名2),
# primary key(字段名3,字段名4),
# ); # create table 表名(
# 字段名1 字段类型(宽度) not null,
# 字段名2 字段类型(宽度) not null,
# 外键名 字段类型(宽度),
# foreign key (外建) references 外表(外表中的字段)
# on delete cascade
# on update cascade,
# primary key(字段名1)
# ) # 添加主键
# alter table 表名 modify 字段名 类型(宽度) primary key;
day46 Pyhton 数据库Mysql 03的更多相关文章
- day45 Pyhton 数据库Mysql 02
一.前期回顾 数据库 mysql的安装 配置环境 为什么要用数据库? 稳定性 一致性 并发 存取数据效率高 数据库的分类 关系型数据库 mysql oracle sqlserver 非关系型数据库 r ...
- day49 Pyhton 数据库Mysql 06
多表查询 连表查询 要进行连接,那一定涉及两个表,两个表中要有关联条件才能进行连接 内连接 只有表一和表二中的连接条件都满足的时候才能显示出来 inner join on /where 条件 sele ...
- day44 Pyhton 数据库Mysql
内容回顾 什么是进程? 就是为了形容执行中的程序的一种称呼 它是操作系统中资源分配的最小单位 进程之间是数据隔离的,占用操作系统资源相对多 独立存在的 谈谈你对并发的理解 同时有多个任务需要执行,但是 ...
- day48 Pyhton 数据库Mysql 05
一内容回顾 insert insert into 表名 (字段名) values (值) insert into 表名 values (有多少个字段写多少个值) insert into 表名 val ...
- day47 Pyhton 数据库Mysql 04
# 表结构 # 建表 - 表的增加 # create table # 删表 - 表的删除 # drop table # 改表 - 表的修改 # alter table 表名 # rename 新表名 ...
- 数据库mysql的常规操作
1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...
- 数据库MySQL经典面试题之SQL语句
数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...
- MYSQL添加新用户 MYSQL为用户创建数据库 MYSQL为新用户分配权限
1.新建用户 //登录MYSQL @>mysql -u root -p @>密码 //创建用户 mysql> insert into mysql.user(Host,User,Pas ...
- Robot Framework-DatabaseLibrary数据库(MySql)
Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...
随机推荐
- 为商务办公赋能,TOM商务邮箱,塑造职场新形象
转眼已至2020年,互联网的这10年发生了翻天覆地的改变.短视频红利崛起.人脸识别支付.AI智能机器人对话...很多产品从默默无闻到一夜崛起.而对于商务邮箱领域,邮箱已成为一种习惯,却往往忽略了它的点 ...
- Python实现加密的ZIP文件解压(密码已知)
博主在上篇博文介绍了<Python实现加密的RAR文件解压(密码已知)>后,又尝试了ZIP文件的解压方法,下面开始分享. 当ZIP文件的压缩密码已知时,可以通过调用zipfile库进行解压 ...
- 【二叉树-BFS系列1】二叉树的右视图、二叉树的锯齿形层次遍历
题目 199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, ...
- linux定时删除过期文件
需求说明 每日凌晨0点定时删除/temp目录下的所有一个月未被访问的文件. 脚本实现 linux 终端输入crontab -e,添加定时任务脚本命令 [root@localhost ~]# cront ...
- 高可用集群之keepalived+lvs实战2
keepalived简介 lvs在我之前的博客<高负载集群实战之lvs负载均衡-技术流ken>中已经进行了详细的介绍和应用,在这里就不再赘述.这篇博文将把lvs与keepalived相结合 ...
- C++万能头文件的秘密
#include<bits/stdc++.h> 万能头文件,拼写怎么这么奇怪? 其实,bits表示一个文件夹,stdc++.h是里面的头文件,这表示路径. 搜索这个文件夹,找到后打开. 打 ...
- 你想要的Java资料这里都有!!!
你想要的Java资料这里都有!!! [复制链接] 1.资源标题:程序员的SQL金典(完整) 资源地址:http://down.51cto.com/data/2207566 2.资源标题: ...
- dedecmsv5.7sp1远程文件包含漏洞审计
dedecms5.7 sp1版本存在远程文件包含漏洞,在此记录审计复现漏洞过程. 漏洞在/install/index.php(index.php.bak)文件中,漏洞起因是$$符号使用不当,导致变量覆 ...
- Next轻量级框架与主流工具的整合
前言 老大说以后会用 next 来做一下 SSR 的项目,让我们有空先学学.又从 0 开始学习新的东西了,想着还是记录一下学习历程,有输入就要有输出吧,免得以后给忘记学了些什么~ Next框架与主流工 ...
- mysql 空值(null)和空字符('')的区别
日常开发中,一般都会涉及到数据库增删改查,那么不可避免会遇到Mysql中的NULL和空字符. 空字符(")和空值(null)表面上看都是空,其实存在一些差异: 定义: 空值(NULL)的长度 ...