|
| 1 | +<!-- |
| 2 | +
|
| 3 | + Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + or more contributor license agreements. See the NOTICE file |
| 5 | + distributed with this work for additional information |
| 6 | + regarding copyright ownership. The ASF licenses this file |
| 7 | + to you under the Apache License, Version 2.0 (the |
| 8 | + "License"); you may not use this file except in compliance |
| 9 | + with the License. You may obtain a copy of the License at |
| 10 | + |
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + |
| 13 | + Unless required by applicable law or agreed to in writing, |
| 14 | + software distributed under the License is distributed on an |
| 15 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + KIND, either express or implied. See the License for the |
| 17 | + specific language governing permissions and limitations |
| 18 | + under the License. |
| 19 | +
|
| 20 | +--> |
| 21 | + |
| 22 | +# Data Addition & Deletion |
| 23 | + |
| 24 | +## 1. Data Insertion |
| 25 | + |
| 26 | +**Syntax:** |
| 27 | + |
| 28 | +```SQL |
| 29 | +INSERT INTO <TABLE_NAME> [(COLUMN_NAME[, COLUMN_NAME]*)]? VALUES (COLUMN_VALUE[, COLUMN_VALUE]*) |
| 30 | +``` |
| 31 | + |
| 32 | +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data.md#_1-1-syntax) |
| 33 | + |
| 34 | +**Example 1: Specified Columns Insertion** |
| 35 | + |
| 36 | +```SQL |
| 37 | +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") |
| 38 | +VALUES ('Hunan', '3001', '3', 4, 90.0, 1200.0); |
| 39 | + |
| 40 | +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature") |
| 41 | +VALUES ('Hunan', '3001', '3', 5, 90.0); |
| 42 | +``` |
| 43 | + |
| 44 | +**Example 2: NULL Value Insertion** |
| 45 | + |
| 46 | +```SQL |
| 47 | +-- Equivalent to partial insertion with NULL values |
| 48 | +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") |
| 49 | +VALUES ('Hunan', '3001', '3', NULL, NULL, 4, 90.0, 1200.0); |
| 50 | + |
| 51 | +INSERT INTO table1("Region", "PlantID", "DeviceID", "Model", "MaintenanceCycle", Time, "Temperature", "Displacement") |
| 52 | +VALUES ('Hunan', '3001', '3', NULL, NULL, 5, 90.0, NULL); |
| 53 | +``` |
| 54 | + |
| 55 | +**Example 3: Multi-row Insertion** |
| 56 | + |
| 57 | +```SQL |
| 58 | +INSERT INTO table1 VALUES |
| 59 | +('Beijing', '3001', '3', '1', '10', 4, 90.0, 1200.0), |
| 60 | +('Beijing', '3001', '3', '1', '10', 5, 90.0, 1200.0); |
| 61 | + |
| 62 | +INSERT INTO table1("Region", "PlantID", "DeviceID", Time, "Temperature", "Displacement") |
| 63 | +VALUES |
| 64 | +('Beijing', '3001', '3', 4, 90.0, 1200.0), |
| 65 | +('Beijing', '3001', '3', 5, 90.0, 1200.0); |
| 66 | +``` |
| 67 | + |
| 68 | +## 2. Data Update |
| 69 | + |
| 70 | +**Syntax:** |
| 71 | + |
| 72 | +```SQL |
| 73 | +UPDATE <TABLE_NAME> SET updateAssignment (',' updateAssignment)* (WHERE where=booleanExpression)? |
| 74 | + |
| 75 | +updateAssignment |
| 76 | + : identifier EQ expression |
| 77 | + ; |
| 78 | +``` |
| 79 | + |
| 80 | +[Detailed syntax reference](../Basic-Concept/Write-Updata-Data.md#_2-1-syntax) |
| 81 | + |
| 82 | +**Example:** |
| 83 | + |
| 84 | +```SQL |
| 85 | +update table1 set b = a where substring(a, 1, 1) like '%' |
| 86 | +``` |
| 87 | + |
| 88 | +## 3. Data Deletion |
| 89 | + |
| 90 | +**Syntax:** |
| 91 | + |
| 92 | +```SQL |
| 93 | +DELETE FROM <TABLE_NAME> [WHERE_CLAUSE]? |
| 94 | + |
| 95 | +WHERE_CLAUSE: |
| 96 | + WHERE DELETE_CONDITION |
| 97 | + |
| 98 | +DELETE_CONDITION: |
| 99 | + SINGLE_CONDITION |
| 100 | + | DELETE_CONDITION AND DELETE_CONDITION |
| 101 | + | DELETE_CONDITION OR DELETE_CONDITION |
| 102 | + |
| 103 | +SINGLE_CODITION: |
| 104 | + TIME_CONDITION | ID_CONDITION |
| 105 | + |
| 106 | +TIME_CONDITION: |
| 107 | + time TIME_OPERATOR LONG_LITERAL |
| 108 | + |
| 109 | +TIME_OPERATOR: |
| 110 | + < | > | <= | >= | = |
| 111 | + |
| 112 | +ID_CONDITION: |
| 113 | + identifier = STRING_LITERAL |
| 114 | +``` |
| 115 | + |
| 116 | +**Example 1: Full Table Deletion** |
| 117 | + |
| 118 | +```SQL |
| 119 | +DELETE FROM table1 |
| 120 | +``` |
| 121 | + |
| 122 | +**Example 2: Time-range Deletion** |
| 123 | + |
| 124 | +```SQL |
| 125 | +-- Single time range |
| 126 | +DELETE FROM table1 WHERE time <= 2024-11-29 00:00:00 |
| 127 | + |
| 128 | +-- Multiple time ranges |
| 129 | +DELETE FROM table1 WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00 |
| 130 | +``` |
| 131 | + |
| 132 | +**Example 3: Device-Specific Deletion** |
| 133 | + |
| 134 | +```SQL |
| 135 | +-- Delete data for specific device |
| 136 | +DELETE FROM table1 |
| 137 | +WHERE device_id='101' AND model_id = 'B'; |
| 138 | + |
| 139 | +-- Delete data for device within time range |
| 140 | +DELETE FROM table1 |
| 141 | +WHERE time >= '2024-11-27 16:39:00' AND time <= '2024-11-29 16:42:00' |
| 142 | + AND device_id='101' AND model_id = 'B'; |
| 143 | + |
| 144 | +-- Delete data for specific device model |
| 145 | +DELETE FROM table1 WHERE model_id = 'B'; |
| 146 | +``` |
| 147 | + |
| 148 | +## 4. Device Deletion |
| 149 | + |
| 150 | +**Syntax:** |
| 151 | + |
| 152 | +```SQL |
| 153 | +DELETE DEVICES FROM tableName=qualifiedName (WHERE booleanExpression)? |
| 154 | +``` |
| 155 | + |
| 156 | +**Example: Delete specified device and all associated data** |
| 157 | + |
| 158 | +```SQL |
| 159 | +DELETE DEVICES FROM table1 WHERE device_id = '101' |
| 160 | +``` |
0 commit comments