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/25 14:25] – vdemir | modul:m290:learningunits:lu05:theorie:02 [2024/10/17 12:43] (aktuell) – vdemir | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== | + | ====== |
**Source**: | **Source**: | ||
- | * [[https:// | + | * [[https:// |
- | * [[https:// | + | * [[https:// |
- | * [[https:// | + | * [[https:// |
===== Learning Objectives ===== | ===== Learning Objectives ===== | ||
- | In this learning unit, we will learn how to manage database tables, i.e. how to create, modify and delete database tables. | + | In this learning unit, we will learn how to manage database tables, i.e. |
+ | - how to create | ||
+ | - 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: | 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 " |
- | + | ||
- | Our goal is to create the table " | + | |
CREATE TABLE table_name ( | CREATE TABLE table_name ( | ||
Zeile 23: | Zeile 25: | ||
In our example the our concrete CREATE statement would look like: | In our example the our concrete CREATE statement would look like: | ||
+ | '' | ||
CREATE TABLE student ( | CREATE TABLE student ( | ||
stundent_id INT, | stundent_id INT, | ||
Zeile 29: | Zeile 31: | ||
surname VARCHAR(50), | surname VARCHAR(50), | ||
birthdate DATE, | birthdate DATE, | ||
- | matrno: CHAR(6) | + | 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; | ||
+ | **Hint: <color # | ||
==== Vocabulary ==== | ==== Vocabulary ==== | ||
^English ^ Deutsch ^ | ^English ^ Deutsch ^ | ||
- | | ::: | ... | | + | | column |
---- | ---- | ||
[[https:// | [[https:// | ||