一.建立基本表

例如:

1.
create table sc(sno varchar(20),cno varchar(20),grade int,//规范格式primary key(sno,cno),//主码foreign key (cno) references course(cno),//外码foreign key (sno) references student(sno));
1.1
create table avg_age(
sdept char(20),
age int);
//插入用select
insert into avg_age select sdept,avg(sage) from student group by sdept;//建立系平均年龄表
1.2删除上面表
drop table avg_age (cascade);
1.3 update student set sage=sage+1;
1.4update sc set grade =0 where sno in(select sno from student where sdept='测绘');//将测绘系成绩置为0
1.5delete from sc where sno=20171777;//删除元组值
1.6 delete from avg;//删除表中所有记录
1.7delete from sc where sno in(select sno from student where sdept='计测试');//删除计测试记录
1.8select *  from student left outer join sc on (student.sno=sc.sno);//左外连接,显示出左边全部
1.9 select *  from student right outer join sc on (student.sno=sc.sno);//右外连接,显示出右边全部
2.插入值
1.insert into course(cno,cname) values(7,'java');//插入一条
2.insert into course(cno,cname) values(8,'c'),(9,'离散');//插入两条及多条记录
3.delete from course where cno=8;//删除某一元组
4.insert into course values(8,'c');//错误!原来有n=4列,未指明插入哪一列。
5.insert into course values(8,'c',1,4);//这样可以
6. update course set ccredit=6 where cno=9;//更改某一元组值
7. update course set ccredit=ccredit+1;//更改全部元组ccredit值
8.select * from student where sdept='计算机' intersect select* from student where sage<20;//计算机系与年龄不超20交集
9.select  sno from sc where cno=1 intersect select  sno from sc where cno=2;//即选修1又选修2的人//条件不能是cno=1 and cno=2,注意!
10.select  a.sno from sc a,sc b where a.sno=b.sno and a.cno=1 and b.cno=2;//上题也可以利用自身连接做
11.select * from sc where sno in(select sno from sc where cno=1) and cno=2;//也可以这样

二.修改基本表

1.增加一新列:alter table  sc  add entryTime DATE;

2.修改某一列数据类型:alter table sc alter column grade int;

3.增加完整性约束:alter table sc add unique(sno);

4.删除某一列:alter table sc drop column grade;

5.删除基本表:drop table sc cascade;

三.索引操作

1.CREATE UNIQUE INDEX  Stusno ON Student(Sno)

2.CREATE UNIQUE INDEX  Coucno ON Course(Cno)

3.CREATE UNIQUE INDEX  SCno ON SC(Sno ASC,Cno DESC);//在sc上建立索引,按学号升序,课程号降序;

4.修改索引名:alter index SCno rename to fun;//将索引名SCno更改;

5.删除索引:drop index fun;

四.数据字典

包含了:

1.关系模式定义

2.视图定义

3.索引定义

4.完整性约束定义

5.各类用户对数据库的操作权限

6.统计信息;

关系规范化中的插入异常是指______。

A) 应该删除的数据未被删除
B) 应该插入的数据未被插入
C) 不该删除的数据被删除
D) 不该插入的数据被插入

在关系数据库设计中,设计关系模式是数据库设计中的______阶段的任务。

A.需求分析阶段
B.概念设计阶段
C.逻辑设计阶段
D.物理设计阶段

事务的隔离性是指()。

A . A.一个事务内部的操作及使用的数据对并发的其他事务是隔离的
B . B.事务一旦提交,对数据库的改变是永久的
C . C.事务中包括的所有操作要么都做,要么都不做
D . D.事务必须是使数据库从一个一致性状态变到另一个一致性状态

数据库恢复的基础是利用转储的冗余数据。这些转储的冗余数据包括()

日志文件、数据库后备副本

在数据库设计中,将E-R图转换成关系数据模型的过程属于()。

A.逻辑设计阶段
B.概念设计阶段
C.需求分析阶段
D.物理设计阶段

在关系数据库设计中,使每个关系达到3NF。这是( )设计阶段的任务。

A.需求分析
B.概念设计
C.逻辑设
D.物理设计

五.单表查询

