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:02 [2024/09/16 15:21] – vdemir | modul:m290:learningunits:lu05:theorie:02 [2024/10/17 12:43] (aktuell) – vdemir | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== | + | ====== |
| - | + | ||
| - | **Source**: [[https:// | + | |
| + | **Source**: | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| ===== Learning Objectives ===== | ===== Learning Objectives ===== | ||
| - | | + | In this learning unit, we will learn how to manage database tables, i.e. |
| + | | ||
| + | - how to modify and | ||
| + | - how to delete database tables | ||
| + | |||
| + | ===== CREATE TABLE ===== | ||
| + | Let's start by creating a table based on the ERD of the student management tool, as shown below: | ||
| + | |||
| + | Our goal is to create the table " | ||
| + | |||
| + | CREATE TABLE table_name ( | ||
| + | column1 datatype, | ||
| + | column2 datatype, | ||
| + | column3 datatype, | ||
| + | | ||
| + | ); | ||
| + | |||
| + | In our example the our concrete CREATE statement would look like: | ||
| + | '' | ||
| + | CREATE TABLE student ( | ||
| + | stundent_id INT, | ||
| + | name VARCHAR(50), | ||
| + | surname VARCHAR(50), | ||
| + | birthdate DATE, | ||
| + | matrno CHAR(6) | ||
| + | ); | ||
| + | |||
| + | After executing the DDL create command, the result would look like the following illustration. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | **Hints:** | ||
| + | * On the right upper side you can see, that the table " | ||
| + | * After a select on this table, we can see that the table does not yet contain any data. | ||
| + | * It's not a big deal so far, is it? 8-). | ||
| + | |||
| + | ===== ALTER TABLE ===== | ||
| + | It may happen that we need to change something, e.g. the change the attribute name or add a new attribute. In this case, the ALTER TABLE commands will do the work. | ||
| + | |||
| + | The basic syntax of the DDL "ALTER TABLE" syntax is: | ||
| + | |||
| + | ALTER TABLE table_name | ||
| + | ADD column_name datatype; | ||
| + | |||
| + | ==== ADD COLUMN ==== | ||
| + | In practical use, if we want to add an email to the student' | ||
| + | |||
| + | ALTER TABLE student | ||
| + | ADD Email varchar(255); | ||
| + | |||
| + | ==== DROP COLUMN ==== | ||
| + | If we can add a column, it should come as no surprise that we also can delete a column. The syntax for deleting an entire column from a table is: | ||
| + | |||
| + | ALTER TABLE table_name | ||
| + | DROP COLUMN column_name; | ||
| + | |||
| + | The following DDL statement removes the currently added column email from the table student. | ||
| + | |||
| + | ALTER TABLE Customers | ||
| + | DROP COLUMN Email; | ||
| + | |||
| + | ==== RENAME COLUMN ==== | ||
| + | Naturally, we also can just rename a wrongly spelled column name. The syntax in that case is: | ||
| + | |||
| + | ALTER TABLE table_name | ||
| + | RENAME COLUMN old_name to new_name; | ||
| + | |||
| + | Or more practical, if we want to change the name of the column | ||
| + | |||
| + | ALTER TABLE student | ||
| + | RENAME COLUMN matrno to studentno; | ||
| + | |||
| + | ==== MODIFY DATATYPE ==== | ||
| + | We also can change the datatype of colums. The basic syntax would be: | ||
| + | |||
| + | ALTER TABLE table_name | ||
| + | ALTER COLUMN column_name datatype; | ||
| + | |||
| + | And of course, as a concrete practical example, in which we change the length of the column " | ||
| + | |||
| + | ALTER TABLE student | ||
| + | MODIFY COLUMN name VARCHAR(100); | ||
| + | |||
| + | ===== DROP TABLE ===== | ||
| + | If, for whatever reason, we have created a table incorrectly or want to get rid of an obsolete table, we need the command DROP TABLE: | ||
| + | |||
| + | DROP TABLE table_name; | ||
| - | ===== Overview ===== | + | **Hint: <color # |
| - | ... | + | |
| - | - ... | + | |
| ==== Vocabulary ==== | ==== Vocabulary ==== | ||
| ^English ^ Deutsch ^ | ^English ^ Deutsch ^ | ||
| - | | ::: | ... | | + | | column |
| ---- | ---- | ||
| [[https:// | [[https:// | ||