Repository navigation
Expand file tree
/
Copy path03_Data_Manipulation_DML.sql
More file actions
115 lines (95 loc) · 3.46 KB
/
Copy path03_Data_Manipulation_DML.sql
File metadata and controls
115 lines (95 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ==============================================================================
SQL Data Manipulation Language (DML)
---------------------------------------------------------------------------------
This guide covers the essential DML commands used for inserting, updating,
and deleting data in database tables.
Table of Contents:
1. INSERT
2. UPDATE
3. DELETE
=================================================================================
*/
-- Run this line first: every query in this script uses MyDatabase
USE MyDatabase;
/* ==============================================================================
1. INSERT
============================================================================== */
/* #1 Method: Manual INSERT using VALUES */
-- Insert new records into the customers table
INSERT INTO customers (id, first_name, country, score)
VALUES
(6, 'Anna', 'USA', NULL),
(7, 'Sam', NULL, 100)
-- Incorrect column order
INSERT INTO customers (id, first_name, country, score)
VALUES
(8, 'USA', 'Max', NULL)
-- Incorrect data type in values
INSERT INTO customers (id, first_name, country, score)
VALUES
('Max', 9, 'Max', NULL)
-- Insert a new record with full column values
-- (if you ran the incorrect column order example above, delete that row first:
-- DELETE FROM customers WHERE id = 8)
INSERT INTO customers (id, first_name, country, score)
VALUES (8, 'Max', 'USA', 368)
-- Insert a new record without specifying column names (not recommended)
INSERT INTO customers
VALUES
(9, 'Andreas', 'Germany', NULL)
-- Insert a record with only id and first_name (other columns will be NULL or default values)
INSERT INTO customers (id, first_name)
VALUES
(10, 'Sahra')
/* #2 Method: INSERT DATA USING SELECT - Moving Data From One Table to Another */
-- The DDL lesson changed the table persons and dropped it: create it again as it was
DROP TABLE IF EXISTS persons;
CREATE TABLE persons (
id INT NOT NULL,
person_name VARCHAR(50) NOT NULL,
birth_date DATE,
phone VARCHAR(15) NOT NULL,
CONSTRAINT pk_persons PRIMARY KEY (id)
);
-- Copy data from the 'customers' table into 'persons'
INSERT INTO persons (id, person_name, birth_date, phone)
SELECT
id,
first_name,
NULL,
'Unknown'
FROM customers
/* ==============================================================================
2. UPDATE
============================================================================== */
-- Change the score of customer with ID 6 to 0
UPDATE customers
SET score = 0
WHERE id = 6
-- Change the score of customer with ID 10 to 0 and update the country to 'UK'
UPDATE customers
SET score = 0,
country = 'UK'
WHERE id = 10
-- Update all customers with a NULL score by setting their score to 0
UPDATE customers
SET score = 0
WHERE score IS NULL
-- Verify the update
SELECT *
FROM customers
WHERE score IS NULL
/* ==============================================================================
3. DELETE
============================================================================== */
-- Select customers with an ID greater than 5 before deleting
SELECT *
FROM customers
WHERE id > 5
-- Delete all customers with an ID greater than 5
DELETE FROM customers
WHERE id > 5
-- Delete all data from the persons table
DELETE FROM persons
-- Faster method to delete all rows, especially useful for large tables
TRUNCATE TABLE persons