数据库
这里的所有操作,都以mysql为主
我这里不介绍简单的SQL语句了,但是可以提供简单sql语句的训练题目和答案,如果都练过的话,我相信,简单的 sql 语句将不在话下
数据库介绍
数据库安装,配置什么的就不讲了,网上都有
这里,就介绍一下三大范式
数据库三大范式
1NF
同一数据表,不能出现相同的列名
2NF
每行数据唯一可分(加上一个主键)
3NF
一张表中不包含已经存在的其他表中已包含的非主关键字信息
比如有两张表一张是学生表,一张是学生信息表,如果学生表中包含了学生姓名,那学生信息表,就不要包含学生姓名了
SQL语言
语言分类
- DDL(Data Definition Language):数据定义语言,用来定义数据库对象:库、表、列等。
- DML(Data Manipulation Language):数据操作语言,用来定义数据库记录(数据)增删改。
- DCL(Data Control Language):数据控制语言,用来定义访问权限和安全级别。
- DQL(Data Query Language):数据查询语言,用来查询记录(数据)查询。
倒霉催 Q
我这里给几个简单SQL语句的练习,看了就会了:
# 年级表
drop table if exists `grade`;
create table `grade` (
`gradeid` bigint not null comment '年级 id',
`name` varchar(50) comment '年级名称',
primary key (`gradeid`)
) engine=innodb default charset =utf8mb4 comment '年级表';
# 成绩
drop table if exists `score`;
create table `score` (
`scoreid` bigint not null comment '成绩id',
`stuno` bigint not null comment '学员编号',
`subjectid` bigint not null comment '科目 id',
`score` int(10) not null comment '分数',
`examtime` datetime not null comment '考试时间',
primary key (`scoreid`)
) engine=innodb default charset =utf8mb4 comment '成绩表';
# 学生表
drop table if exists stu;
create table `student` (
`stuid` bigint not null comment '学生 id',
`stuname` varchar(50) comment '学生名称',
`password` varchar(50) comment '登录密码',
`sex` varchar(10) comment '性别',
`gid` bigint not null comment '年级id',
`telphone` varchar(50) comment '电话',
`address` varchar(255) comment '地址',
`birthday` date comment '出生日期',
`email` varchar(50) comment '邮箱',
primary key (`stuid`)
) engine=innodb default charset =utf8mb4 comment '学生表';
# 科目表
drop table if exists `subject`;
create table `subject` (
`subjectid` bigint not null comment '科目 id',
`subjectname` varchar(50) comment '科目名称',
`studycount` int comment '课时',
`gradeid` bigint not null comment '年级 id',
primary key (`subjectid`)
) engine=innodb default charset =utf8mb4 comment '科目表';
-- 1. grade 表增加一个阶段,“就业期”
insert into grade value (1,'就业期');
-- 2.将第三阶段的学生的 gradeid 改为就业期的 id
update stu set gid=(select gradeid from grade where name='就业期') where gid=3;
-- 3.查询所有得了 100 分的学号
select stuno from score where score=100;
-- 4.查询所有 1989 年出生的学生(1989-1-1~1990-1-1)
select * from stu where birthday between '1989-1-1' and '1990-1-1';
-- 5.查询学生姓名为“金蝶”的全部信息
select * from stu where stuname='金蝶';
-- 6.查询 subjectid 为 8 的科目考试未及格(60 分)的学号和成绩
select stuno,score from score where scoreid=8 and score<60;
-- 7.查询第 3 阶段课时大于 50 的课程全部信息
select * from subject where gradeid=3 and studycount