一.查询列
1.select sno,sname from student;
2.select * from student;//查询所有学生全部信息
二.查询经过计算值
1.select sname,2018-sage from student;
2.select sname,2018-sage date from student;//将查询到的年龄值所在列给予特定名字
3.select sname,'date',2018-sage,upper(sdept) from student;//将系大写,多显示一列'date';
三.选择若干元组
1.select sno from sc;
2.select distinct sno from sc;//去重
3.select sno from student where sdept='计算机';//计算机系
4.select sname from student where sage<20;
5.select distinct sno,cno from sc where grade<60;//distinct 有时候一定要注意!
6.select sname,sage from student where sage between 18 and 20;
7.select sname,sage from student where sage not between 18 and 20;
8.select sname,sage from student where sdept in ('计算机','通信');//in!,特别注意一下
9.select sname,sage from student where sdept not in ('计算机','通信');//in!
10.select sname from student where sname like'王%';//查询王姓同学,不限制长度
11.select sname from student where sname like'王_';//查询姓王且只有两个字的同学
12.select sname from student where sname like'_阳';//查询第二个字为阳的同学
13.SELECT Cno,Ccredit FROM  Course WHERE  Cname LIKE 'DB\_Design' ESCAPE '\ ';
查询DB_Design课程的课程号和学分。
14.SELECT  * FROM    Course WHERE  Cname LIKE  'DB\_%i_ _' ESCAPE '\ ' ;
查询以"DB_"开头,且倒数第3个字符为 i的课程的详细情况。
15.select sno,cno from sc where grade is null;//查询grade为空的学号和课程号;
16.select sno,cno from sc where grade is not null;//查询所有有成绩的...
四.order by字句
1.select sno,grade from sc where cno=3 order by grade desc;//选修了3号课程且成绩降级排列
2.select * from student order by sdept,sage desc;//系号升序,同系按年龄降序排列
五.聚集函数
1.select count(*) from student;//查询学生总人数
2. select count(distinct sno ) from sc;//查询选修课程总人数,注意distinct;
3.select avg(grade) from sc where cno=1;//选修1号课程平均分
4.select max(grade) from sc where cno=3;//max函数作用于几个特定元组
5.select sum(credit) from sc,course where sno=20171789 and sc.cno=course.cno;//总学分
6.select sno,avg(grade) from sc group by sno having avg(grade)>89;//查询平均成绩大于89的
7.

六.连接查询

一.等值非等值连接查询
1.select student.*,sc.* from student,sc where sc.sno=student.sno;//选修情况
2.select sname,sc.sno from student,sc where sc.sno=student.sno and cno=3 and grade>90;
二.自身连接
1.select fi.cno,se.cpno from course fi,course se where fi.cpno=se.cno;//选修课的选修课
2.SELECT Student.Sno,Sname,Ssex,Sage,Sdept,Cno,GradeFROM  Student  LEFT OUT JOIN SC ON    (Student.Sno=SC.Sno); //左外连接
3.select student.sno,sname,course.cname,grade from student,course,sc where student.sno=sc.sno and sc.cno=course.cno//多表连接;
三.嵌套查询
1.SELECT Sno, Sname, SdeptFROM StudentWHERE Sdept  IN(SELECT SdeptFROM StudentWHERE Sname= ' 刘晨 ');//与刘晨相同系学生
2.select s1.sno,sname,sdept from student s1,student s2 where s1.sdept=s2.sdept and s2.sname='刘晨';
3.select sname,sno from student where sno in(select sno from sc where cno in(select cno from course where sname='数学'));
4. select sname,sno from student where sno in(select sno from sc where cno in(select cno from course where cname='数学'));//查询选修了课程名为数学信息
5.select sname,student.sno from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and cname='数学';//同上,利用连接查询;
6.select sno,cno from sc x  where grade >=(select avg(grade) from sc y  where x.sno=y.sno);//相关子查询;
7.create view iiii (sno,avg) as select sno,avg(grade) from sc group by sno;//先建立每个人及其对应选修课平均成绩表
select sc.sno ,cno from sc,iii where sc.sno=iii.sno and sc.grade>=avg;
再连接选择
四.ANY、ALL查询
//ANY任一、ALL全部
1.select sname,sage from student where sage<any(select sage from student where sdept='计算机')and sdept<>'计算机';//查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄
2.select sname,sage from student where sage<(select max(sage) from student where sdept='计算机') and sdept<>'计算机';//利用聚集函数实现;
3.select sname,sage from student where sage<all(select sage from student where sdept='计算机') and sdept<>'计算机';//小于最小值
4.select sname,sage from student where sage<(select min(sage) from student where sdept='计算机') and sdept<>'计算机';//利用聚集函数实现;
五.EXISTS、NOT EXISTS查询
1.select sname from student,sc  where student.sno=sc.sno and cno=1;
转化成select sname from student where exists(select * from sc where sno =student.sno and cno=1);
2.select sname from student where not exists (select * from sc where sno =student.sno and cno=1);//未选修1号课程
333.select sno,sname,sdept from student s1 where exists(select * from student s2 where s1.sdept=s2.sdept and s2.sname='冯硕');//查询与我同系学生信息
444.select sname from student where not exists(select * from course where not exists(select * from sc where sno=student.sno and cno=course.cno));//查询选修了全部课程人姓名//!!!!重点
555.select distinct sno from sc x where not exists(select * from sc y where y.sno=20171789 and not exists(select * from sc z where z.sno=x.sno and z.cno=y.cno));//至少选修了学号为..选修全部课程人信息
六.集合查询
1. select * from student where sdept='计算机'union select * from student where sage<20;//计算机系及年龄小于20人信息
2.select sno from sc where cno=1 union select sno from sc where cno=2;//不能用and选修了课程1和课程2
UNION:将多个查询结果合并起来时,系统自动去掉重复元组
UNION ALL:将多个查询结果合并起来时,保留重复元组
七.派生表查询
1. select sno,cno from sc,(select sno,avg(grade) from sc group by sno)as avg_sc(avg_sno,avg_grade)where sc.sno=avg_sc.sno and grade>=avg_sc.avg_gade;//查询成绩超过自己平均成绩
2. select sname from student ,(select sno from sc where cno=1 )ac sc6 where student.sno=sc6.sno;//选修1号课程
八.建立视图
1.//查询只选修了2,1号课程的人
create view  ooop(snoo,num) as(select sno,count(*) from sc group by sno);
select ooop.snoo from ooop,sc where ooop.snoo=sc.sno and  ooop.num=2 and cno=1 and sc.sno in(select sno from sc where cno=2);

