Skip to content

Commit cd4226b

Browse files
committed
SQL-294 - Insert into SQL table or update if exists
1 parent 7b43c4f commit cd4226b

File tree

4 files changed

+17
-43
lines changed

4 files changed

+17
-43
lines changed
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
CREATE TABLE employees (
2-
id INT PRIMARY KEY,
3-
name VARCHAR(50),
4-
department VARCHAR(50)
5-
);
1+
INSERT INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CS');
62

7-
8-
INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'Finance');
9-
10-
MERGE INTO employees AS target
11-
USING (SELECT 1 AS id, 'Alice' AS name, 'Accounting' AS department) AS source
3+
MERGE INTO Department AS target
4+
USING (SELECT 1 AS id, 'Computer Science' AS name, 'CSE' AS code) AS source
125
ON target.id = source.id
136
WHEN MATCHED THEN
14-
UPDATE SET name = source.name, department = source.department
7+
UPDATE SET name = source.name, code = source.code
158
WHEN NOT MATCHED THEN
16-
INSERT (id, name, department) VALUES (source.id, source.name, source.department);
17-
9+
INSERT (id, name, code) VALUES (source.id, source.name, source.code);
1810

19-
SELECT * from employees;
11+
SELECT * from Department;
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
CREATE TABLE employees (
2-
id INT PRIMARY KEY,
3-
name VARCHAR(50),
4-
department VARCHAR(50)
5-
);
1+
INSERT INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CS');
62

7-
INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'Finance');
3+
INSERT INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CSE')
4+
ON CONFLICT (id) DO UPDATE SET name = 'Computer Science', code = 'CSE';
85

9-
INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'Accounting')
10-
ON CONFLICT (id) DO UPDATE SET name = 'Alice', department = 'Accounting';
11-
12-
SELECT * from employees;
6+
SELECT * from Department;
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
CREATE TABLE employees (
2-
id INT PRIMARY KEY,
3-
name VARCHAR(50),
4-
department VARCHAR(50)
5-
);
1+
INSERT INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CS');
62

7-
INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'Finance');
3+
INSERT INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CSE')
4+
ON DUPLICATE KEY UPDATE name = 'Computer Science', code = 'CSE';
85

9-
INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'Accounting')
10-
ON DUPLICATE KEY UPDATE name = 'Alice', department = 'Accounting';
11-
12-
SELECT * from employees;
6+
SELECT * from Department;
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
CREATE TABLE employees (
2-
id INT PRIMARY KEY,
3-
name VARCHAR(50),
4-
department VARCHAR(50)
5-
);
1+
INSERT INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CS');
62

7-
INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'Finance');
3+
REPLACE INTO Department (id, name, code) VALUES (1, 'Computer Science', 'CSE');
84

9-
REPLACE INTO employees (id, name, department) VALUES (1, 'Alice', 'Accounting');
10-
11-
SELECT * from employees;
5+
SELECT * from Department;

0 commit comments

Comments
 (0)