SQL ALTER TABLE 语句

ALTER TABLE 语句

ALTER TABLE 语句用于在已有的表中添加、修改或删除列。

SQL ALTER TABLE 语法

টেবিলে কলাম যোগ করতে, নিচের গ্রাফিক্স ব্যবহার করুন:

ALTER TABLE table_name
ADD column_name datatype

টেবিলের কলাম মুক্ত করতে, নিচের গ্রাফিক্স ব্যবহার করুন:

ALTER TABLE table_name
DROP COLUMN column_name

মন্তব্য:কোনও ডাটাবেস সিস্টেম ডাটাবেস টেবিলের কলাম মুক্ত করার এই পদ্ধতিকে অনুমদিত করে না (DROP COLUMN column_name)।

To change the data type of a column in the table, use the following syntax:

ALTER TABLE table_name
ALTER COLUMN column_name datatype

The original table (used in the example):

Persons table:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

SQL ALTER TABLE instance

Now, we want to add a new column named 'Birthday' to the table 'Persons'.

We use the following SQL statement:

ALTER TABLE Persons
ADD Birthday date

Please note that the type of the new column 'Birthday' is date, which can store dates. The data type specifies the type of data that can be stored in the column.

The new 'Persons' table will be like this:

Id LastName FirstName Address City Birthday
1 Adams John Oxford Street London  
2 Bush George Fifth Avenue New York  
3 Carter Thomas Changan Street Beijing  

Change data type instance

Now we want to change the data type of the 'Birthday' column in the 'Persons' table.

We use the following SQL statement:

ALTER TABLE Persons
ALTER COLUMN Birthday year

Please note that the data type of the 'Birthday' column is year, which can store years in 2-digit or 4-digit format.

DROP COLUMN instance

Next, we delete the 'Birthday' column from the 'Person' table:

ALTER TABLE Person
DROP COLUMN Birthday

The 'Persons' table will become like this:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing