diff --git a/daten-abfragen/notes.md b/daten-abfragen/notes.md index 294c427..e62ae9c 100644 --- a/daten-abfragen/notes.md +++ b/daten-abfragen/notes.md @@ -53,4 +53,8 @@ - `SELECT CONCAT(spalte1, spalte2, ...)` Verknüpft mehrere Spalten oder Zeichenketten miteinander und gibt die kombinierte Zeichenkette zurück. - `REPLACE(spalte, 'suchen', 'ersetzen')` - Ersetzt alle Vorkommen des Suchstrings durch den Ersetzungsstring in der angegebenen Spalte. \ No newline at end of file + Ersetzt alle Vorkommen des Suchstrings durch den Ersetzungsstring in der angegebenen Spalte. + +## Sonderfall `NULL` +Mit `IS NULL` oder `IS NOT NULL` kann geprüft werden, ob ein Wert in einer Spalte `NULL` ist oder nicht. +Mit `NULL` kann nicht verglichen werden. `NULL = NULL` ergibt `NULL`, nicht `TRUE` oder `FALSE`. \ No newline at end of file diff --git a/daten-manipulieren/datatypes.md b/daten-manipulieren/datatypes.md new file mode 100644 index 0000000..5a79886 --- /dev/null +++ b/daten-manipulieren/datatypes.md @@ -0,0 +1,70 @@ +# MySQL Data Types Cheatsheet + +## 1. **Numeric Data Types** + +### Integer Types +| Data Type | Storage | Range (Signed) | Range (Unsigned) | +|-----------|---------|---------------|-------------------| +| `TINYINT` | 1 byte | -128 to 127 | 0 to 255 | +| `SMALLINT` | 2 bytes | -32,768 to 32,767 | 0 to 65,535 | +| `MEDIUMINT` | 3 bytes | -8,388,608 to 8,388,607 | 0 to 16,777,215 | +| `INT` / `INTEGER` | 4 bytes | -2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 | +| `BIGINT` | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0 to 18,446,744,073,709,551,615 | + +### Floating-Point Types +| Data Type | Storage | Range | Precision | +|-----------|---------|--------|-----------| +| `FLOAT(m,d)` | 4 bytes | Approx. ±3.4E38 | Rounded due to floating-point approximation | +| `DOUBLE(m,d)` / `REAL` | 8 bytes | Approx. ±1.8E308 | Higher precision than FLOAT but still subject to rounding | +| `DECIMAL(m,d)` / `NUMERIC(m,d)` | Varies | Exact fixed-point values | Precise representation, no rounding errors | + +#### **Floating-Point Precision Considerations** +- **`FLOAT` and `DOUBLE`** are subject to rounding errors due to floating-point representation and should not be used where exact precision is required (e.g., financial applications). +- **`DECIMAL`** is a fixed-point type that maintains exact precision and is ideal for storing monetary values, making it the preferred choice for banking applications. + +## 2. **String Data Types** +| Data Type | Storage | Description | +|-----------|---------|-------------| +| `CHAR(n)` | 1 byte per character | Fixed-length string (0-255 characters) | +| `VARCHAR(n)` | 1 byte per character + 1 or 2 bytes | Variable-length string (0-65,535 characters) | +| `TEXT` | Varies | Large text field (0-4GB depending on type) | +| `TINYTEXT` | Up to 255 bytes | Small text field | +| `TEXT` | Up to 65,535 bytes | Standard text field | +| `MEDIUMTEXT` | Up to 16,777,215 bytes | Medium text field | +| `LONGTEXT` | Up to 4GB | Very large text field | +| `BLOB` | Varies | Binary large object (same size variations as TEXT) | + +## 3. **Date and Time Data Types** +| Data Type | Storage | Format | Range | +|-----------|---------|--------|--------| +| `DATE` | 3 bytes | YYYY-MM-DD | 1000-01-01 to 9999-12-31 | +| `DATETIME` | 8 bytes | YYYY-MM-DD HH:MM:SS | 1000-01-01 00:00:00 to 9999-12-31 23:59:59 | +| `TIMESTAMP` | 4 bytes | YYYY-MM-DD HH:MM:SS | 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC | +| `TIME` | 3 bytes | HH:MM:SS | -838:59:59 to 838:59:59 | +| `YEAR` | 1 byte | YYYY | 1901 to 2155 | + +## 4. **Boolean Type** +| Data Type | Alias For | Description | +|-----------|-----------|-------------| +| `BOOLEAN` | `TINYINT(1)` | Stores 0 (false) or 1 (true) | + +## 5. **Spatial Data Types** +| Data Type | Description | +|-----------|-------------| +| `GEOMETRY` | Stores any spatial value | +| `POINT` | Stores (X,Y) coordinate | +| `LINESTRING` | Stores a line of multiple points | +| `POLYGON` | Stores a polygon | + +## 6. **JSON Data Type** +| Data Type | Description | +|-----------|-------------| +| `JSON` | Stores JSON-formatted data efficiently | + +### **Notes:** +- Use `UNSIGNED` for numeric types when negative values are not needed. +- `DECIMAL(m,d)` is preferred for exact decimal values (e.g., financial applications). +- Use `TEXT` for large text fields instead of `VARCHAR` when indexing is not required. +- `TIMESTAMP` is useful for automatic time zone conversion, while `DATETIME` is better for absolute timestamps. +- `JSON` is useful for semi-structured data storage. + diff --git a/daten-manipulieren/table-manipulation.sql b/daten-manipulieren/table-manipulation.sql index 97519c0..25aea02 100644 --- a/daten-manipulieren/table-manipulation.sql +++ b/daten-manipulieren/table-manipulation.sql @@ -1,6 +1,47 @@ create table if not exists stores ( - street varchar(100), + street varchar(100) NOT NULL, description text -) +); +alter table stores + add column name varchar(100); + +alter table stores + drop column name; + +alter table stores + add column name varchar(100) first; # Bei MySQL gibt es kein BEFORE, es gibt nur AFTER und FIRST + +alter table stores + modify column name varchar(100); + +alter table stores + add column city varchar(100) after street; + +alter table stores + add column rent decimal(8, 2); + +alter table stores + add column num_sales int unsigned; # Unsigned (nur positive Zahlen) + +INSERT INTO stores (street, city, description, rent, num_sales) +VALUES ('123 Main St', 'New York', 'Spacious 2-bedroom apartment in the heart of downtown.', 2500.00, 5), + ('456 Elm St', 'Los Angeles', 'Modern loft with great city views and natural light.', 3200.50, 3), + ('789 Pine Ave', 'Chicago', 'Cozy studio close to public transport and shopping.', 1800.75, 7), + ('101 Maple Rd', 'San Francisco', 'Luxury penthouse with rooftop access and premium amenities.', 5500.00, 2), + ('202 Oak St', 'Miami', 'Beachfront condo with private balcony and ocean views.', 4200.25, 4); + +create table if not exists users +( + id int primary key auto_increment, + email varchar(100) not null, + password varchar(100) not null, + created_at timestamp default current_timestamp +); + +insert into users (email, password) +values ('test@test.de', '123456'); + +alter table users + add column num_books int not null default 0; \ No newline at end of file