草庐IT

零基础入门MySQL数据库(全)

Want595 2023-07-13 原文

文章目录

前言

一切从创建数据库开始
数据库→基本表(创建-增删改查)→视图(创建-增删改查)

一、数据类型

1.1 数值型

int #4个字节
smallint #2个字节
tinyint #1个字节
float   #浮点型
numeric/decimal #小数

1.2 字符型

char   #固定长度
varchar    #可变长度
text     #文本

1.3 日期型

datetime   #8个字节
year       #年
date       #天
time       #时间

二、数据库

2.1 创建数据库

create database 数据库名
default character set utf8;

2.2 修改数据库

alter database 数据库名;

2.3 删除数据库

drop database 数据库名;

三、基本表

3.1 表结构

3.1.1 创建表结构

定义

create table 表名
(
列名  数据类型  约束条件,
列名  数据类型  约束条件,
……
);

创建学生表student
唯一性标识 id:整型,自动增长列,主键
学号 sno:9个字符,非空,不允许重复
姓名 sname:20个字符,非空
性别 ssex:2个字符,男或女
年龄 sage:tinyint,15到45之间
系别 sdept:20个字符,默认值为计算机系

create table student
(
id int auto_increment primary key,
sno char(9) not null unique,
sname char(20) not null,
ssex char(2) check(ssex in ('男','女')),
sage smallint check(sage>=15 and sage<=45),
sdept char(20) default '计算机系'
);

创建课程表course
课程号 cno:4个字符 主键
课程名 cname: 50个可变长字符,不允许为空
先修课程号 cpno: 4个字符
学分 ccredit: smallint
先修课程号 cpno 参照 课程号cno 取值

create table course
(
cno char(4) primary key,
cname varchar(50) not null,
cpno char(4),
ccredit smallint,
foreign key(cpno) references course(cno)
);

创建成绩表sc
学号 sno:9个字符
课程号 cno:4个字符
成绩 grade: smallint
sno,cno:组合主键
sno参照student表的sno取值
cno参照course表的cno取值

