Mysql

Mysql

1.简介

1.1介绍

1.外码

F是表A的主码,是表B的属性(但不是码),F是B的外码

1.2说明

管理员的账号和密码都是root

1.3workbench使用

  1. 点一个加号直接默认创建就行,起个名
  2. 然后就可以打了
  3. 创建数据库
create database pra;
use pra;

2.基本使用

use口令:选择数据库

show口令:展示内容

表:

创建:create table

删除:drop table

修改:alter table

3.创建表

3.1简单示例

use pra;
create table student1
(
	stu_id		int 		not null auto_increment,
    stu_name 	char(50) 	not null,
    stu_address char(50) 	null,
    primary key (stu_id)
);

解释:

1.必须先指定哪个数据库;

2.指定主键

主键可以以多个组组成

3.not null是不能有空值 null可以有

4.auto_increment:

auto_increment告诉mysql,本列每当增加一行时自动增量。

3.2指定默认值

示例:

create table student2
(
	stu_id		int 		not null auto_increment,
    stu_name 	char(50) 	not null,
    stu_address char(50) 	null,
    stu_g		int			not null default 1,
    primary key (stu_id)
);

解释:default 1表示默认值

3.3多个表格连接

示例:

use pra;
create table student
(sno char(9) primary key,
sname char(20) unique,
ssex char(2),
sage smallint,
sdept char(20)
);
create table course
(cno char(4) primary key,
cname char(40),
cpno  char(4),
ccredit smallint,
foreign key(cpno) references course(cno)
);
create table sc
(sno char(9),
cno char(4),
grade smallint,
primary key (sno,cno),
foreign key (sno) references student(sno),
-- sno是外码 被参照表是student
foreign key (cno) references course(cno)
-- cno是外码 被参照表是course
);

4.修改表

1.示例:

alter table student add scome date;
drop table student1 cascade;

说明:

第一句,在表格里面添加列

第二句,无限制删除一个表格,如果要判断一下是否能删除,把cascade改成restrict。

5.检索数据

5.1简单select

1.检索列:

select stu_name from student1;
select * from student1;

2.查询经过计算的值

select sname,2016-sage from student;
-- 查询2016 - sage的结果 
select sname,'year of birth',2016-sage,lower(sdept)
from student;
-- 查询全体学生的姓名,出生年份所在院系,要求用小写字母表示系名
select sname sNAME,'year of birth:' birth,
2016-sage birthday,lower(sdept) department from student;
-- 修改一些显示出来的列的名字

5.2有条件的查询

1.常用的查询条件(用where实现)

表格P37

select * from student where sdept='CS';
-- 查询计算机系的学生 
select * from student where sage between 20 and 23;
-- 查询这个闭区间的学生

in:可以用来查找属性值属于指定集合的元组

select sname,ssex from student where sdept in ('IS','CS');

like:可以用来查找与字符串匹配的指定集合的元组

select * from student where sno like '201619001';
select * from student where sname like '李%';
select * from student where sname like '刘_';
select * from student where sname not like '刘%';

说明:% 和 _ 都是填充符号 但是%可以表示N个字符 _ 只能表示一个字符

最后一条是查询不姓刘的人

2.涉及空值的查询

is null / is not null

select * from sc where grade is null;
select * from sc where grade is not null;

3.多重条件查询

逻辑运算符and / or ,and优先级高于or 用括号可以改变优先级

select * from student where sdept='CS' and sage<23;

5.3排序检索数据

第一句:正常排序

第二句:按照两列排序

第三句:倒序排序

第四句:可以指明哪些是倒序

SELECT * FROM pra.student1 order by stu_id;
SELECT * FROM pra.student1 order by stu_id,stu_name;
SELECT * FROM pra.student1 order by stu_id desc;
SELECT * FROM pra.student1 order by stu_id desc,stu_name;

5.4聚集函数

1.常用聚集函数:

count([distinct|all] *)
count([distinct|all] <列名>)
sum([distinct|all] <列名>)
avg([distinct|all] <列名>)
max([distinct|all] <列名>)
min([distinct|all] <列名>)

