More SQL notes

This commit is contained in:
thomasabishop 2023-01-10 13:45:04 +00:00
parent 7fa40a435c
commit 87bb85ebcc
4 changed files with 69 additions and 8 deletions

View file

@ -10,9 +10,12 @@ tags: [SQL]
```sql
SHOW DATABASES
USE [database_name]
SHOW TABLES
-- View column names and data types
DESCRIBE [table_name]
-- View disk usage per column
SHOW TABLE STATUS
```

View file

@ -0,0 +1,8 @@
---
categories:
- Databases
- Programming Languages
tags: [SQL, relational-database]
---
# Autoincrement and `SERIAL`

View file

@ -13,15 +13,16 @@ An understanding of the differences between data types is important because it o
### Integer (`INT`)
No fractional part, fixed number of digits
- No fractional part, fixed number of digits
- `TINYINT`, `BIGINT` etc are all instances of `INT`, the difference is the size of the number stored.
### Fixed point
### Fixed point (`DECIMAL`)
Contains decimal point. Use when accuracy is more important that representing very large or very small values
Contains decimal point. Use when accuracy is more important that representing very large or very small values. Hence you would use this for monetary amounts.
### Floating point
### Floating point (`FLOAT`)
Contains decimal point. Use when the ability to represent very large and very small values is more important than precision
Contains decimal point. Use when the ability to represent very large and very small values is more important than precision. Hence you would not use this for monetary amounts.
## String
@ -29,7 +30,7 @@ Contains decimal point. Use when the ability to represent very large and very sm
Spaces are stripped in storage but represented with a character set.
An example would be `CHAR(10)` or `CHAR(3)`. Here we set the upper limit but it must be the case that no string exceeds it.
An example would be `CHAR(10)` or `CHAR(3)`. Here we set the upper limit but it must be the case that no string exceeds it. For either of these, if you add a value that is, e.g, two characters in length, it will add spaces to pad it out and make up the upper limit.
### Variable-length character strings

View file

@ -0,0 +1,49 @@
---
categories:
- Databases
- Programming Languages
tags: [SQL, relational-database]
---
# Useful operators in SQL
## True and false
> In SQL false results are represented by `0` and true results are represented by `1`. Any zero value is false and any non-zero value is true.
> When comparing operators we can use `SELECT` as a general "execute" function for SQL to interpret
```sql
SELECT 0 = 0 -- 1
SELECT 0.0 = 0 -- 1
SELECT 9 >= 12 -- 0
```
## Boolean operators
SQL supports all Boolean operators:
```sql
SELECT (9 > 7) AND (3 < 5) -- 1
SELECT (3 > 1) OR (2 > 5) -- 1
SELECT (3 > 1) XOR (5 > 1) -- 0
```
We also have `IS` and `IS NOT` for testing Boolean values:
```sql
SELECT (9 > 7) IS TRUE -- 1
SELECT (9 > 7) IS NOT TRUE -- 0
```
## `NULL`
- `NULL` is neither true (1) or false (0). It has no value.
- An empty string (`""`) is **not** `NULL`
```sql
SELECT 1 IS NULL -- 0
SELECT 1 IS NOT NULL -- 1
SELECT NULL IS NULL -- 1
SELECT "" IS NULL -- 0
```