SQL ALTER TABLE statement
- Vorige pagina SQL Drop
- Volgende pagina SQL Increment
ALTER TABLE statement
The ALTER TABLE statement is used to add, modify, or delete columns in an existing table.
SQL ALTER TABLE syntax
To add a column to a table, please use the following syntax:
ALTER TABLE table_name ADD column_name datatype
To delete a column from a table, please use the following syntax:
ALTER TABLE table_name DROP COLUMN column_name
Note:Some database systems do not allow this method of deleting a column from a database table (DROP COLUMN column_name).
Gebruik de volgende syntaxis om het gegevenstype van een kolom in een tabel te wijzigen:
ALTER TABLE table_name ALTER COLUMN column_name datatype
Oorspronkelijke tabel (gebruikt in voorbeelden):
Tabel "Persons":
Id | Achternaam | Voornaam | Adres | Stad |
---|---|---|---|---|
1 | Adams | John | Oxfordstraat | Londen |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changanstraat | Peking |
Voorbeeld van SQL ALTER TABLE
Nu willen we een nieuwe kolom genaamd "Birthday" toevoegen aan de tabel "Persons".
We gebruiken de volgende SQL-statement:
ALTER TABLE Persons ADD Birthday date
Let op, het nieuwe type van de kolom "Birthday" is date en kan datums opslaan. Het gegevenstype specifies het type van gegevens dat in een kolom kan worden opgeslagen.
De nieuwe tabel "Persons" zal er als volgt uitzien:
Id | Achternaam | Voornaam | Adres | Stad | Birthday |
---|---|---|---|---|---|
1 | Adams | John | Oxfordstraat | Londen | |
2 | Bush | George | Fifth Avenue | New York | |
3 | Carter | Thomas | Changanstraat | Peking |
Voorbeeld van wijzigen van gegevenstype
Nu willen we de gegevenstype van de kolom "Birthday" in de tabel "Persons" wijzigen.
We gebruiken de volgende SQL-statement:
ALTER TABLE Persons ALTER COLUMN Birthday year
Let op, de gegevenstype van de kolom "Birthday" is year en kan jaargangen in 2 of 4-cijferige formaten opslaan.
DROP COLUMN voorbeeld
Daarnaast zullen we de kolom "Birthday" van de tabel "Person" verwijderen:
ALTER TABLE Person DROP COLUMN Birthday
De tabel "Persons" zal er als volgt uitzien:
Id | Achternaam | Voornaam | Adres | Stad |
---|---|---|---|---|
1 | Adams | John | Oxfordstraat | Londen |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changanstraat | Peking |
- Vorige pagina SQL Drop
- Volgende pagina SQL Increment