Data Addition & Deletion
8/1/25About 2 min
Data Addition & Deletion
1. Data Insertion
Syntax:
INSERT INTO <TABLE_NAME> [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*)Example 1: Specified Columns Insertion
INSERT INTO table1(region, plant_id, device_id, time, temperature, humidity) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:37:00', 90.0, 35.1)
INSERT INTO table1(region, plant_id, device_id, time, temperature) VALUES ('Hamburg', '1001', '100', '2025-11-26 13:38:00', 91.0)Example 2: NULL Value Insertion
# Equivalent to the example above
INSERT INTO table1(region, plant_id, device_id, model_id, maintenance, time, temperature, humidity) VALUES ('Hamburg', '1001', '100', null, null, '2025-11-26 13:37:00', 90.0, 35.1)
INSERT INTO table1(region, plant_id, device_id, model_id, maintenance, time, temperature, humidity) VALUES ('Hamburg', '1001', '100', null, null, '2025-11-26 13:38:00', 91.0, null)Example 3: Multi-row Insertion
INSERT INTO table1
VALUES
('2025-11-26 13:37:00', 'Frankfurt', '1001', '100', 'A', '180', 90.0, 35.1, true, '2025-11-26 13:37:34'),
('2025-11-26 13:38:00', 'Frankfurt', '1001', '100', 'A', '180', 90.0, 35.1, true, '2025-11-26 13:38:25')
INSERT INTO table1
(region, plant_id, device_id, model_id, maintenance, time, temperature, humidity, status, arrival_time)
VALUES
('Frankfurt', '1001', '100', 'A', '180', '2025-11-26 13:37:00', 90.0, 35.1, true, '2025-11-26 13:37:34'),
('Frankfurt', '1001', '100', 'A', '180', '2025-11-26 13:38:00', 90.0, 35.1, true, '2025-11-26 13:38:25')Example 4: Query Write-back
insert into target_table select time,region,device_id,temperature from table1 where region = 'bj';
insert into target_table(time,device_id,temperature) table table3;
insert into target_table (select t1.time, t1.region as region, t1.device_id as device_id, t1.temperature as temperature from table1 t1 where t1.time in (select t2.time from table2 t2 where t2.region = 'shh'));2. Data Update
Syntax:
UPDATE <TABLE_NAME> SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)?
updateAssignment
: identifier EQ expression
;Example:
update table1 set b = a where substring(a, 1, 1) like '%'3. Data Deletion
Syntax:
DELETE FROM <TABLE_NAME> [WHERE_CLAUSE]?
WHERE_CLAUSE:
WHERE DELETE_CONDITION
DELETE_CONDITION:
SINGLE_CONDITION
| DELETE_CONDITION AND DELETE_CONDITION
| DELETE_CONDITION OR DELETE_CONDITION
SINGLE_CODITION:
TIME_CONDITION | ID_CONDITION
TIME_CONDITION:
time TIME_OPERATOR LONG_LITERAL
TIME_OPERATOR:
< | > | <= | >= | =
ID_CONDITION:
identifier = STRING_LITERALExample 1: Full Table Deletion
DELETE FROM table1Example 2: Time-range Deletion
-- Single time range
DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00
-- Multiple time ranges
DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00Example 3: Device-Specific Deletion
-- Delete data for specific device
DELETE FROM table1
WHERE device_id='101' AND model_id = 'B';
-- Delete data for device within time range
DELETE FROM table1
WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00'
AND device_id='101' AND model_id = 'B';
-- Delete data for specific device model
DELETE FROM table1 WHERE model_id = 'B';4. Device Deletion
Syntax:
DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)?Example: Delete specified device and all associated data
DELETE DEVICES FROM table1 WHERE device_id = '101'