MySQL介绍
1、数据库概述
- 数据库,简单理解就是存放计算机数据的仓库
- 可以对仓库的数据进行新增、查询、更新、删除
1、架构
数据库的架构可以分为三个层次
- 内层 :最接近实际存储体
- 外层 :最接近用户,观看数据的方式
- 概念层 :介于两者之间的中间层
2、数据库分类
-
关系型数据库(SQL),由多张二维行列表组成的数据库
优势:易于维护,使用方便
劣势:I/O瓶颈 -
非关系型数据库(NOSQL)
优势:查询速度快,key-value键值型 高性能并发读写
劣势:不提供关系型数据库对事物的处理
MySQL介绍
关系型数据库知识和特点小姐
事务:
- 原子性:要么操作成功,要么操作失败
- 一致性:执行前和执行后必须处于一致
- 隔离性:用户在访问数据库时,数据库会为每一个操作开启一个事务,不会被其他事务干扰,互相隔离
- 持久性:一旦事务被提交,数据库的数据改变改变是永久性的,系统发生故障了,也不会丢失事务操作
1、简介
MySQL是一个关系型数据库管理系统
特点
- 开源
- 标准sql
- 兼容,支持多种语言
- 支持大型数据库
2、数据结构
库
表
表结构
- 表头
- 列
- 行
- 键
- 值
3、数据类型
- 整数:int,bit
- 小数:decimal
- 字符串:varchar
- 日期和时间:date,time,datetime
- 枚举:enum
三、MySQL常用操作
1、常见语句
select 查询
insert 插入数据
delete 删除语句
update 更新语句
select version() 数据库版本
mysql -uroot -pqwe123 进入MySQL数据库
which python 定位到python目录位置
which mysql 定位到mysql目录位置
show databases; 查看所有库
show tables; 查看库中所有表
create database python50 charset=utf8; 创建一个空的库
drop datebase python50;删除一个库
use (库名) 进入指定的库
---- auto_increment #自动增长
---- not null #不能为空
---- unique key #唯一
---- primary key #主键
---- foreign key #外键
---- default #默认值
---- create table student(id int,name varchar(30)); #创建表
---- create table teacher(id int not null primary key auto_increment,name varchar(30)); #创建一个id为主键非空且自动增长的表
---- desc 表名 #查看表结构
---- insert into student values(1,'lazier'); #选择student表插入表元素
---- insert into student (sex,age) values(‘women’,18),(‘man’,37); #指定字段插入多行数据
---- select * from student; #查看表student的全部内容
---- select * from student where sex='man'; # 根据条件查询指定信息
---- alter table student modify name int(10); #修改字段类型
---- alter table student add age int(10) afer id; #添加字段并指定位置
---- alter table student add category varchar(100); #修改表结构,增加category
---- alter table student change category cate varchar(40) default null; #修改表结构,category改名为cate(默认值为null)
---- alter table 表名 drop 字段名; #删除字段
---- alter table tb drop primary key; #删除主键
---- update student set name='python' where id=1; #将位置id=1的值更新为‘python’
---- delete from student where id=1; #删除id=1的元素
---- delete from student where name='python'; #删除name='python'的所有元素
---- delete from student; #删除表数据(不会删除自增记录)
---- truncate from student; #删除表数据(删除自增记录,重新定义起始数据大小)
2、表关系
1、多对一关系
简介:数据中两个表之前的一种关系,第一个表总的单个行可以与第二个表中的单个行或者多个行相关,但是第二个表的一个行只能与第一个表中的一个行相关
- 外键关联,外键必须建立在多的一方
- 一对多关系的时候,添加数据请先添加少的一方
一个学院可以有多个学生,一个学生只属于一个学院,学生表和学院表就是一对多关系
# 创建学院表
create table college(id int primary key auto_increment,name varchar(30) not null);
# 创建学生表
create table student(
id int primary key not null auto_increment,
name varchar(30) not null,
college_id int not null,
foreign key(college_id) references college(id) on delete cascade on update cascade);
# 查询语句
select * from class as c left join student as s on c.id=s.class_id;
select * from class as c right join student as s on c.id=s.class_id;
查询结果:
2、多对多关系
- 学生可以报名多个课程,一个课程可以被多个学生报名
- 学生表和课程表形成多对多关系
# 创建课程表
create table course(id int primary key not null auto_increment,
title varchar(30) not null);
insert into course(title) values('python_web'),('python_spider'),('python_project');
# 创建选修课 中间表
# 方法一
create table student_course(
stu_id int int not null,
cou_id int not null,
primary key(stu_id,cou_id),
foreign key(stu_id) references student(id) on delete cascade,
foreign key(cou_id) references course(id) on delete cascade);
# 方法二
create table student_course(
stu_id int int not null,
cou_id int not null,
primary key(stu_id,cou_id));
alter table stu_course add constraint fk_student foreign key(stu_id) references student(id);
alter table stu_course add constraint fk_course foreign key(course_id)se_id) references course(id);
3、一对一关系
- 一个人对应一张身份证,一张身份证对应一个人
通过外键 + 唯一
案例:
create table student_detail(
id int primary key not null auto_increment,
stu_id int null unique,hobby varchar(30),
foreign key(stu_id) references student(id) on delete cascade);
insert into student_detail(id int primary key not null auto_increment,
stu_id int null unique,
hobby varchar(30),
foreign key(stu_id) references student(id) on delete cascade);
select * from student as c right join student_detail as s on c.id=s.stu_id;