distinct:计算时取消指定列中的重复值

示例:

select count(distinct sno) from student;
-- 多少学生
select avg(grade) from sc where cno = '3';
-- 计算平均成绩

5.5对查询结果分组

group by:将查询结果中各行按照一列或者多列取值相等的原则进行分组

having:进行筛选,分组后再筛选

where:分组前筛选

示例:

select cno,count(sno)
from sc
group by cno;
-- 求每个课程号及对应的选课人数

select *
from sc
group by sno
having count(*)>2;
-- 查询选修了两门以上课程的学生学号
-- 先查找分类之后选择条件

5.6连接查询△

1.概念:同时查询两个以上的表称为连接查询

格式:

[表1] [列1] [比较运算符] [表2] [列2]

或:[表1] [列1] between [表2] [列2] and [表3] [列3];

2.等值和非等值连接查询

原理:首先在表1中找到第一个元组,然后扫描表2,

select student.*,sc.* from student,sc where student.sno=sc.sno;
-- 查询每个学社及其选修课程的情况

3.自身连接查询

示例:

select first.cno, second.cpno
from course first,course second
where first.cpno = second.cno;

说明:

first和second是把同一个表复制成两个然后连接,查找第二个表中跟第一个先修课一样的元组,然后输出这个元组的先修课,就是先修课的先修课。

4.多表连接查询:

示例:

select student.sno,sname,cname,grade
from student,sc,course
where student.sno = sc.sno
and sc.cno = course.cno;
-- 查询每个学生的学号姓名选修的课程和成绩

5.复合条件查询

示例:

select student.sno,sname
from student,sc
where student.sno = sc.sno
and sc.cno = '2' and sc.grade > 80;
-- 查询选修2号课程且成绩在80分以上的学生

5.7嵌套查询

1.定义:

一个select-from-where语句称为一个查询块。将一个查询块嵌套在另一个查询块的where或者having里面叫嵌套查询或者子查询。

示例:

use pra;
select sname from student where sno in(select sno
from sc where cno='2');

注意:子查询的select不允许有order by 子句,order by 只能对最终的结果排序

2.in:父查询和子查询之间用in连接,判断某个属性列值是否在查询的结果中。

示例:

select sno,sname,sdept
from student
where sdept in(select sdept from student where sname='刘晨');
-- 查询和刘晨在一个系学习的学生

3.使用any和all

说明:使用any和all必须同时使用比较运算符,而使用any或all谓词时必须同时使用比较运算符。

示例:

select sname,sage
from student
where sage < all (select sage
					from student
					where sdept = 'IS')
				and sdept != 'IS';
-- 查询其他系中比信息系所有学生年龄都小的学生名单

select sname,sage from student
where sage < (select min(sage) from student where sdept='IS')
and sdept != 'IS';
-- 聚集函数的写法

3.exist谓词的子查询

说明:

内层查询结果为空,则返回false

内层查询结果非空,则返回true

select sname from student
where exists
		(select * 
        from sc where sno=student.sno and cno='1');
-- 查询所有选修了一号课程的同学的名字
select sname from student
where no exists
		(select * 
        from sc where sno=student.sno and cno='1');
-- 查询所有没选一号课程的同学的名字

5.8集合查询

说明:集合的运算

6.数据存储操作

6.1插入

示例:

insert into student (sno,sname,ssex,sdept,sage)
values ('201619006','程丽','女','CS',18);

insert into student values ('201619007','王齐','男',18,'CS');
-- 学生信息插入

插入记录集:略

6.2修改

示例:

update student
set sage=22
where sno = '201619007';
-- 改下wq的年龄

SET SQL_SAFE_UPDATES = 0;
update student set sage=sage-1;
-- 安全模式下这个是不允许的 所以得加上第一句

update sc
set grade = 0 where 'CS'= (select sdept from student where student.sno=sc.sno);
-- 把计算机所有同学的成绩置零

6.3删除

示例:

delete from student where sno = '201619007';

带子查询的删除

posted @   wlqtc  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示