create table sc
(
sno char(9) not null,
cno char(4) not null,
grade smallint,
primary key(sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
);

3.1.2 修改表结构

定义

alter table 表名 modify 列名 新数据类型 约束;

例:修改student的sdept为40个字符宽度

alter table student modify sdept char(40);

3.1.3 增加列

定义

alter table 表名 add 列名 数据类型 约束;

例:在student中增加address列

alter table student add address varchar(50) not null;

3.1.4 删除列

定义

alter table 表名 drop column 列名;

例:删除student的sage列

alter table student drop column sage;

3.1.5 增加约束

定义

alter table 表名 add constraint 约束名 约束类型(列名);

例:在student中增加sno为主关键字

alter table student add primary key(sno);

例:添加sc的sno列的外码约束为student的sno

alter table sc add foreign key(sno) references studnet(sno);

3.1.6 删除约束

定义

alter table 表名 drop 约束类型 约束名

例:删除student的主键约束

alter table student drop primary key

3.1.7 删除表

定义

drop table 表名

3.2 数据查询

定义

select 查询目标
from 目标所在表
where 条件
group by 分组依据
having 分组条件
order by 排序依据
limit 限制;

3.2.1 简单查询

  1. 查询全体学生的学号、姓名
select sno,sname from student;
  1. 查询全部列
select * from student;
select * from course;
select * from sc;
  1. 查询全体学生的姓名和出生年份
select sname as 'student name',2023-sage 'birthday' from student;

3.2.2 条件(where)

  1. 查询计算机系全体学生的姓名
select sname 
from student 
where sdept='计算机系';
  1. 查询年龄小于20岁的学生的姓名
select sname 
from student 
where sage<20;
  1. 查询计算机系年龄小于20岁的学生的姓名
select sname 
from student 
where sage<20 and sdept='计算机系';
  1. 查询年龄介于22岁到24岁的学生姓名和年龄
select sname,sage 
from student 
where sage>=22 and sage<=24;
select sname,sage 
from student 
where sage between 22 and 24;
  1. 查询信息系、数学系和计算机系学生的姓名和性别
select sname,ssex 
from student 
where sdept='信息系' 
or sdept='数学系' 
or sdept='计算机系';
select sname,ssex 
from student 
where sdept in ('信息系','数学系','计算机系');
  1. 查询未确定系学生的基本信息
select * 
from student 
where sdept is null;
  1. 查询无考试成绩的学生的学号
select sno 
from sc 
where grade is null;
  1. 查询有考试成绩的学生的学号和成绩
select sno,grade 
from sc 
where grade is not null;

3.2.3 消除(distinct)

  1. 查询由哪些学生选修了课程,只需给出学号(消除重复值)
select distinct sno 
from sc;

3.2.4 模糊(like)

  1. 查询姓王的同学的基本信息
select * 
from student 
where sname like "王%";
  1. 查询姓名中有王字的同学的基本信息
select * 
from student
where sname like "%王%";

3.2.5 排序(order by)

  1. 将学生按年龄由小到大排序
select * 
from student 
order by sage asc;
  1. 输出选修c02课程学生的学号和成绩,按成绩降序排序
select sno,grade 
from sc 
where sno='c02' 
order by grade desc;

3.2.6 分组(group by)

  1. 统计学生的总人数
select count(*) 
from student 
group by sno;
  1. 统计选修了课程的学生的人数
select count(distinct sno) 
from sc 
group by sno;
  1. 计算学号为200215121学生的考试成绩总和
select sum(grade) 
from sc 
where sno='200215121' 
group by sno;
  1. 计算c01课程的平均成绩
select avg(grade) 
from sc 
where cno='c01' 
group by cno ;
  1. 查询最大的学生年龄
select max(sage) 
from student 
group by sno;
  1. 统计每门课的平均成绩,输出课程号和平均成绩
select cno,avg(grade) 
from sc 
group by cno;
  1. 统计每门课的选修人数,输出课程号和课程选修人数
select cno,count(*) 
from sc 
group by cno;
  1. 查询每名学生的选课门数和平均成绩
select sno,count(*),avg(grade) 
from sc 
group by sno;
  1. 查询选修了3门以上课程的学生的学号
select sno 
from sc 
group by sno 
having count(*)>3;
  1. 查询选课门数大于等于4门的学生的平均成绩和选课门数
select sno,avg(grade),count(*) 
from sc
group by sno 
having count(*)>=4;
  1. 查询选修门数超过3门,且平均分在70以上的学生的学号,选修门数,平均成绩,并按成绩从高到低排序
select sno,count(*),avg(grade) 
from sc
where grade>=60
group by sno
having count(*)>3 and avg(grade)>70
order by avg(grade) desc;

3.2.7 限制(limit)

  1. 查询平均成绩前三名同学的学号,姓名,平均成绩
select student.sno,student.sname,avg(sc.grade) from student,sc
where student.sno=sc.sno
group by student.sno
order by avg(sc.grade)
limit 3;

3.2.8 单行输出(group_concat)

  1. 查询200215121学生的选课情况,显示:学号,课程号(横向排列成一行,用’,'分隔)
select sno,group_concat(cno) 
from sc 
where sno='200215121';
  1. 查询每个学生的选课情况,显示:学号,课程号(横向排列成一行,用’,'分隔)
select sno,group_concat(cno) 
from sc 
group by sno;
  1. 查询学生的选课情况,要求输出学号,姓名,课程门数,课程名列表(用逗号分隔),按照学号升序排序
select student.sno,student.sname,count(sc.cno),
group_concat(cname order by cname separator ',') 
from sc,student,course 
where student.sno=sc.sno 
and course.cno=sc.cno
group by student.sno
order by sno asc;

3.2.9 多表查询(连接)

  1. 查询每个学生的基本信息及其选课情况
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left join sc on student.sno=sc.sno;
  1. 查询计算机系学生的选课情况,要求输出姓名,课程号,成绩
select student.sname,sc.cno,sc.grade 
from student,sc
where student.sno=sc.sno and sdept='计算机系';
  1. 查询计算机系学生的选课情况,要求输出姓名,课程名,成绩
select student.sname,course.cname,sc.grade 
from student,course,sc
where student.sno=sc.sno 
and course.cno=sc.cno 
and sdept='计算机系';
  1. 查询计算机系学生的选课情况,要求输出姓名,课程名
select student.sname,course.cname 
from student,course,sc
where student.sno=sc.sno 
and course.cno=sc.cno 
and sdept='计算机系';
  1. 查询学生的选课情况,要求输出学号,姓名,课程代号,成绩
select student.sno,student.sname,sc.cno,sc.grade from student,sc
where student.sno=sc.sno;
  1. 查询学生的选课情况,要求输出学号,姓名,课程代号,成绩(包括未选课学生的信息)
select student.sno,student.sname,sc.cno,sc.grade
from student 
left join sc 
on student.sno=sc.sno;
  1. 查询各门课程的先修课名称情况
select a.cno,a.cname,b.cname 
from course a,course b
where a.cpno=b.cno;

3.2.10 子查询(嵌套)

  1. 查询与刘晨在同一个系的学生
select * 
from student 
where sdept=
(select sdept from student where sname='刘晨')
and sname!='刘晨';
  1. 查询年龄最小的同学信息
select * 
from student 
where sage=
(select min(sage) from student);
select * 
from student 
order by sage asc 
limit 1;
  1. 查询有成绩大于90分的学生的学号和姓名
select student.sno,student.sname 
from student,sc
where student.sno=sc.sno and grade>90;
select sno,sname 
from student 
where sno in
(select sno from sc where grade>90);
  1. 查询有成绩大于90分的学生的学号和姓名和成绩
select student.sno,student.sname,sc.grade 
from student,sc
where student.sno=sc.sno and grade>90;
  1. 查询选修了c02课程且成绩高于此课程的平均成绩的学生的学号和成绩
select sno,grade 
from sc 
where grade>
(select avg(grade) from sc where cno='c02' group by cno);
  1. 查询每门课课程且成绩高于此课程的平均成绩的学生的学号和成绩
select a.sno,a.grade 
from sc a 
where grade>
(select avg(grade) from sc b 
where a.cno=b.cno 
group by b.cno);
  1. 查询每个学生成绩高于他选修课的平均成绩的学生的学号和课程号及成绩
select sno,cno,grade 
from sc a 
where grade>
(select avg(grade) 
from sc b 
where b.sno=a.sno);
  1. 查询其他系中比信息系某一学生年龄小的学生姓名和年龄
select sname,sage 
from student 
where sdept!='信息系'and 
sage<
(select max(sage) 
from student 
where sdept='信息系');
select sname,sage 
from student 
where sdept!='信息系' and 
sage<
any(select age 
from student 
where sdept='信息系');
  1. 查询所有选修了c02的学生的姓名
select sname 
from student 
where exists
(select * from sc where cno='c02' and sno=student.sno);
select sname from 
student where sno in 
(select sno from sc where cno='c02');
select sname from sc,student 
where sc.sno=student.sno and sno='c02';
  1. 查询所有未选修c02课程的学生的姓名
select sname from student 
where not exists
(select * from sc where cno='c02' and sno=student.sno);
select sname 
from student,sc
where student.sno=sc.sno and cno!='c02';
  1. 查询所有选修了’数据库’课程的学生的姓名
select sname 
from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno and cname='数据库';
select sname 
from student 
where exists
(select * from sc where cno=(select cno from course where cname='数据库'));

3.2.11 复杂查询(exists)

  1. 查询同时选修了c01和c02课程的学生的学号
select sno from sc where cno='c01' and cno='c02';
select distinct sno 
from sc where 
sno in (select sno from sc where sno='c01') and 
sno in (select sno from sc where sno='c02');
select distinct sno 
from sc a 
where exists
(select * from sc b where b.sno=a.sno 
and cno='c01') and exists
(select * from sc c where c.sno=a.sno 
and cno='c02');
  1. 查询同时选修了’数据库’和’数据结构’课程的学生的学号
select distinct sno from sc a where
exists(select * from sc b where b.sno=a.sno and 
cno=(select cno from course where cname='数据库'))
and exists(select * from sc c where c.sno=a.sno and cno=(select cno from course where cname='数据结构'));
  1. 查询选修了全部课程的学生的姓名(即查询这样的学生,没有一门课是他不选修的)
select sname from student 
where not exists(select * from course
where not exists(select * from sc 
where sno=student.sno and cno=course.cno);
  1. 查询选修了学号为’200215122’学生选修的全部课程的学号
select distinct sno from sc a
where not exists(select * from sc b 
where sno='200215122' and 
not exists(select * from sc c where
c.sno=a.sno and c.cno=b.cno);

3.2.12 组合查询(union)

  1. 查询所有课程名与学生名并将查询列命名为name
(select sname as name from student) 
union
(select cname from course);

3.3 数据更新

3.3.1 插入(insert)

定义

insert into 表名 (列名) values (值列表);

1. 将新生记录为(200821105,陈冬,男,18,信息系)插入到student表中

insert into student (sno,sname,ssex,sage,sdept)
values('200821105','陈冬','男',18,'信息系');
insert student
values('200821105','陈冬','男',18,'信息系');

2. 将与刘晨同一个系的新生记录(200821105,陈冬,男,18)插入到student表中

insert into student set sno='200821105',sname='陈冬',ssex='男',sage=18,
sdept=(select sdept from student where sname='刘晨');

3. 将新生记录(200821107,陈冬,男,18,信息系),(200821118,刘晨,男,18,信息系)一起插入到student表中

insert into student
values('200821107','陈冬','男',18,'信息系'),('200821108','刘晨','男',18,'信息系');

4. 在sc表中插入一新记录学号为200821105,课程为c01

insert into sc (sno,cno) values('200821105','c01',null);
insert into sc values('200821105','c01',null);

5. 备份c01的成绩到c01_cj新表中

create table c01_cj like sc;
insert into c01_cj
select * from sc where cno='c01';

3.3.2 修改(update)

定义

update 表名 set 列名=表达式 where 条件;

1. 将计算机系全体学生的成绩加5分

update sc set grade=grade+5
where sno in (select sno from student where sdept='计算机系');

2. 将平均成绩80分以上的学生成绩加3分

update sc set grade=grade+3 
where sno in
(select sno from sc group by sno having avg(grade)>80);

3. 将数据结构3-5名学生的成绩加5分

update sc set grade=grade+5 where sno=
(select sno from course where course.cno=sc.cno and cname='数据结构'
order by grade desc limit 2,4);

3.3.3 删除(delete)

定义

delete from 表名 where 条件;

1. 删除学号为200215121的学生的记录

delete from sc where sno='200215121';
delete from student where sno='200215121';

2. 删除所有学生的记录

delete from student;

3. 删除计算机系所有学生的选课记录

delete from sc where sno in(select sno from student where sdept='计算机系');

四、视图

4.1 创建视图

定义

create view 视图名 as 子查询;

1. 建立系名为计算机系的学生的视图

create view student_view as
select sno,sname,ssex,sdept from student 
where sdept='计算机系';

2. 建立系名为计算机系的学生的视图,要求对视图的更新进行检查

create view student_view as
select sno,sname,ssex,sdept 
from student 
where sdept='计算机系'
with check option;

3. 建立信息系选修了c01课程的学生的视图

create view student_view as
select student.sno,student.sname,sc.grade
from student,sc 
where student.sno=sc.sno 
and sdept='信息系' 
and cno='c01';

4.2 其他

同基本表操作一样

总结

数据库→基本表(创建-增删改查)→视图(创建-增删改查)

有关零基础入门MySQL数据库(全)的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  3. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  4. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  5. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  6. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  7. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

  8. STM32读取串口传感器数据(颗粒物传感器,主动上传) - 2

    文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,

  9. LC滤波器设计学习笔记(一)滤波电路入门 - 2

    目录前言滤波电路科普主要分类实际情况单位的概念常用评价参数函数型滤波器简单分析滤波电路构成低通滤波器RC低通滤波器RL低通滤波器高通滤波器RC高通滤波器RL高通滤波器部分摘自《LC滤波器设计与制作》,侵权删。前言最近需要学习放大电路和滤波电路,但是由于只在之前做音乐频谱分析仪的时候简单了解过一点点运放,所以也是相当从零开始学习了。滤波电路科普主要分类滤波器:主要是从不同频率的成分中提取出特定频率的信号。有源滤波器:由RC元件与运算放大器组成的滤波器。可滤除某一次或多次谐波,最普通易于采用的无源滤波器结构是将电感与电容串联,可对主要次谐波(3、5、7)构成低阻抗旁路。无源滤波器:无源滤波器,又称

  10. SPI接收数据异常问题总结 - 2

    SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手

随机推荐