More SQL notes

This commit is contained in:
thomasabishop 2023-01-04 12:54:45 +00:00
parent 2e18f13a91
commit ff08ebf998
2 changed files with 74 additions and 1 deletions

View file

@ -9,7 +9,7 @@ tags: [SQL, relational-database]
We create tables in an SQL database with the `CREATE` command.
Below is an example of this in practice. Each field corresponds to a column. We specify the name of the field and its corresponding data-type. Every table must have a **primary key**. In the example, `employee_id` is the primary key.
Below is an example of this in practice. Each field corresponds to a column. We specify the name of the field and its corresponding [data type](/Databases/SQL/Data_types_in_MySQL.md). Every table must have a **primary key**. In the example, `employee_id` is the primary key.
```sql
CREATE TABLE employee (
@ -26,3 +26,9 @@ CREATE TABLE employee (
days_per_week real
);
```
## Check table metadata
We can use `DESCRIBE [table_name]` any time we want to list out the data types and other metadata about the table.
We can use `SHOW TABLE STATUS`/

View file

@ -0,0 +1,67 @@
---
categories:
- Databases
- Programming Languages
tags: [SQL, relational-database]
---
# Data types in MySQL
An understanding of the differences between data types is important because it obviously affects the amount of memory the database uses.
## Numeric
### Integer (`INT`)
No fractional part, fixed number of digits
### Fixed point
Contains decimal point. Use when accuracy is more important that representing very large or very small values
### Floating point
Contains decimal point. Use when the ability to represent very large and very small values is more important than precision
## String
### Fixed-length character strings ()
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.
### Variable-length character strings
As above but allowing for variable-length strings.
A common example is `VARCHAR(255)`. The 255 refers to the maximal character length, not the byte length. We must put `255` as the parameter even if our character lengths will be below this but where we don't know the minimum and maximum length.
## Large object storage
### Blob
For storing images, audio and other binary media.
Variants: `TINYBLOB`, `BLOB`, `MEDIUMBLOB` `LONGBLOB`
### Text
Like blob but has character set
Variants: `TINYTEXT`, `TEXT`, `MEDIUMTEXT`, `LONGTEXT`
## Date and time
- `DATE`
- `DATETIME`
- `TIMESTAMP`
## Speciality
### Enumerations
- `ENUM`: a single value from a list
- `SET`: several values from a list
## Storage limits
Each row can have a maximum of 65,535 bytes.