title: 06-Data-Modification
date: 2023-11-17
status: DONE
tags:
- MySQL
- NOTE
- Lec6
author:
- AllenYGY
created: 2023-11-18T13:46
updated: 2024-03-21T21:48
publish: True
Data-Modification
INSERT INTO table_name (column1, column2, column3, ...)
VALUES
(value1_1, value1_2, value1_3, ...),
(value2_1, value2_2, value2_3, ...),
...
INSERT INTO destination_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE conditions;
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE FROM table_name
WHERE condition;
Usually, database users are only allowed to change data (table rows), which is covered by this lab.
Table columns are related to the logical design and are managed by database managers.
To change the logical design, you need ALTER TABLE (last lab).
Program code: 1001
Program name: Computer Science
Division: Science and Technology
Director: NULL (unknown)
INSERT INTO program (p_code, p_name, division, director_id) VALUES (1001, 'Computer Science', 'Science and Technology', NULL);
The schema of the table is to remind users the data types, which can be omitted.
Currently, the instructor table is empty. So, the program director is unknown.
Course:
Name: Introduction to Biology, Credits: 3,
Domain: ENVS, Course Number: 2001
Instructor:
ID: 20002, Name: S.H. Zee, Title: Professor,
Salary: 100000, Program: Environmental Science
Program:
ID: 1002, Name: Environmental Science,
Division: Science and Technology, Program director: Unknown
the instructor cannot be inserted before the program
INSERT INTO course VALUES
('Introduction to Biology', 3, 'ENVS', 2001);
INSERT INTO program VALUES
(1002, 'Environmental Science', 'Science and Technology', NULL);
INSERT INTO program VALUES
(20002, 'S.H. Zee', 'Professor', 100000, 1002);
INSERT INTO offer
SELECT p_code,c_name
FROM program,course
WHERE p_name='Environmental Science'
AND c_name = 'Introduction to Biology '
UPDATE program
SET director_id = (SELECT id FROM instructor WHERE i_name='S.T. Kwok')
WHERE p_name = 'Computer Science'
UPDATE: the keyword to update a table, followed by the table name.
SET: the keyword to update a specific column, followed by an assignment operation.
(…): a subquery to find the person id of S.T. Kwok.
WHERE: only update the rows which satisfy the condition in the WHERE clause.
DELETE FROM instructor WHERE i_name='S.T. Kwok'