Last Sync: 2022-08-05 20:00:04
This commit is contained in:
parent
5ca968113f
commit
c679657cd8
6 changed files with 129 additions and 0 deletions
82
Databases/SQL/2_SELECT.md
Normal file
82
Databases/SQL/2_SELECT.md
Normal file
|
@ -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
|
0
Databases/SQL/3_INSERT.md
Normal file
0
Databases/SQL/3_INSERT.md
Normal file
0
Databases/SQL/4_WHERE.md
Normal file
0
Databases/SQL/4_WHERE.md
Normal file
35
Databases/SQL/5_UPDATE.md
Normal file
35
Databases/SQL/5_UPDATE.md
Normal file
|
@ -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 = '<http://www.hp.co.uk>'
|
||||
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 = '<http://www.apple.co.uk>',
|
||||
year_founded = 1977
|
||||
WHERE manufacturer_id = 2;
|
||||
````
|
12
Databases/SQL/6_DELETE.md
Normal file
12
Databases/SQL/6_DELETE.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
tags:
|
||||
- Programming_Languages
|
||||
- Databases
|
||||
- sql
|
||||
---
|
||||
|
||||
# SQL: The DELETE query
|
||||
|
||||
````sql
|
||||
DELETE FROM sales WHERE sale_id = 1;
|
||||
````
|
0
Databases/SQL/7_ALTER.md
Normal file
0
Databases/SQL/7_ALTER.md
Normal file
Loading…
Add table
Reference in a new issue