Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
modul:m290:learningunits:lu05:theorie:03 [2024/09/27 12:04] – vdemir | modul:m290:learningunits:lu05:theorie:03 [2024/10/17 12:42] (aktuell) – vdemir | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== | + | ====== |
**Source**: | **Source**: | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | - [[https:// |
- | | + | |
+ | | ||
===== Learning Objectives ===== | ===== Learning Objectives ===== | ||
- | - Discuss what database | + | - Discuss what database |
- Explain the four most important CONSTRAINTS in database systems | - Explain the four most important CONSTRAINTS in database systems | ||
- | - Apply constraints | + | - Apply CONSTRAINTS |
===== Overview ===== | ===== Overview ===== | ||
** Sources: ** | ** Sources: ** | ||
- | * [[https:// | + | * [[https:// |
* [[https:// | * [[https:// | ||
- | MySQL constraints ensure data integrity, enforcing rules at the database level. | + | MySQL constraints ensure data integrity, enforcing rules at the database level. |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | Let’s explore these constraints | + | Let’s explore these CONSTRAINTS |
- | ==== PRIMARY KEY ==== | + | ==== 1. PRIMARY KEY ==== |
The Primary Key constraint uniquely identifies each record in a table. A primary key column (or a set of columns) must contain unique, non-null values. Each table can have only one primary key. | The Primary Key constraint uniquely identifies each record in a table. A primary key column (or a set of columns) must contain unique, non-null values. Each table can have only one primary key. | ||
Zeile 48: | Zeile 49: | ||
); | ); | ||
- | ==== FOREIGN KEY ==== | + | ==== 2. AUTO INCREMENT |
- | The Foreign Key constraint ensures referential integrity by linking | + | The AUTO_INCREMENT attribute automatically generates |
** General Syntax ** | ** General Syntax ** | ||
CREATE TABLE table_name ( | CREATE TABLE table_name ( | ||
- | | + | |
- | | + | |
- | FOREIGN | + | |
); | ); | ||
- | | + | |
** Example ** | ** Example ** | ||
- | CREATE TABLE orders | + | CREATE TABLE products |
- | | + | |
- | | + | |
- | FOREIGN KEY (customer_id) REFERENCES customers(customer_id) | + | |
); | ); | ||
- | In this example, customer_id in the orders table references the customer_id in the customers table. This relationship ensures that any customer_id in the orders table corresponds to an existing customer in the customers table. | + | Here, product_id automatically increments each time a new row is inserted, ensuring a unique ID for every product. |
| | ||
- | ==== NOT NULL ==== | + | ==== 3. NOT NULL ==== |
The NOT NULL constraint ensures that a column cannot contain a NULL value. It is used when the column must always have a value. | The NOT NULL constraint ensures that a column cannot contain a NULL value. It is used when the column must always have a value. | ||
Zeile 86: | Zeile 85: | ||
In this example, the employee_name column is constrained to never contain a NULL value, ensuring every employee has a name. | In this example, the employee_name column is constrained to never contain a NULL value, ensuring every employee has a name. | ||
- | ==== AUTO INCREMENT | + | ==== 4. UNIQUE |
- | The AUTO_INCREMENT attribute automatically generates | + | The UNIQUE constraint ensures that all values in a column (or a set of columns) are unique |
** General Syntax ** | ** General Syntax ** | ||
- | |||
CREATE TABLE table_name ( | CREATE TABLE table_name ( | ||
- | column_name datatype | + | column_name datatype |
- | PRIMARY KEY (column_name) | + | |
); | ); | ||
+ | | ||
+ | ** Example ** | ||
- | ** Example ** | + | |
- | | + | |
- | | + | |
- | | + | |
); | ); | ||
- | Here, product_id automatically increments each time a new row is inserted, ensuring a unique | + | In this case, the email column must contain |
- | ==== UNIQUE | + | ==== 5. FOREIGN KEY ==== |
- | The UNIQUE | + | The Foreign Key constraint ensures |
** General Syntax ** | ** General Syntax ** | ||
+ | |||
CREATE TABLE table_name ( | CREATE TABLE table_name ( | ||
- | | + | |
+ | column_name2 datatype, | ||
+ | FOREIGN KEY (column_name1) REFERENCES another_table(primary_key_column) | ||
); | ); | ||
| | ||
** Example ** | ** Example ** | ||
- | + | | |
- | | + | |
- | | + | |
- | | + | FOREIGN KEY (customer_id) REFERENCES customers(customer_id) |
); | ); | ||
- | In this case, the email column must contain unique values. No two users can have the same email address. | + | In this example, customer_id in the orders table references the customer_id in the customers table. This relationship ensures that any customer_id in the orders table corresponds to an existing customer in the customers table. |
===== Practical Example with Multiple Constraints ===== | ===== Practical Example with Multiple Constraints ===== | ||
Zeile 132: | Zeile 133: | ||
); | ); | ||
===== Summary ===== | ===== Summary ===== | ||
+ | In summary, MySQL constraints like Primary Key, Foreign Key, NOT NULL, AUTO_INCREMENT, | ||
+ | |||
+ | * **PRIMARY KEY** is applied to student_id to uniquely identify each data record in the table. | ||
* **AUTO_INCREMENT** is applied to student_id to automatically generate a unique ID for each student. | * **AUTO_INCREMENT** is applied to student_id to automatically generate a unique ID for each student. | ||
* **NOT NULL** is applied to student_name, | * **NOT NULL** is applied to student_name, | ||
Zeile 141: | Zeile 145: | ||
* **Preventing Errors:** Constraints like NOT NULL prevent the insertion of incomplete or invalid data. | * **Preventing Errors:** Constraints like NOT NULL prevent the insertion of incomplete or invalid data. | ||
* **Automation: | * **Automation: | ||
- | |||
- | In summary, MySQL constraints like Primary Key, Foreign Key, NOT NULL, AUTO_INCREMENT, | ||
- | |||
- | |||
- | |||
===== Vocabulary ===== | ===== Vocabulary ===== |