第三章 关系数据库标准语言SQL

1.SQL功能

数00据查询select

数据定义create drop alter

数据操纵 insert update delete

数据控制 grant revoke

2.数据定义

a.模式的定义和删除

为用户zhang创建一个模式test

create scheme test authorization ZHANG

删除模式test

drop scheme test cascade(级联)

drop scheme test restrict(限制)

b.基本表的定义、删除与修改

建立一个学生表Student

Create table student(

Sno char(9) primary key,

Sname char(20) unique,

Ssex char(2),

Sage smallint,

Sdept char(20)

);

建立一个课程表Course

Create table course(

Cno char(4) primary key,

Cname char(40) not null,

Cpno char(4),

Ccredit smallint,

Foreign key(cpno) preferences course(Cno)

);

建立学生选课表SC

Create table sc(

Sno char(9),

Cno char(4),

Grade smallint,

Primary key(Sno,Cno),

Foreign key(Sno) references student(Sno),

Foreign ket(Cno) references course(Cno)

);

删除student表

Drop table student cascade

c.索引建立、修改与删除

为学生-课程数据库中的student,course和sc三个表建立索引。其中student表按学号升序建唯一索引,course表按课程号升序建唯一索引,sc表按照学号升序和课程号降序建唯一索引。

Create unique index stusno on student(sno);

Create unique index coucno on course(cno);

Create unique inde  scno on sc(sno asc,cno desc)

将SC表的scno索引名改为scsno

Alter index scno rename to scsno

删除student表的stusname索引

Drop index stusname

3.数据查询

查询全体学生的学号与姓名

Select sno,sname from student;

查询全体学生的姓名、学号、所在系

Select sname,sno,sdept from student;

查询全体学生的详细记录

Select * from student;

查询全体学生的姓名及其出生年份

Select sname,sbirth from student;

查询全体学生的姓名、出生年份和所在的院系

Select sname,sbirth,lower(sdept) from student;

查询选修了课程的学生学号

Select discinct sno from sc;

查询计算机科学系全体学生的名单

Select sname from student where sdept=’CS’

查询所有年龄在20岁以下的学生姓名及其年龄

Select sname,sage from student where sage<20;

查询考试成绩不及格的学生的学号

Select distinct sno from sc where grade<60

查询年龄在20-23岁之间的学生姓名、系别和年龄

Select sname,sdept,sage from student where sage between 20 and 23

查询年龄不在20-23岁之间的学生姓名、系别和年龄

Select sname,sdept,sage from student where sage not between 20 and 23

查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别

Select sname,ssex from student where sdept in(‘CS’,’MA’,’IS’);

查询既不是计算机科学系、数学系,也不是信息系的学生的姓名和性别

Select sname,ssex from student where sdept not in(‘CS’,’MA’,’IS’);

查询学号为201215121的学生的详细情况

Select * from student where sno like ‘201215121’

查询所有姓刘的学生的姓名、学号和性别

Select sname,sno,ssex from student where sname like ‘刘%’

查询姓欧阳且全名为三个汉字的学生的姓名

Select sname from student where sname like ’欧阳_’

查询名字中第二个字为阳的学生的姓名和学号

Select sname,sno from student where sname like ’_阳%’

查询所有不姓刘的学生的姓名、学号和性别

Select sname,sno,ssex from student where sname not like ‘刘%’

查询DB_Design课程的课程号和学分

Select cno,ccredit from course where cname like ‘DB\_Design’ escape ‘\’

查询以“DB_”开头,且倒数第三个字符为i的课程的详细情况

Select * from course where cname like ‘DB\_%i__’ escape ‘\’

某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩,查询缺少成绩的学生的学号和相应的课程号

Select sno,cno from sc where grade is null

查所有有成绩的学生学号和课程号

Select sno,cno from sc where grade is not null

查询计算机科学系年龄在20岁以下的学生姓名

Select sname from student where sdept=’cs’ and sage<20

查询选修了3号课程的学生的学号及其成绩,查询结果按分数的降序排列

Select sno,grade from sc where cno=’3’ order by grade desc

查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列

Select * from student order by sdept,sage desc;

查询学生总人数

Select count(*) from student

查询选修了课程的学生人数

Select count(distinct sno) from sc

计算选修1号课程的学生平均成绩

Select avg(grade) from sc where cno=’1’

查询选修1号课程的学生最高分数

Select max(grade) from sc where cno=’1’

查询学生201215012选修课程的总学分数