七.修改操作

一.
1.update student set sdept=null where sno=20171766;//将某人系置为空值
2.select * from student where sdept is null;//查询系暂时为空人,不能用=null;
3.select * from sc where (grade<60 or grade is null) and cno=2;//2号课程缺考或不及格人
4.create view ok(sdept,avg) as select sdept,avg(sage) from student group by sdept;//建立系平均年龄视图
5.create view op as select * from student where sdept='计算机';//建立计算机系信息视图
6.create view op as select * from student where sdept='计算机' with check option;//该视图进行插入、修改和删除操作时RDBMS会自动加上Sdept='IS'的条件
7.create view pp(sno,sname,grade)as select student.sno,sname,grade from sc,student where student.sno=sc.sno and sdept='计算机' and cno=6;//计算机选修6号课程,注意select后的某些值要加上表名,student.sno,因为sno在两个表中都有
8.create view ppp as select  student.sno,sname,grade from student,sc where student.sno=sc.sno and cno=6 and grade>90;//建立计算机系选修了1号课程且成绩在90分以上的学生的视图
9.也可以利用7的视图,在视图上建立视图
create view ppp as select * from pp where grade>90;//一步到位
10.create view bb(sno,sname,birth) as select sno,sname,2018-sage from student;//出生年份
11.create view oo as select sno,avg(grade) from sc group by sno;//平均成绩
12.drop view pp;//上面建的图ppp用到了pp,则利用级联删除:
drop view pp cascade;
该语句从数据字典中删除指定的视图定义
如果该视图上还导出了其他视图,使用CASCADE级联删除语句,把该视图和由它导出的所有视图一起删除
删除基表时,由该基表导出的所有视图定义都必须显式地使用DROP VIEW语句删除
二.在视图上进行查询
1. select * from op where sno in (select sno from sc where cno=6);
2. select op.sname from op,sc  where op.sno=sc.sno and cno=6;同上也行
查询选修了6号课程的计算机系学生
3.create view avg(sno,avg) as select sno,avg(grade)  from sc  group by sno;select sno , avg from avg where avg>=90;
在avg视图中查询平均成绩在90分以上的学生学号和平均成绩
4.或者
select sno,avg(grade) from sc group by sno having avg(grade)>=90;
但是不能:SELECT Sno,AVG(Grade)
FROM     SC
WHERE  AVG(Grade)>=90
GROUP BY Sno;
这样是不对的
三.更新视图
1. update op set sage=24 where sno=20171789;//和表一样其实
2.insert into op values(20171789,'哈哈','计算机',23,'女');//必须是计算机,因为加了with check option
3.delete from op where sname='哈哈';//把它删除吧,插入和删除其实本质上都转化为对基本表的更改
4.

八.简答题

简述视图作用
1.简化用户操作
2.使用户以多角度看问题
3.对重构数据库提供了一定程度逻辑独立性
4.对机密数据提供安全保护
5.适当利用视图可以清晰表达查询

