1. 程式人生 > >Mysql修改欄位與修改表操作

Mysql修改欄位與修改表操作

當我們需要針對mysql的表名或者欄位名需要修改的時候,這個時候就需要用到mysql的alter命令。
為了方便起見,首先我們新建一張使用者表。

create table user(
`id` INT NOT NULL AUTO_INCREMENT,
`court_id` INT NOT NULL DEFAULT 1,
`user_id` INT NOT NULL DEFAULT 0,
PRIMARY KEY(`id`)
)
  ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

1. 新增刪除欄位

首先如果我們想刪除user_id欄位:

mysql> alter table user drop user_id;
Query OK, 0 rows affected (0.26 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user; +----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | court_id | int(11) | NO | | 1 | | +----------+
---------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)

注意:如果資料表中只剩餘一個欄位則無法使用DROP來刪除欄位。

我們發現user_id刪除錯誤,想添加回來:

mysql> alter table user add user_id int;
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra | +----------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | court_id | int(11) | NO | | 1 | | | user_id | int(11) | YES | | NULL | | +----------+---------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)

可以看出來,此時user_id自動新增到了表的最後。

如果我們想指定新增欄位的位置,可以使用MySQL提供的關鍵字 FIRST (設定位第一列), AFTER 欄位名(設定位於某個欄位之後)。

mysql> alter table user drop user_id;
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table user add user_id int first;
Query OK, 0 rows affected (0.24 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| user_id  | int(11) | YES  |     | NULL    |                |
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| court_id | int(11) | NO   |     | 1       |                |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

此時first已經指定user_id為第一個欄位。

mysql> alter table user drop user_id;
Query OK, 0 rows affected (0.27 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table user add user_id int after id;
Query OK, 0 rows affected (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11) | YES  |     | NULL    |                |
| court_id | int(11) | NO   |     | 1       |                |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

此時可以看到,user_id位於id後面了。

2.修改欄位的名稱型別

ALTER命令中使用 MODIFY 或 CHANGE 子句能滿足以上需求。

mysql> alter table user modify court_id char(16);
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11)  | YES  |     | NULL    |                |
| court_id | char(16) | YES  |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

change的語法比較奇葩。請看下面的例子

mysql> alter table user change court_id court_id int;
Query OK, 0 rows affected (0.24 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11) | YES  |     | NULL    |                |
| court_id | int(11) | YES  |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

在 CHANGE 關鍵字之後,緊跟著的是你要修改的欄位名,必須要指定新欄位名及型別,即使欄位名不變!

3.修改欄位的預設值

為欄位新增預設值

mysql> alter table user alter court_id set default 1;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11) | YES  |     | NULL    |                |
| court_id | int(11) | YES  |     | 1       |                |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

刪除某個欄位的預設值

mysql> alter table user alter court_id drop default;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11) | YES  |     | NULL    |                |
| court_id | int(11) | YES  |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

4.修改表名

可以在 ALTER TABLE 語句中使用 RENAME 子句來實現修改表名的目的。

mysql> alter table user rename to user_test;
Query OK, 0 rows affected (0.05 sec)

5.修改表的引擎

可以在ALTER TABLE中設定ENGINE屬性,得到改變資料表引擎的目的。

mysql> alter table user_test ENGINE = MYISAM;
Query OK, 0 rows affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show table status like 'user_test'\G
*************************** 1. row ***************************
           Name: user_test
         Engine: MyISAM
        Version: 10
     Row_format: Fixed
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 3659174697238527
   Index_length: 1024
      Data_free: 0
 Auto_increment: 1
    Create_time: 2018-06-05 15:36:17
    Update_time: 2018-06-05 15:36:17
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

相關推薦

Mysql修改修改操作

