修改列與更改列 (Modify column Vs change column)


問題描述

修改列與更改列 (Modify column Vs change column)

I know, we can not rename a column using modify column syntax,but can change column syntax.

My question is: what is the main usage of modify syntax?

For example,

alter table tablename change col1 col1 int(10) not null

instead of 

alter table tablename modify col1 int(10) not null

Edited Question replaced

What is the main usage of modify syntax?

Above question was replaced by below

Why we have to use change column instead of modify column?

‑‑‑‑‑

參考解法

方法 1:

CHANGE COLUMN If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don't need to remove it and make a replacement, you can simply rename it using change column

ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;

MODIFY COLUMN This command does everything CHANGE COLUMN can, but without renaming the column.You can use the modify SQL command if you need to resize a column in MySQL. By doing this you can allow more or less characters than before. You can't rename a column using modify and other 

ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;

Note : ALTER TABLE is used for altering a table means to change column name, size, drop column. CHANGE COLUMN and MODIFY COLUMN commands cannot be used without help of ALTER TABLE command.

方法 2:

The difference is whether you want to change the column name, column definition or both.

CHANGE

Can change a column name or definition, or both ALTER TABLE t1 CHANGE a b BIGINT NOT NULL

MODIFY

Can change a column definition but not its name ALTER TABLE t1 MODIFY b INT NOT NULL

RENAME COLUMN (from MySQL 8.0)

Can change a column name but not its definition ALTER TABLE t1 RENAME COLUMN b TO a


Also, CHANGE and MODIFY can be followed by an optional COLUMN keyword.

For complete explanation:

  1. MySQL 5.7 Docs‑ Renaming, Redefining, and Reordering Columns
  2. MySQL 8.0 Docs‑ Renaming, Redefining, and Reordering Columns
#### 方法 3:

I found one difference after more than an hour of effort in trying to make a non auto_increment column into auto_increment statement:

alter table `doctor_experience` modify column `id` int(11) unsigned auto_increment

works, but statment:

alter table `doctor_experience` change column `id` `id` int(11) unsigned auto_increment 

will report an error.

方法 4:

That is the same. It was done to support another syntax (Oracle ALTER TABLE as I know). You can use both of them.

Note: ALTER TABLE CHANGE old_col_name new_col_name syntax allows renaming column using one command.

方法 5:

Change Column : Used when we want to change the column name with its definition. eg ‑ alter table student CHANGE name full_name VARCHAR(32) NOT NULL;

Modify column : Used when column name is to be same but change in its definition. eg ‑ alter table student MODIFY full_name VARCHAR(64) NOT NULL;

Rename column : Used when we only need to change the column name (its definition will be same) alter table student RENAME COLUMN full_name TO name;

(by user2053420ParvathyshaahiinHongweiDevartvsharma)

參考文件

  1. Modify column Vs change column (CC BY‑SA 3.0/4.0)

#MySQL #alter-table






相關問題

MySQL WHERE 時間戳 >= SUBDATE(MAX(timestamp), INTERVAL 5 DAY) (MySQL WHERE timestamp >= SUBDATE(MAX(timestamp), INTERVAL 5 DAY))

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

在 MySQL 中查看授權 (View grants in MySQL)

修改列與更改列 (Modify column Vs change column)

SQL 查詢沒有返回任何值 (SQL query did not return any values)

IFNULL trong vấn đề tốc độ mysql (IFNULL in mysql speed issue)

Tối ưu hóa truy vấn MySQL PHP - Phản hồi (MySQL PHP Query Optimization - Feedbacks)

我將如何從我的數據庫中顯示用戶個人資料圖片? (How would I go about displaying user profile pictures from my database?)

自聯接以比較同一張表中的季度數據? (Self join to compare quarterly data from same table?)

設置默認數據庫連接 Mysql Workbench 5.2 (Set Default Database connection Mysql Workbench 5.2)

在 Mac 上將 PHP/MySQL/HTML 生成的報告轉換為 PDF 格式 (Convert reports generated in PHP/MySQL/HTML to PDF format on a Mac)

MYSQL查詢神奇地抓取數值 (MYSQL query magically grabs numeric value)







留言討論