Data maintenance (addition, deletion and modification of database)

One, insert data

INSERT INTO table name [(column name 1 [, column name 2 ...])]

    VALUES (value 1 [, value 2 ...])

          [, (Value 1 [, value 2 ...]), ......, (value 1 [, value 2 ...])]

[Example 2-41] It is planned to create a new department with the number 80 and the address "SHANGHAI", but the name of the department is not determined, and the insertion of this record is completed.

SELECT * FROM dept_c;
INSERT INTO dept_c(deptno,loc)VALUES(80,'SHANGHAI');

[Example 2-42] It is planned to create two new departments, one department deptno is 60, dname is "SALES", loc is "BEIJING", the other department deptno is 70, dname is "RESEARCH", loc is "XIAN".

INSERT INTO dept_c
  VALUES(60,'SALES','销售','BEIJING'),
                (70,'RESEARCH','研究员','XIAN');


Second, update the data

UPDATE table name

  SET column name = value [, column name = value, ……]

  [WHERE <condition>]

[Example 2-43] Update the address of department 60 in the dept_c table to CHINA.

UPDATE dept_c SET loc='CHAIN' WHERE deptno=60;

[Example 2-44] Change the addresses of all departments in the dept_c table to CHICAGO.

UPDATE dept_c SET loc='CHICAGO';

Extension: UPDATE dept_c SET loc = 'CHICAGO', Job = 'R & D' WHERE dname LIKE 'R & D%';

Use subquery to modify records

[Example 2-45] Update the department name of department 3 in the dept_c table according to the dept table

UPDATE dept_c SET dname=(SELECT dname FROM dept WHERE deptno=3)WHERE deptno=3;

Third, delete the data

The basic syntax of the DELETE statement is as follows:

   DELETE  [ FROM ]  表名

     [WHERE <condition>]

First delete all the records in the dept_c table, and then use the INSERT command to insert the records in the dept_c table into the dept_c table.

TRUNCATE TABLE dept_c;
SELECT * FROM dept_c;
INSERT INTO dept_c(deptno,dname) SELECT deptno,dname FROM dept;
SELECT * FROM dept_c;
INSERT INTO dept_c SELECT * FROM dept;
SELECT * FROM dept_c;

[Example 2-46] Delete the record of department 2 in the dept_c table.

DELETE FROM dept_c WHERE deptno=2;

[Example 2-47] Delete all records in the dept_c table.

DELETE FROM dept_c;

 

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/105554521