當我們需要針對mysql的表名或者欄位名需要修改的時候,這個時候就需要用到mysql的alter命令。 為了方便起見,首先我們新建一張使用者表。 create table user( `id` INT NOT NULL AUTO_INCREMENT, `

mysqlMySQL新增修改

MySQL新增欄位的方法並不複雜,下面將為您詳細介紹MYSQL新增欄位和修改欄位等操作的實現方法,希望對您學習MySQL新增欄位方面會有所幫助。 1新增表字段 alter table table1 add transactor varchar(10) not Null

MySQL新增修改

1新增表字段 alter table table1 add transactor varchar(10) not Null; alter table table1 add id int unsigned not Null auto_increment primary ke

MySQL新增修改的方法

1、登入資料庫     >mysql -u root -p 資料庫名稱   2、查詢所有資料表     >show tables;   3、查詢表的欄位資訊     >desc 表名稱;   4.1修改表名     alter table table1 r

SQL語句增加修改修改型別、修改預設值

一、修改欄位預設值 alter table 表名 drop constraint 約束名字   ------說明:刪除表的欄位的原有約束 alter table 表名 add constraint 約束名字 DEFAULT 預設值 for 欄位名稱 -------說明

MySQL(1) 基本操作MySQL的啟動,的建立,查詢表的結構和修改

MySQL啟動流程 1 啟動伺服器   2 使用者名稱登入到MySQL資料庫中    3  檢視有哪些資料庫   4 使用其中的資料庫    5 檢視該資料庫中已有哪些表,沒有就新建 mysql

mysql中新加修改

新增欄位 在某個欄位之後新加一個欄位: ALTER TABEL xxx ADD column_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT '' AFTER xxx; 修改欄位 ALTER TABEL xxx MODIFY co

SQL 操作結構(建立,刪除修改

新增欄位: ALTER TABLE [表名] ADD [欄位名] NVARCHAR (50) NULL 刪除欄位: ALTER TABLE [表名] DROP COLUMN [欄位名] 修改欄位: ALTER TABLE [表名] ALTER COLUMN [欄位名] N

Mysql 資料庫 字符集修改

修改資料庫字符集 alter database owl default character set utf8; 修改表字符集 alter table t_app character set utf8; 修改單個欄位字符集 alter table t_app modify a

mysql修改新增(alter table 名 add column 或者 modify column)且帶unique時提示duplicate entry for key的原因以及解決方案

今天在公司臨時維護一張表時,我作了一個小動作,新增一個欄位,並且設定為unique時,盡然無法新增欄位,我當時就納悶了,寫了這麼多sql,這麼奇怪的問題還是第一次見,不多說,直接看圖 【我的sql檔案如下】 【執行sql語句報錯:alter table smart_

MySQL在資料修改的排列位置

建立資料表的時候,欄位在表中的排列位置就已經確定了。不過,使用ALTER TABLE語句可以改變欄位在表中的排列位置。 在MySQL中,修改欄位排列位置的基本語法格式如下: ALTER TABLE 表名 MODIFY 欄位名1 資料型別 FIRST | AFTER 欄位名2

mysql資料庫的簡單增刪改查,合併,拼接字元操作,用java完成將一張中的查詢結果合併存入另一張的指定

首先問題描述:我現在有兩個表,一個表是關鍵詞,一個表是含有關鍵詞的標籤,需要做的就是在關鍵詞表中新建一個標籤欄位,把包含該關鍵詞的全部標籤存入其中。比如關鍵詞是Java,標籤可能有Java開發,Java後臺等。我這裡關鍵詞有4000個,標籤有40000個,我用了小段java程式碼+sql的函式就完成

資料庫工作筆記010---Mysql中用SQL增加、刪除修改名、型別、註釋,調整順序總結

  JAVA技術交流QQ群:170933152   Mysql中用SQL增加、刪除欄位,修改欄位名、欄位型別、註釋,調整欄位順序總結   在網站重構中,通常會進行資料結構的修改,所以新增,刪除,增加mysql表的欄位是難免的,有時為了方便,還會增加

flask-連線資料庫flask_aqlalchemy-建-增加-刪除-修改

安裝模組 pip install pymysql pip install flask_aqlalchemy flaskpei配置檔案setting.py 中配置資料庫 設定資料庫url #coding=utf-8 class DataBaseSetting: DEBUG=Tr

Mysql 修改預設值

環境描述: MySQL 5.7.13 問題描述: 建表的時候,users_info表的role_id欄位沒有預設值,後期發現註冊的時候,需要提供給使用者一個預設角色,也就是給role_id欄位一個預設值。 當前users_info表的role_id 在建立表的時候,

Mysql 升級到5.7修改報錯 datetime 型別報錯

原因是Mysql5.7以上版本不支援無效日期時間欄位,例如 0000-00-00 00:00:00; 解決方法有兩種: 1:把date 或 datetime 型別值為 0000-00-00 的欄位,改成 1970-01-01 2:修改欄位預設值為空 程式碼示例: ALTER TA

mysql 中modify和change區別(以及使用modify修改名稱報錯)

使用modify修改欄位報錯如下: mysql> alter table student modify name sname char(16);ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

mysql修改長度(sql命令)

alter table news  modify column title varchar(130); alter table 表名 modify column 欄位名 型別; 如:news 表裡的title  欄位 原來長度是 100個字元,現長度要改成130個字元

記錄:Mysql 修改長度、修改列名、新增列、修改自增主鍵起始值

以下轉自https://www.cnblogs.com/yangjinwang/p/5918906.html alter table 表名 modify column 欄位名 型別; 例如 資料庫中user表 name欄位是varchar(30) 可以用 alter tab

批量修改mysql 表格編碼

SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER