Database-Data Update (INSERT, modify, delete)

Source: Internet
Author: User

Inserting data

Two ways of inserting data
1. Inserting tuples
2. Inserting sub-query results
Multiple tuples can be inserted at one time
Statement format

    INSERT    <表名> [(<属性列1>[,<属性列2 >…)]    VALUES (<常量1> [,<常量2>]    …           )

Function

将新元组插入指定表中
[例1]  将一个新学生元组(学号:200215128;姓名:陈冬;性别:男;所在系:IS;年龄:18岁)插入到Student表中。    INSERT    INTO  Student (Sno,Sname,Ssex,Sdept,Sage)    VALUES (‘200215128‘,‘陈冬‘,‘男‘,‘IS‘,18);
[例2]  将学生张成民的信息插入到Student表中。        INSERT            INTO  Student            VALUES (‘200215126’, ‘张成民’, ‘男’,18,‘CS‘);
[例3]  插入一条选课记录( ‘200215128‘,‘1 ‘)。    INSERT    INTO SC(Sno,Cno)    VALUES (‘ 200215128 ’,‘ 1 ’);   RDBMS将在新插入记录的Grade列上自动地赋空值。   或者:    INSERT    INTO SC    VALUES (‘ 200215128 ‘,‘ 1 ‘,NULL);
Insert subquery Results
语句格式    INSERT     <表名>  [(<属性列1> [,<属性列2>…  )]    子查询;功能    将子查询结果插入指定表中
INTO子句(与插入元组类似) 子查询SELECT子句目标列必须与INTO子句匹配值的个数值的类型
[例4]  对每一个系,求学生的平均年龄,并把结果存入数据库。第一步:建表      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;
modifying data
语句格式

UPDATE < table name >
Set < column name >=< expression >[,< column name >=< expression;] ...
[WHERE < conditions >];

功能修改指定表中满足WHERE子句条件的元组
SET子句指定修改方式要修改的列修改后取值WHERE子句指定要修改的元组缺省表示要修改表中的所有元组

Three ways to modify
1. Modify the value of a tuple

[例5]  将学生200215121的年龄改为22岁         UPDATE  Student         SET Sage=22         WHERE  Sno=‘ 200215121 ‘; 
    1. To modify the values of multiple tuples
[例6]  将所有学生的年龄增加1岁         UPDATE Student         SET Sage= Sage+1;
    1. Modified statement for the tape query
[例7]  将计算机科学系全体学生的成绩置零。        UPDATE SC        SET  Grade=0        WHERE  ‘CS‘=                       (SELECT Sdept                        FROM  Student                        WHERE  Student.Sno = SC.Sno);
[例8] 将选修了课程名为DB的不及格的成绩全部改为60分。        UPDATE SC        SET  Grade=60        WHERE  Grade<60 AND Cno IN                       (SELECT Cno                        FROM  COURSE                        WHERE  Cname = ‘DB’);

The RDBMS checks for modifications as it executes the modification statement
Whether to break the defined integrity rules on the table
Entity integrity
Master code not allowed to modify
User-defined Integrity
NOT NULL constraint
Unique constraint
Domain constraints

Delete data

Statement format

       DELETE       FROM     <表名>       [WHERE <条件>];

Function
Delete a tuple in the specified table that satisfies the WHERE clause condition
WHERE clause
Specifies the tuple to delete
The default is to delete all tuples in the table, and the table definition is still in the dictionary
Three ways to delete
1. Delete a tuple's value

[例8]  删除学号为200215128的学生记录。        DELETE         FROM Student         WHERE Sno= 200215128 ‘;
    1. Delete values for multiple tuples
[例9]  删除所有的学生选课记录。        DELETE        FROM SC;
    1. Delete statement for a tape query
[例10]  删除计算机科学系所有学生的选课记录。        DELETE        FROM SC        WHERE  ‘CS‘=                           (SELETE Sdept                            FROM Student                            WHERE Student.Sno=SC.Sno);

Database-Data Update (INSERT, modify, delete)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.