数据库总结作业SQL操作语句三相关推荐

  1. jsch连接mysql_求用jsch网络工具包通过ssh连接远程oracle数据库并发送sql操作语句(数据库在unix上)java代码例子...

    求用jsch网络工具包通过ssh连接远程oracle数据库(数据库在unix上)java代码例子:为何jsch发送:sqlplususer/pwd@service此命令,却没有结果返回啊.下面是代码: ...

  2. 数据库操作 linq php,.NET_asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析,本文实例讲述了asp.net使用LINQ t - phpStudy...

    asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析 本文实例讲述了asp.net使用LINQ to SQL连接数据库及SQL操作语句用法.分享给大家供大家参考,具体如下: LI ...

  3. ubuntu下mysql语句_Ubuntu安装mysql及常用SQL操作语句

    Ubuntu安装mysql及常用SQL操作语句:安装mysql,在终端运行如下三条命令. sudo apt-get install mysql-server sudo apt-get install ...

  4. MySQL数据库基础:安装+登入+SQL操作语句+数据库授权、备份、恢复+其他操作

    MySQL简介 MySQL最流行的RDBMS(关系型数据库系统),特别是在WEB应用方面,表现特点 数据以表格的形式出现 每行为各种记录名称 每列为记录名称所对应的数据域 许多的行和列组成一张表单 若 ...

  5. CDA数据分析师 - SQL数据库基础 数据类型表操作语句

    SQL 基础概念 [领会] 关系型数据库基本概念 表的基本概念(字段.记录) 表的约束条件(主键.外键.非空.唯一.自增.默认值) 实体关系图(E-R 图) ANSI-SQL 以及不同的数据库实现的关 ...

  6. 经典SQL操作语句【转载】

    1.经典SQL语句大全(绝对的经典) 2. 3. 4.一.基础 1.1.说明:创建数据库 2.CREATE DATABASE database-name 3.2.说明:删除数据库 4.drop dat ...

  7. 测试基础——数据库及数据库表的SQL操作(了解即可)

    目录 1.数据库基础概念 2.SQL介绍 3.MySQL介绍 4.数据库连接工具Navicat 5.数据类型 6.约束 7.对数据库操作的SQL语句 7.1创建数据库 7.2使用/打开/切换数据库 7 ...

  8. Sqlite 移动嵌入式数据库Sqlite的日常SQL操作语句汇总

    序言:     嵌入式数据库Sqlite的基本sql使用汇总,使用测试起来,与关系型数据库mysql在语法上有很多的相似之处,先准备测试数据: CREATE TABLE COMPANY(ID INT ...

  9. SQL操作语句中的注意点

    一 查询语句 1 distinctkeyword消除反复行 当查询的结果数据中出现反复数据时.在查询条件中加上distinctkeyword消除反复行: 如:select distinct Sno f ...

  10. 常用sql操作语句实战演示

    本文为原创博客,未经本人允许,禁止将本人的博客复制下来上传到百度文库等平台. 作者:合肥工业大学 管理学院 钱洋 1563178220@qq.com 目录 linux中或cmd中连接数据库 查看库名 ...

最新文章

  1. java交通工具的类继承代码_Java作业-交通工具继承
  2. 用户信息填写web代码_基于web的自定义表单引擎
  3. 亿级商品详情页架构演进技术解密 | 高可用架构系列
  4. python脚本画pie饼图_python 使用matplotlib.pyplot.pie绘制饼图
  5. sublime text3 添加到右键菜单
  6. CMOS图像传感器——相位对焦
  7. 英特尔为 Kubernetes 推出分布式深度学习平台:Nauta
  8. 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。...
  9. 荣耀手机动态修改imei信息
  10. u盘带走的绿化wamp配置方式
  11. Microsoft Lync2013客户端下载
  12. 目前大多数计算机工作原理采用的是,计算机应用基础复习题2016有答案
  13. NutUI Bingo - 基于 Vue 3.0 的移动端抽奖组件,由京东前端团队打造
  14. system(“mode con cols=40 lines=15“)参数活起来
  15. 挪威科技大学计算机硕士,挪威科技大学硕士留学申请条件
  16. html转化成PDF
  17. 在cmd中进入mysql的步骤
  18. VIM for windows
  19. nmn修复脑神经是真的吗,nmn到底有没有效,看这一篇就够了
  20. 2022年3月青少年机器人技术等级考试理论综合试卷(一级)

热门文章

  1. 蓝宝石英语怎么读,sapphire是什么意思_sapphire的翻译_音标_读音_用法_例句_爱词霸在线词典...
  2. 【从蛋壳到满天飞】JS 数据结构解析和算法实现-栈和队列
  3. 建筑有言丨如果大学有一个最好的专业,那就是建筑学
  4. 谷歌统计插件ga的使用
  5. 中职计算机教学工作随笔,教学随笔(精选15篇)
  6. L2行情接口怎么用最高效?
  7. Python3的sys模块
  8. onenote同步问题
  9. 公告栏文本横向循环滚动
  10. MSL、TTL、RTT