Select sum(credit) from sc,course where sno=’201215012’ and sc.cno=course.cno

求各个课程号及其相应的选课人数

Select cno,count(sno) from sc group by cno

查询选修了三门以上课程的学生学号

Select sno from sc group by sno having count(*)>3

查询平均成绩大于等于90分的学生学号和平均成绩

Select sno,avg(grade) from sc group by sno having avg(grade)>=90

查询选修2号课程且成绩在90分以上的所有学生的学号和姓名

Select student,sno,sname from student,sc where student.sno=sc.sno and sc.cno=’2’ and sc.grade>90

查询与“刘晨”在同一个系学习的学生

Select sno,sname,sdept from student where sdept in( select sdept from student where sname=’刘晨’)

Select s1.sno,s1.sname,s1.sdept from student s1,student s2 where s1.sdept=s2.sdept and s2.sname=’刘晨’

查询选修了课程名为“信息系统”的学生学号和姓名

Select student.sno,sname from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and course.cname=’信息系统’

找出每个学生超过他自己选修课程平均成绩的课程号

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

查询选修了课程1或者选修了课程2的学生

Select sno from sc where cno=’1’ union select sno from sc where cno=’2’

查询计算机科学系的学生与年龄不大于19岁的学生的交集

Select * from student where sdept=’cs’ intersect select * from student where sage<=19

查询计算机科学系的学生与年龄不大于19岁的学生的差集

Select * from student where sdept=’cs’ except select * from student where sage<=19

4.数据更新

插入操作:

将一个新学生元组(学号:201215128,姓名:陈东,性别:男,所在系:IS,年龄:18岁)插入到student表中

Insert into student(sno,sname,ssex,sdept,sage) values(‘201215128’,’陈东’,’男’,’IS’,18)

将学生张成民的信息插入到student表中

Insert into student values(‘201215126’,’张成民’,’男’,18,’CS’)

插入一条选课记录(‘201215128’,’1’)

Insert into sc(sno,cno) values(‘201215128’,’1’)

对每一个系,求学生的平均成绩,并把结果存入数据库

create table dept_age( sdept char(15) avg_age smallint);

insert into dept_age(sdept,avg_age) selct sdept,avg(sage) from student group by sdept;

修改数据

将学生201215121的年龄修改为22岁

update student set sage=22 where sno=’201215121’

将所有学生的年龄都增加1岁

update student set sage=sage+1;

将计算机科学系全体学生的成绩置0

update sc set grade=0 where sno in (select sno from student where sdept=’cs’)

删除记录

删除学号为201215128的学生记录

delete from student where sno=’201215128’

删除所有学生的选课记录

delete from sc;

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

delete from sc where sno in(select sno from student where sdept=’cs’)

5.视图的建立、修改和删除

建立视图

建立信息系学生的视图

create view is_student as select sno,sname,sage from student where sdept=’is’

建立信息系学生的视图,并要求进行修改和删除操作时仍需保证该视图只有信息系的学生

create view is_student as select sno,sname,sage from student where sdept=’is’ with

check_option

建立信息系选修了1号课程的学生的视图(包括学号、姓名、成绩)

create view is_si(sno,sname,grade) as select student.sno,sname,grade from student,sc where sdept=’is’ and student.sno=sc.sno and sc.cno=’1’

建立信息系选修了1号课程且成绩在90分以上的学生的视图

create view is_s2 as select sno,sname,grade from is_s1 where grade>90

将学生的学号及平均成绩定义为一个视图

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

删除视图

删除视图BT_S和视图IS_S1

drop view bt_s

drop view is_s1 cascade

