本文用到3张表:
student(sno,sname,sage,ssex,sdept)、primary key(sno)
sc(sno,cno,score)、primary key(sno,cno)
course(cno,cname,cpno,ccredit)、primary key(cno)
insert、update、delete操作对象为:
1. 一个元组
2. 多个元组
3. 全表
insert into:插入一个元组
框架为:
insert into tableName(attribute1,attribute2......) values (attribute1's values,attribute2's values.....)
例子:
/两个括号里面的字段名位置一致
insert into student(sno,sname,sage) values (20200001,张三,20);
/当然,如果表结构中某一字段(列)可以为null,我们在插入时也可以为空
insert into student(sno,sname,sage) values (20200001,'张三',null);
/当表名后面没有括号,后面的values就只能按表的字段顺序给值
insert into student values (20200001,'张三',20,'男','计算机科学与技术');
insert into:插入多个元组
框架为:
insert into tableName(attribute1,attribute2......) (select attribute1,attribute2.....
from sc where .........
)
例子:
/详细DDL语言(数据库模式定义语言)在文末有链接()
creat table fail(
sno datetype primary key,
cno datetype,
cname datetype,
score datetype
)/datetype与原表中的一样;比如说我student中sno的数据类型为char(9),那么fail表中的sno的数据类型就是char(9)
/把选修了c1但是成绩不及格的学生添加到fail表中
insert into fail(sno,cno,cname,score) (
select sno,cno,cname,score from sc,course where sc.cno = course.cno and cno = 'c2'
)
insert into:在sc表中插入一个在student表中和course表中都没有的学号和课程号
步骤:
1、在student表中插入该生
2、在course表中插入改课程
3、在sc表中插入改条选课记录
很简单的,请读者自行实现了 ^_^
update:一个元组
框架为:
/属性为整数类型可以进行+-*/运算再赋值 或者 可以直接进行修改(修改前后的数据类型要一致)
update tableName set attribute = (attribute +-*/ constant) or constant where .....
例子:
update student set sage = sage * 2 where sno = 20200001;
update student set ssex = '男' where sno = 20200001;
update:多个元组
框架为:
update tableName set attribute = (attribute +-*/ constant) or constant where .....
例子:
/将选修c2这门课的的同学的分数都改成100分
update sc set score = 100 where cno = 'c2'
update:多个元组修改-进阶题
一、对于SC表,将课程编号为c2号的最低分改为在原分数*2;
逻辑:
- 1、得到’c2’这门课的最低分
- 2、用最低分和’c2’作条件从sc里面找出满足条件的元组
- 3、修改元组的分数
update sc set score=score * 2 where sno in (
select sno from sc where score = (
select min(score) from sc where cno='c2' )
and cno = 'c2'
)
细节:请读者思考最外层查询要用sno in?以及逻辑2中为什么得到了’c2’的最低分了还要加上’c2’这个条件?
二、对于SC表,将课程名为数据结构的最低分改为在原分数*2
逻辑:
- 1、得到’c2’这门课的最低分
- 2、用最低分和数据结构作条件从sc里面找出满足条件的元组
- 3、修改元组的分数
细节:请读者思考最外层查询要用sno in?以及逻辑2中为什么得到了数据结构的最低分了还要加上数据结构这个条件?
/多表连接,
update sc set score = score * 100 where sno in (
select sno from sc where score = (
select min(score) from sc,course where sc.cno = course.cno and cname = '数据结构'
)
and cno = (
select cno from course where cname = '数据结构'
)
)
/嵌套查询
update sc set score = score * 2 where sno in (
select sno from sc where score = (
select min(score) from sc where cno = (
select cno from course where cname = '数据结构'
)
)
and cno = (
select cno from course where cname = '数据结构'
)
)
/两个子查询做条件来得到数据结构这门课最低分
update sc set score = 3 where sno in (
select sno from sc where ( score = (
select min(score) from sc )
and
cno = (
select cno from course where cname = '数据结构'
)
)
and cno = (
select cno from course where cname = '数据结构'
)
)
对细节的回答:
-
如果你用sno = ,而不用sno in,那么满足’c2’最低分(数据结构最低分)的结果是多个元组怎么办,就是最低分有好几个学生。
-
当内层查询返回数据结构最低分时, 你不加’c2’(数据结构)这个条件直接:
select sno from sc where score = min(score)l;
你不也把其他科目和数据结构最低分相等的元组也加进去了。
delete:一个元组
框架为:
delete from tableName where (table's attribute) = ...;
例子:
/删除学号为20200001的学生记录
delect from student where sno = 20200001;
delete:多个元组
框架为:
delect from tableName where (table's attribute) in (select ......)
例子:
/删除计算机系的所以学生记录
delect from student where snoin (select sno from student where sdept = '计算机')
delete:全表记录
框架为:
delect from tableName;
例子:
/删除所有选课记录
delect from sc;