数据库定义与操作语言 Mysql

本文介绍了数据库的定义,并详细讲解了MySQL中的数据操作,包括创建和删除数据库(尽管MySQL不支持模式)、创建和删除表、数据查询(计算值、有条件查询、分组统计、多表连接、自身连接等)、子查询、数据更新(插入、更新、删除)以及视图的创建、使用和删除。内容涵盖基础查询到高级查询技巧,是学习MySQL操作的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库的定义

创建数据库

CRAETE DATABASE test;

在这里插入图片描述
删除数据库

DROP DATABASE test;

在这里插入图片描述

SQL中创建模式的语句:CRAETE SCHEMA <模式名> AUTHORIZATION <用户名>;
SQL中删除模式的语句:DROP SCHEMA <模式名><CASCADE|RESTRICT>
但是MySQL中没有模式,因此无法创建。

创建表

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) not null,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),foreign key (Cno) references Course(Cno));

在这里插入图片描述
删除表

DROP table Student;

在这里插入图片描述

数据基本查询

select sno,sname from student;

在这里插入图片描述
查询经过计算的值:

select sname,2019-sage from student;

在这里插入图片描述
有条件的查询:

select sname from student where sdept='CS' and sage<20;

在这里插入图片描述

分组统计查询:

select sdept,avg(sage) from student group by sdept;

在这里插入图片描述
多表连接查询:

select student.*,sc.* from student,sc where student.sno=sc.sno;

在这里插入图片描述
自身连接查询:
先修课的先修课

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

在这里插入图片描述
外连接:

select student.*,cno,grade from student left outer join sc on (student.sno=sc.sno);

在这里插入图片描述
实验1.3 数据高级查询
不相关子查询:

select sname from student where sno in (select sno from sc where cno='1');

在这里插入图片描述
相关子查询:
找出每个学生超过他自己选修课程平均分的课程号

select sno,cno from sc x where grade>=(select avg(grade) from sc y where y.sno=x.sno);

在这里插入图片描述
带有any的查询
查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄:

select sname,sage from student where sage<any(select sage from student where sdept='cs') and sdept<>'cs';

在这里插入图片描述
带有exists的子查询:
查询所有选修了1号课程的学生的姓名

select sname from student where exists (select * from sc where sno=student.sno and cno='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));

在这里插入图片描述
集合查询:
查询计算机科学系的学生与年龄不大于19岁学生的交集

select * from student where sdept='cs' union select * from student where sage<=19;

在这里插入图片描述
实验1.4 数据更新
插入一个学生信息:

insert into student values('201215126','张成民','男','18','CS');

在这里插入图片描述
插入一条课程记录:

insert into course values('1','数据库',null,4);

在这里插入图片描述
插入一条选课记录:

insert into sc values('201215126','1',null);

在这里插入图片描述

插入子查询结果:

create table dept_age(sdept char(15),avg_age smallint);
insert into dept_age(sdept,avg_age) select sdept,avg(sage) from student group by sdept;

在这里插入图片描述
更新数据

update student set sage=22 where sno='201215126';

在这里插入图片描述
将所有学生的年龄加一:

update student set sage=sage+1;

在这里插入图片描述

将所有计算机系学生成绩置零;

update sc set grade=0 where sno in (select sno from student where sdept='CS');

在这里插入图片描述

删除一条数据:

delete from student where sno='201215128';

在这里插入图片描述
带子查询的删除:

delete from sc where sno in (select sno from student where sdept='EE');

在这里插入图片描述
实验1.5 视图
创建带with check option的视图
建立信息系学生的视图:

create view IS_student as select sno,sname,sage from student where sdept='IS' with check option;

在这里插入图片描述
创建分组视图:

create view s_g(sno,gavg) as select sno,avg(grade) from sc group by sno;

在这里插入图片描述
删除视图

drop view s_g;

在这里插入图片描述
查询视图:

select sno,sage from is_student where sage<=20;

在这里插入图片描述
with check option验证

create view seniorstu(sno,sage) as select sno,sage from student where sage>='19' with check option;
insert into seniorstu values('201215127',18);
insert into seniorstu values('201215127',21);

在这里插入图片描述
更新不可更新视图

update s_g set gavg=90 where sno='201215121';

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值