From c679657cd82d769daeee2d4b22e98816262184ec Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Fri, 5 Aug 2022 20:00:04 +0100 Subject: [PATCH] Last Sync: 2022-08-05 20:00:04 --- Databases/SQL/2_SELECT.md | 82 +++++++++++++++++++++++++++++++++++++++ Databases/SQL/3_INSERT.md | 0 Databases/SQL/4_WHERE.md | 0 Databases/SQL/5_UPDATE.md | 35 +++++++++++++++++ Databases/SQL/6_DELETE.md | 12 ++++++ Databases/SQL/7_ALTER.md | 0 6 files changed, 129 insertions(+) create mode 100644 Databases/SQL/2_SELECT.md create mode 100644 Databases/SQL/3_INSERT.md create mode 100644 Databases/SQL/4_WHERE.md create mode 100644 Databases/SQL/5_UPDATE.md create mode 100644 Databases/SQL/6_DELETE.md create mode 100644 Databases/SQL/7_ALTER.md diff --git a/Databases/SQL/2_SELECT.md b/Databases/SQL/2_SELECT.md new file mode 100644 index 0000000..9311fa6 --- /dev/null +++ b/Databases/SQL/2_SELECT.md @@ -0,0 +1,82 @@ +--- +tags: + - Programming_Languages + - Databases + - sql +--- + +# SQL: The SELECT query + +## Print/retrieve/write an entire table, unfiltered + +````sql +SELECT * FROM [table_name] + +SELECT * FROM model +```` + +## Retrieve all data from a specific field + +````sql +SELECT [field_name] FROM [table_name] + +SELECT name FROM manufacturer +```` + +## Retrieve data and order it + +This example orders alphabetically: + +````sql +SELECT [field_name] FROM [table_name] ORDER BY [property] +SELECT name FROM model ORDER BY name +```` + > When `ORDER BY` is used the default method for strings is alphabetical and for integers it is ascending order. + +Here's a more complex real-life request: + +````sql +SELECT name, cores, ram FROM model ORDER BY ram, name +```` + +It gives us: + +```` +name cores ram +---------------- ---------- ---------- +Commodore VIC-20 1 0.005 +Commodore 64 1 0.064 +Amiga 500 1 0.5 +Apple Lisa 1 1.0 +Raspberry Pi 1 M 1 256.0 +Raspberry Pi 1 M 1 256.0 +Raspberry Pi 1 M 1 512.0 +Raspberry Pi 1 M 1 512.0 +Raspberry Pi 1 M 1 512.0 +Raspberry Pi Zer 1 512.0 +```` + +But we can obviously specify our own ordering method: + +````sql +SELECT name, cores, release_date FROM model ORDER BY cores DESC, name; +```` + +Returns: + +```` +name cores release_date +----------------- ---------- ------------ +Apple MacBook Pro 6 2019-05-21 +Apple iMac 4 2019-03-19 +Raspberry Pi 2 Mo 4 2015-02-01 +Raspberry Pi 3 Mo 4 2018-11-01 +Raspberry Pi 3 Mo 4 2016-02-01 +Raspberry Pi 3 Mo 4 2018-03-14 +Raspberry Pi 4 Mo 4 2019-06-24 +Raspberry Pi 4 Mo 4 2019-06-24 +Raspberry Pi 4 Mo 4 2019-06-24 +```` + + > + > `ORDER BY` always comes last, after the selection and any filtering clauses but *before* a `WHERE` clause diff --git a/Databases/SQL/3_INSERT.md b/Databases/SQL/3_INSERT.md new file mode 100644 index 0000000..e69de29 diff --git a/Databases/SQL/4_WHERE.md b/Databases/SQL/4_WHERE.md new file mode 100644 index 0000000..e69de29 diff --git a/Databases/SQL/5_UPDATE.md b/Databases/SQL/5_UPDATE.md new file mode 100644 index 0000000..4c8a1b0 --- /dev/null +++ b/Databases/SQL/5_UPDATE.md @@ -0,0 +1,35 @@ +--- +tags: + - Programming_Languages + - Databases + - sql +--- + +# SQL: The UPDATE clause + +With UPDATE we modify existing records. + +## Schematic syntax + +````sql +UPDATE [table_name] +SET [field] +WHERE [conditional expression/filter] +```` + +## Real example + +````sql +UPDATE manufacturer +SET url = '' +WHERE manufacturer_id = 4; --typically this will be the primary key as you are updating and existing record and need to identify it uniquely +```` + +## Multiple fields + +````sql +UPDATE manufacturer +SET url = '', + year_founded = 1977 +WHERE manufacturer_id = 2; +```` \ No newline at end of file diff --git a/Databases/SQL/6_DELETE.md b/Databases/SQL/6_DELETE.md new file mode 100644 index 0000000..65c8e47 --- /dev/null +++ b/Databases/SQL/6_DELETE.md @@ -0,0 +1,12 @@ +--- +tags: + - Programming_Languages + - Databases + - sql +--- + +# SQL: The DELETE query + +````sql +DELETE FROM sales WHERE sale_id = 1; +```` \ No newline at end of file diff --git a/Databases/SQL/7_ALTER.md b/Databases/SQL/7_ALTER.md new file mode 100644 index 0000000..e69de29