From 87bb85ebcc5a7f4548d6f3528b89602f3883062b Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Tue, 10 Jan 2023 13:45:04 +0000 Subject: [PATCH] More SQL notes --- .../Accessing_metadata_about_SQL_database.md | 7 ++- .../Autoincrement_for_unique_key_in_SQL.md | 8 +++ Databases/SQL/Data_types_in_MySQL.md | 13 ++--- Databases/SQL/Useful_operators_in_SQL.md | 49 +++++++++++++++++++ 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 Databases/SQL/Autoincrement_for_unique_key_in_SQL.md create mode 100644 Databases/SQL/Useful_operators_in_SQL.md diff --git a/Databases/SQL/Accessing_metadata_about_SQL_database.md b/Databases/SQL/Accessing_metadata_about_SQL_database.md index cc8c30e..69649b7 100644 --- a/Databases/SQL/Accessing_metadata_about_SQL_database.md +++ b/Databases/SQL/Accessing_metadata_about_SQL_database.md @@ -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 + ``` diff --git a/Databases/SQL/Autoincrement_for_unique_key_in_SQL.md b/Databases/SQL/Autoincrement_for_unique_key_in_SQL.md new file mode 100644 index 0000000..18621e9 --- /dev/null +++ b/Databases/SQL/Autoincrement_for_unique_key_in_SQL.md @@ -0,0 +1,8 @@ +--- +categories: + - Databases + - Programming Languages +tags: [SQL, relational-database] +--- + +# Autoincrement and `SERIAL` diff --git a/Databases/SQL/Data_types_in_MySQL.md b/Databases/SQL/Data_types_in_MySQL.md index 21c68ca..026b787 100644 --- a/Databases/SQL/Data_types_in_MySQL.md +++ b/Databases/SQL/Data_types_in_MySQL.md @@ -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 diff --git a/Databases/SQL/Useful_operators_in_SQL.md b/Databases/SQL/Useful_operators_in_SQL.md new file mode 100644 index 0000000..323e8c1 --- /dev/null +++ b/Databases/SQL/Useful_operators_in_SQL.md @@ -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 +```