数据库系统概述--关系数据库标准语言SQL相关推荐

  1. 【数据库】关系数据库标准语言SQL

    目录 SQL的特点 SQL的基本概念 数据定义 create table创建基本表 alter table修改基本表 drop table删除基本表 创建.删除索引 单表查询 常用的符号: where ...

  2. 【数据库系统设计】关系数据库标准语言SQL(3)

    关系数据库标准语言SQL 数据更新 插入数据 插入元组 插入子查询结果 修改数据 修改某一个元组值 删除数据 删除某一个元组的值 删除多个元组的值 带子查询的删除语句 空值的处理 空值的产生 空值的判 ...

  3. 【数据库系统设计】关系数据库标准语言SQL(2)

    关系数据库标准语言SQL 数据查询(连接查询) 等值连接 `=` 自然连接 自身连接 外连接 `LEFR/RIGHT JOIN ... ON` 多表连接 数据查询(嵌套查询 ) 带有`IN`谓词的子查 ...

  4. 【数据库系统设计】关系数据库标准语言SQL(1)

    关系数据库标准语言SQL SQL介绍 SQL的特点 SQL中基本概念 示例:学生-课程 数据库 数据定义 SCHEMA定义 基本表定义(重点) 定义基本表(关系模式) 数据类型 定义基本表示例 修改基 ...

  5. 【数据库原理 • 三】关系数据库标准语言SQL

    前言 数据库技术是计算机科学技术中发展最快,应用最广的技术之一,它是专门研究如何科学的组织和存储数据,如何高效地获取和处理数据的技术.它已成为各行各业存储数据.管理信息.共享资源和决策支持的最先进,最 ...

  6. 【数据库系统】第一部分 数据库基础(3) 关系数据库标准语言SQL(3) 数据定义

    本文属于「数据库系统」系列文章之一,这一系列着重于「数据库系统知识的学习与实践」.由于文章内容随时可能发生更新变动,欢迎关注和收藏数据库系统系列文章汇总目录一文以作备忘.需要特别说明的是,为了透彻理解 ...

  7. 重温《数据库系统概论》【第一篇 基础篇】【第3章 关系数据库标准语言SQL】

    本篇内容为中国人民大学教授王珊.萨师煊的<数据库系统概论>自学课程的复习笔记,学习视频源于小破站(传送门),对应视频P16-P27,属教材"[第一篇 基础篇]"的&qu ...

  8. 实验一 关系数据库标准语言SQL

    实验一 关系数据库标准语言SQL [实验目的] 在给定的关系数据库管理系统MySQL环境下,通过实验学生能够: 1.MySQL环境的认识及熟悉,了解其对标准SQL的扩充. 2.掌握MySQL环境下数据 ...

  9. 【数据库】关系数据库标准sql语言

    3:关系数据库标准语言SQL DDL操作(3) 创建数据库 sqlserver CREATE DATABASE database_name [ON { [PRIMARY] (NAME=logical_ ...

  10. [XJTUSE DATABASE]——第三章 关系数据库标准语言SQL

    文章目录 [XJTUSE DATABASE]--第三章 关系数据库标准语言SQL 一.SQL概述 二.数据定义 模式的定义与删除 定义模式 删除模式 基本表的定义.删除与修改 数据类型 模式与表 修改 ...

最新文章

  1. Linux shell 学习笔记(4)— linux 环境变量(全局变量、局部变量及变量持久化)
  2. Linux下history命令详解---转载
  3. mariadb 没有mysql表_数据未插入mySQL表(MariaDB)
  4. 【转载】Callable、FutureTask中阻塞超时返回的坑点
  5. “开源、共享、创新”, 中国最具前景开发者峰会落幕魔都
  6. 嵌入式C语言入门操作
  7. 像素密度(衡量屏幕显示能力)
  8. excel中单元格的引用方法
  9. 产品 电信nb接口调用_【IoT】物联网NB-IoT之电信物联网开放平台对接流程浅析
  10. op 消除 消除自激振荡
  11. ExoPlayer网速估计方法
  12. 2023年上海理工大学材料与化工专业考研成功上岸前辈复习经验指导
  13. Python地学分析 — 地理空间参考系介绍
  14. 有关HP LaserJet M132 打印机 打印文本 整体偏右 解决办法
  15. APP自动化测试(2)-通过appium打开手机的应用
  16. 高并发编程之生产者—消费者设计模式
  17. 超级好用的php文件上传类(上传、缩略图、水印)
  18. 为什么深度学习中一般使用mean=[0.485, 0.456, 0.406]和std=[0.229, 0.224, 0.225]来归一化图像?
  19. 程序员找工作的注意事项
  20. 解决IE主页被www.13721.net劫持。

热门文章

  1. 添加IIS FTP站点密码
  2. google浏览器添加抓包插件
  3. WPS for Linux添加字体
  4. python 股票回测系统_Python爬虫回测股票的实例讲解
  5. 酷我音乐解析API,支持搜索、歌单、单曲、专辑、MV解析、多音质切换、图片大小切换
  6. 移动端背景图片自适应
  7. 人脸识别一体机解决方案
  8. c语言封皮,C语言程序设计封皮.doc
  9. 通过Jquery实现文本高亮及取消高亮
  10. 模拟卷Leetcode【普通】015. 三数之和