diff --git a/mysql-queries/mysql-store-json/creating-table-example.sql b/mysql-queries/mysql-store-json/creating-table-example.sql new file mode 100644 index 00000000..b15fee24 --- /dev/null +++ b/mysql-queries/mysql-store-json/creating-table-example.sql @@ -0,0 +1,5 @@ +CREATE TABLE Departments ( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(50) NOT NULL, + info JSON +); \ No newline at end of file diff --git a/mysql-queries/mysql-store-json/inserting-json-data.sql b/mysql-queries/mysql-store-json/inserting-json-data.sql new file mode 100644 index 00000000..d4dac5b7 --- /dev/null +++ b/mysql-queries/mysql-store-json/inserting-json-data.sql @@ -0,0 +1,14 @@ +INSERT INTO Departments (id, name, info) VALUES +(1, 'Computer Science', '{"head": "Dr. Smith", "location": "Building 1", "courses": ["Algorithms", "Data Structures"]}'); + +INSERT INTO Departments (id, name, info) VALUES +(2, 'Mathematics', JSON_OBJECT('head', 'Dr. Johnson', 'location', 'Building 2', 'courses', JSON_ARRAY('Calculus', 'Linear Algebra'))); + +INSERT INTO Departments (id, name, info) VALUES +(3, 'Physics', JSON_OBJECT('head', 'Dr. Clark', 'location', 'Building 3', 'courses', JSON_ARRAY('Quantum Mechanics', 'Thermodynamics'))); + +SET @head = 'Dr. Miller'; +SET @location = 'Building 4'; +SET @courses = JSON_ARRAY('Classical Mechanics', 'Electromagnetism'); +INSERT INTO Departments (id, name, info) VALUES +(4, 'Engineering', JSON_OBJECT('head', @head, 'location', @location, 'courses', @courses)); \ No newline at end of file diff --git a/mysql-queries/mysql-store-json/retrieving-json-data.sql b/mysql-queries/mysql-store-json/retrieving-json-data.sql new file mode 100644 index 00000000..58c10ffe --- /dev/null +++ b/mysql-queries/mysql-store-json/retrieving-json-data.sql @@ -0,0 +1,5 @@ +SELECT id, name, info FROM Departments; + +SELECT id, name, JSON_EXTRACT(info, '$.head') AS head FROM Departments; + +SELECT id, name, info->'$.head' AS head FROM Departments; \ No newline at end of file diff --git a/mysql-queries/mysql-store-json/updating-json-data.sql b/mysql-queries/mysql-store-json/updating-json-data.sql new file mode 100644 index 00000000..20aa4f54 --- /dev/null +++ b/mysql-queries/mysql-store-json/updating-json-data.sql @@ -0,0 +1,19 @@ +UPDATE Departments +SET info = JSON_SET(info, '$.head', 'Dr. Parker') +WHERE id = 1; +SELECT id, name, info FROM Departments WHERE id = 1; + +UPDATE Departments +SET info = JSON_REPLACE(info, '$.location', 'Building 2A') +WHERE id = 2; +SELECT id, name, info FROM Departments WHERE id = 2; + +UPDATE Departments +SET info = JSON_REMOVE(info, '$.courses') +WHERE id = 3; +SELECT id, name, info FROM Departments WHERE id = 3; + +UPDATE Departments +SET info = JSON_ARRAY_APPEND(info, '$.courses', 'Artificial Intelligence') +WHERE id = 4; +SELECT id, name, info FROM Departments WHERE id = 4; \ No newline at end of file diff --git a/sql-queries-3/merging-two-rows/common-table-expression.sql b/sql-queries-3/merging-two-rows/common-table-expression.sql new file mode 100644 index 00000000..6cce7218 --- /dev/null +++ b/sql-queries-3/merging-two-rows/common-table-expression.sql @@ -0,0 +1,15 @@ +WITH MergedStudent AS ( + SELECT + 2017 AS id, + COALESCE(MAX(name), MIN(name)) AS name, + COALESCE(MAX(national_id), MIN(national_id)) AS national_id, + COALESCE(MAX(birth_date), MIN(birth_date)) AS birth_date, + COALESCE(MAX(enrollment_date), MIN(enrollment_date)) AS enrollment_date, + COALESCE(MAX(graduation_date), MIN(graduation_date)) AS graduation_date, + COALESCE(MAX(gpa), MIN(gpa)) AS gpa + FROM + (SELECT * FROM Student WHERE id = 2017 + UNION ALL + SELECT * FROM Student WHERE id = 2008) s + ) + SELECT * FROM MergedStudent; diff --git a/sql-queries-3/merging-two-rows/updating-with-coalesce.sql b/sql-queries-3/merging-two-rows/updating-with-coalesce.sql new file mode 100644 index 00000000..735e08bc --- /dev/null +++ b/sql-queries-3/merging-two-rows/updating-with-coalesce.sql @@ -0,0 +1,15 @@ +SELECT * FROM Student WHERE id IN (1011, 1610); + +UPDATE Student AS target + SET name = COALESCE(target.name, source.name), + national_id = COALESCE(target.national_id, source.national_id), + birth_date = COALESCE(target.birth_date, source.birth_date), + enrollment_date = COALESCE(target.enrollment_date, source.enrollment_date), + graduation_date = COALESCE(target.graduation_date, source.graduation_date), + gpa = COALESCE(target.gpa, source.gpa) + FROM Student AS source + WHERE target.id = 1011 AND source.id = 1610; + +DELETE FROM Student WHERE id = 1610; + +SELECT * FROM Student WHERE id IN (1011); \ No newline at end of file diff --git a/sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql b/sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql new file mode 100644 index 00000000..8208f262 --- /dev/null +++ b/sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql @@ -0,0 +1,16 @@ +INSERT INTO Student (id, name, national_id, birth_date, enrollment_date, graduation_date, gpa) + SELECT + 1000, + COALESCE(s1.name, s2.name), + COALESCE(s1.national_id, s2.national_id), + COALESCE(s1.birth_date, s2.birth_date), + COALESCE(s1.enrollment_date, s2.enrollment_date), + COALESCE(s1.graduation_date, s2.graduation_date), + COALESCE(s1.gpa, s2.gpa) + FROM + (SELECT * FROM Student WHERE id = 1717) s1, + (SELECT * FROM Student WHERE id = 1719) s2; + +DELETE FROM Student WHERE id IN (1717, 1719); + +SELECT * FROM Student WHERE id = 1000; \ No newline at end of file