zoukankan      html  css  js  c++  java
  • 《数据库系统概论》 -- 3.3 DML之增删改

    数据更新操作有三种:添加、修改、删除

    INSERT     格式         INSERT

                                         INTO <表名> [(<属性列1>[,<属性列2>...])]

                                         VALUES (<常量1>[,<常量2>...]);

                                         或

                                         INSERT

                                         INTO <表名> [(<属性列1>[,<属性列2>...])]

                                         <select子查询>;

                      注意         在INTO子句中没有出现的属性列,新元组取空值;

                                         如果某列不允许为空,则会报错

                                         如果INTO子句只有表名,则数据的顺序必须与创建语句的顺序一致

                      举例

                                将一个新学生元组(201215128,陈冬,男,IS,18)插入Student表

                                          INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215128','陈冬','男','IS',18);

                               将学生张成民的信息(201215126,张成民,男,CS,18)插入到Student表中

                                         INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215126','张成民','男','CS',18);

                               插入一条选课记录('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)

                                         SELECT Sdept,AVG(Sage) FROM Student GROUP BY Sdept;

    UPDATE   格式         UPDATE <表名>

                                         SET <列名>=<表达式>[, <列名>=<表达式>...]

                                         [WHERE <条件表达式>];

                      举例

                                将学生201215121的年龄改为22岁

                                         UPDATE Student SET Sage=22 WHERE Sno='201215122';

                               将所有学生年龄增加1岁

                                         UPDATE Student SET Sage=Sage+1;

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

                                         UPDATE SC SET Grade=0 WHERE Sno IN (

                                         SELECT Sno FROM Student WHERE Sdept='CS'

                                         );

    DELETE    格式         DELETE FROM <表名>

                                         [WHERE <条件表达式>];

                      说明         如果没有WHERE子句,则删除表中所有数据,该表还在

                       举例

                                删除学号为201215128的学生记录

                                         DELETE FROM Student WHERE Sno='201215128';

                               删除所有学生的选课记录

                                         DELETE FROM SC;

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

                                         DELETE FROM SC WHERE Sno IN (

                                                  SELECT Sno FROM Student WHERE Sdept='CS'

                                         );

  • 相关阅读:
    Java web ch02_5
    Java web ch02_4
    Java web ch02_3
    Java web ch02_9
    myeclipse和eclipse的区别以及优越性
    新的学年,迎来新的活力!
    Javaweb中的Tomcat服务器(简单了解)
    一切都是对象
    java中被隐藏的具体实现
    SSL与TLS的区别以及介绍
  • 原文地址:https://www.cnblogs.com/yc913344706/p/7482169.html
Copyright © 2011-2022 走看看