day29
总结
-
sql 第一天
数据库: 数据的仓库 (集散地) , 它解决了数据持久化和数据管理的问题 持久化 ---> 将数据从内存转移到硬盘(可以长久保存数据的储存介质) 数据库的分类: 1972 ---> Codd ---> 如何使用关系模型来保存大规模数据 ~ 关系型数据库 - 理论基础: 关系代数、集合论 - 具体表象: 用二维表来保存数据(行(记录)和列(字段)) - 编程语言: SQL (结构化查询语言) ---> SQL方言 ~ 非关系型数据库 - NoSQL ---> No SQL ---> No, SQL ---> Not Only SQL - NewSQL ---> 保存数据的方式可能完全不同于传统的关系型数据库,但是允许使用关系型数据库的编程语言操作/获取数据 ~ Hadoop生态圈 ---> Drill / Hive / PIG - Hive ---> HQL ---> (和MySQL中使用的SQL无限雷同) 关系型数据库的产品: ~ Oracle ---> Oracle ---> 金融、证券、电商、电子政务s ---> 好、贵 ---> No.1 ~ MySQL ---> GPL ---> 社区版 ---> No.2 ---> MariaDB ~ PostgreSQL / IBM DB2 / Microsoft SQLServer cmd----> mysql -u root -h host -p pwd:******* SQL(Strucutured Query Language) ---> 结构化查询语言 ~ DDL (数据定义语言) ----> 创建、删除、修改(各种对象) ----> create / drop / alter ~ DML (数据操作语言) ----> 插入、删除、修改数据 ----> insert / delete / update ~ DQL (数据查询语言) ----> 检索(查询) 数据 ---> select ~ DCL (数据控制语言) ----> 授予或者召回用户权限 ---> grant / revoke SQL是不区分大小写的编程语言 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 ~ 小数: float / double / decimal ~ 时间日期: time / date / datetime / timestamp ~ 字符串: char / varchar ~ 大对象: longtext / longblob ---> 4G
-
excercise
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 change column stu_gender stu_gender boolean default 1 comment '性别'; -- 修改列的数据类型 alter table tb_student modify stu_gender char(1) default '男' comment '性别';
create table tb_college ( col_id int unsigned auto_increment comment '学院编号', col_name varchar(20) not null comment '学院名', col_introduce varchar(80) not null comment '学院介绍', col_location varchar(20) not null comment '学院位置', primary key (col_id) ) engine=InnoDB auto_increment=11 comment '学院表'; drop table if exists tb_college; alter table tb_college modify column col_introduce varchar(500) not null 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); alter table tb_student drop constraint fk_student_col_id;
作业
use school;
create table tb_course (
cou_id int unsigned auto_increment comment '编号',
cou_name varchar(16) not null comment '名称',
cou_score decimal not null comment '学分',
cou_start_date date comment '开课时间',
cou_cls_hour smallint unsigned not null comment '课时',
tch_id int unsigned not null comment '授课老师',
primary key (cou_id),
key (tch_id),
constraint fk_course_tch_id foreign key(tch_id) references tb_teacher (tch_id)
) engine=innodb comment '课程表';
use school;
create table tb_course (
cou_id int unsigned auto_increment comment '编号',
cou_name varchar(16) not null comment '名称',
cou_score decimal not null comment '学分',
cou_start_date date comment '开课时间',
cou_cls_hour smallint unsigned not null comment '课时',
tch_id int unsigned not null comment '授课老师',
primary key (cou_id),
key (tch_id),
constraint fk_course_tch_id foreign key(tch_id) references tb_teacher (tch_id)
) engine=innodb comment '课程表';
USE school;
CREATE TABLE tb_course_selection_info (
cs_id INT UNSIGNED auto_increment COMMENT '选课编号',
cou_id INT UNSIGNED NOT NULL COMMENT '课程编号',
cs_date date NOT NULL COMMENT '选课日期',
stu_id INT UNSIGNED NOT NULL COMMENT '学生id',
stu_score SMALLINT COMMENT '成绩',
PRIMARY KEY (cs_id),
KEY (stu_id),
KEY (cou_id),
CONSTRAINT fk_course_selection_stu_id FOREIGN KEY (stu_id) REFERENCES tb_student (stu_id),
CONSTRAINT fk_course_selection_cou_id FOREIGN KEY (cou_id) REFERENCES tb_course (cou_id)
) ENGINE = INNODB COMMENT '选课表';