一、数据库的基础知识
1.数据库:数据的仓库(集散地),它解决了数据持久化和数据管理的问题
2.持久化 —> 将数据从内存转移到硬盘(可以长久保存数据的存储介质)
3.数据库的分类:
1972—> Codd —> 如何使用关系模型来保存大规模数据
~关系型数据库 —>首选方案
-理论基础:关系代数、集合论
-具体表象:用二维表保存数据(行(记录)和列(字段))
-编程语言:SQL(结构化查询语言)—> SQL方言
~非关系型数据库
~ NoSQL —> No SQL —> No, SQL —> Not Only SQL
~ NewSQL —> 保存数据的方式可能完全不同于传统的关系型数据库,但是允许使用关系型数据库的编程语言操作/获取数据
~ Hadoop生态圈 —> Hive —>HQL—>跟MySQL中使用的SQL无限雷同
-
关系型数据库的产品:
-
Oracle (甲骨文) —> Oracle —> 金融、证券、电商、电子政务 —>好、贵 —> No.1
~MySQL —> GPL —> 社区版 —> No.2 —> MariaDB - PostgreSQL / IBM DB2 / Microsoft SQLServer
二、MySQL的使用
-
SQL(Strucutured Query Language) —>结构化查询语言
- DDL(数据定义语言) —> 创建删除修改各种对象—>creat/drop/alter
- DML(数据操作语言)—> 插入、删除、修改数据 —> insert/delete/update
- DQL(数据查询语言)—> 检索(查询)数据 —> select
- DCL(数据控制语言)—> 授予或者召回用户权限 —> grant/revoke
SQL是不区分大小写的编程语言 —> create / CREATE
-- 0. 查看所有数据库
show databases;
-- 1. 创建数据库
create database school default charset utf8mb4;
-- 2. 删除数据库
drop database if exists school;
-- 3. 查看创建数据库的过程
show create database school;
-- 4. 切换到指定的数据库
use school;
-- 5. 显示数据库中所有的表
show tables;
-- 6. 创建二维表
create table tb_student
(
stu_id int unsigned not null comment '学号',
stu_name varchar(20)not null comment '姓名',
stu_gender boolean default 1 comment '性别',
stu_birth date comment '出生日期',
primary key(stu_id)
) engine=innodb comment '学生表';
-- 主键(primary key):能够唯一确定一条记录的列
数据类型:
~整数: int(integer)/bigint/smallint/tinyint —> unsigned(无符号整数,只表示0和正数)
~小数:float/double/decimal
~时间日期:time/date/datetime/timestamp
~字符串:char(10)(定长字符串:开辟一个10字符的空间)/varchar(10)(变长字符串,根据字符来定义空间)
~大对象:longtext(放文本)/longblob(放二进制)—> 单列可放4G的内容
not null:非空约束
default 1:默认值约束
关系的重数
学生表 <-------从属 ---->学院表
(多) (一)
use school;
-- 删除表
drop table if exists tb_student
-- 创建表
create table tb_student
(
stu_id int unsigned not null comment '学号',
stu_name varchar(20) not null comment '姓名',
stu_gender boolean default 1 comment '性别',
stu_birth date comment '出生日期',
primary key(stu_id)
) engine=innodb comment '学生表';
-- 修改表
-- 添加一个列
alter table tb_student add column stu_addr varchar(50) default '' comment '籍贯';
-- 删除一个列
alter table tb_student drop column stu_addr;
-- 修改一个列(修改列的名字)
alter table tb_student change column stu_gender stu_sex boolean default 1 comment '性别';
alter table tb_student change column stu_sex stu_gender boolean default 1 comment '性别';
-- 修改一个列(修改列的数据类型)
alter table tb_student modify column stu_gender char(1) default '男' comment '性别';
-- 创建学院表
create table tb_college
(
col_id int unsigned not null comment '学院代码',
col_name varchar(20) not null comment '学院名',
col_intro varchar(500) default '' comment '学院介绍',
primary key(col_id)
)engine=InnoDB comment '学院表';
alter table tb_college modify column col_id int unsigned auto_increment comment '学院代码';
-- 修改学生表添加一个列来维护学生对学院的多对一关系
-- 多对一关系都是在多的一方添加一个列来维护
alter table tb_student add column col_id int unsigned not null comment '学院编号';
-- 修改学生表添加一个外键约束,限制学生表中的学院编号必须参照学院表的学院编号
alter table tb_student add constraint fk_student_col_id foreign key (col_id) references tb_college (col_id);
-- 老师表(工号、姓名、性别、职称、所属学院)
create table tb_teacher
(
tc_id int unsigned not null comment '工号',
tc_name varchar(20) not null comment '姓名',
tc_gender char(1) default '男' comment '性别',
tc_title varchar(20) default '' comment '职称',
col_id int unsigned not null comment '学院编号',
primary key(tc_id)
)engine=InnoDB comment '老师表';
alter table tb_teacher add constraint fk_teacher_col_id foreign key (col_id) references tb_college (col_id);
-- 课程表(编号、名称、学分、开课时间、课时数、授课老师)
create table tb_course
(
cs_id int unsigned not null comment '编号',
cs_name varchar(20) not null comment '名称',
cs_credit int unsigned not null comment '学分',
cs_curriculum_time datetime not null comment '开课时间',
cs_hours int unsigned not null comment '课时',
cs_teather varchar(20) not null comment '授课老师',
primary key(cs_id)
)engine=InnoDB comment '课程表';
alter table tb_course add constraint fk_course_cs_teather foreign key (cs_id) references tb_teacher (tc_id);
-- 选课记录表(选课编号、学号、课程号、选课日期、考试成绩)
create table tb_course_record
(
cr_id int unsigned not null comment '选课编号',
cr_stu_id int unsigned not null comment '学号',
cr_cs_id int unsigned not null comment '课程编号',
cr_date date not null comment '选课日期',
cr_score int unsigned default 0 comment '考试成绩',
primary key(cr_id)
)engine=InnoDB comment '选课记录表';
alter table tb_course_record add constraint fk_course_record_cr_stu_id foreign key (cr_id) references tb_student(stu_id);
alter table tb_course_record add constraint fk_course_record_cr_cs_id foreign key (cr_id) references tb_course(cs_id);