Aithris Clàr Alter SQL

Tha an aithris ALTER TABLE ann an SQL air a chleachdadh gus colbhan bùird a chur ris, atharrachadh no a dhubhadh às.

Faodaidh sinn cuideachd aithris ALTER TABLE a chleachdadh gus diofar chuingealachaidhean bùird a chur ris no a leigeil sìos.



SQL ALTER TABLE - Cuir colbh ùr ris

Gus colbh ùr a chur ri clàr a tha ann mu thràth bidh sinn a ’cleachdadh an t-siostaim:


ALTER TABLE table_name ADD column_name datatype;

Eisimpleir

Tha an còd a leanas a ’cur colbh“ Start_Date ”ris a’ chlàr “Luchd-obrach”:

ALTER TABLE Employees ADD Start_Date date;

SQL ALTER TABLE - Thoir air falbh colbh

Gus colbh a thoirt air falbh bho chlàr a tha ann mar-thà bidh sinn a ’cleachdadh an co-fhacal:


ALTER TABLE table_name DROP COLUMN column_name;

Eisimpleir

Tha an còd a leanas a ’toirt air falbh an colbh“ Start_Date ”bhon chlàr“ Luchd-obrach ”:



ALTER TABLE Employees DROP COLUMN Start_Date;

SQL ALTER TABLE - Atharraich Colbh

Faodaidh sinn an ALTER TABLE a chleachdadh aithris gus datatype colbh atharrachadh le bhith a ’cleachdadh an t-siostaim:

ALTER TABLE table_name MODIFY column_name datatype;

Eisimpleir

Bidh an còd a leanas ag atharrachadh stòr-dàta a ’cholbh“ Start_Date ”bho date gu year:

ALTER TABLE Employees MODIFY Start_Date year;

SQL ALTER TABLE - Stòr-dàta Demo

Osbarr tha clàr againn air a bheil “Luchd-obrach” leis na colbhan a leanas:


+------------+-----------+----------+ | EmployeeID | FirstName | LastName | +------------+-----------+----------+ | 1

| Mark
| Otto
| | 2

| Jacob
| Thornton | | 3

| Su
| Bird
| | 4

| Sam
| Burger | +------------+-----------+----------+

Cuir Colbh ris

A-nis tha sinn airson an clàr “Luchd-obrach” a leudachadh agus colbh ùr a chuir ris an canar “Start_Date”

ALTER TABLE Employees ADD Start_Date date;

Toradh:

+------------+-----------+----------+------------+ | EmployeeID | FirstName | LastName | Start_Date | +------------+-----------+----------+------------+ | 1

| Mark
| Otto
|

| | 2

| Jacob
| Thornton |

| | 3

| Su
| Bird
|

| | 4

| Sam
| Burger |

| +------------+-----------+----------+------------+

Atharraich Colbh

An ath rud tha sinn airson an datatype den cholbh “Start_Date” atharrachadh bho date gu year:

ALTER TABLE Employees MODIFY Start_Date year;

Cuir às do cholbh

Tha sinn airson an colbh “Start_Date” a dhubhadh às a ’chlàr“ Luchd-obrach ”. Bidh sinn a ’cleachdadh:


ALTER TABLE Employees DROP COLUMN Start_Date;

Toradh:

+------------+-----------+----------+ | EmployeeID | FirstName | LastName | +------------+-----------+----------+ | 1

| Mark
| Otto
| | 2

| Jacob
| Thornton | | 3

| Su
| Bird
| | 4

| Sam
| Burger | +------------+-----------+----------+