MySQL向表中添加列方法實例
我們使用alter table add column語句向現(xiàn)有表中添加新列。
簡介
alter table table_name add [column] column_name column_definition [first|after existing_column];
說明:
alter table子句后指定表名;column關鍵字是可選的,可以省略它;- 可以通過
first關鍵字將新列添加為表的第一列,也可以使用after existing_column子句在現(xiàn)有列之后添加新列,如果沒有明確指定會將其添加為最后一列;
若要向表中添加兩個或更多列,使用下面語法:
alter table table_name add [column] column_name column_definition [first|after existing_column], add [column] column_name column_definition [first|after existing_column], ...;
舉例
創(chuàng)建一個表
create database test; use test; create table if not exists vendor ( id int auto_increment primary key, name varchar(255) );
添加新列并指定位置
alter table vendor add column phone varchar(15) after name;
添加新列但不指定新列位置
alter table vendor add column vendor_group int not null;
插入記錄
insert into vendor(name, phone, vendor_group)
values('IBM', '(408)-298-2987', 1);
insert into vendor(name, phone, vendor_group)
values('Microsoft', '(408)-298-2988', 1);同時添加兩列
alter table vendor add column email varchar(100) not null, add column hourly_rate decimal(10, 2) not null;
注意:email和hourly_rate兩列都是not null,但是vendor表已經有數(shù)據(jù)了,在這種情況下,MySQL將使用這些新列的默認值。
檢查vendor表中的數(shù)據(jù)
select id, name, phone, vendor_group, email, hourly_rate from vendor;
查詢結果:
+----+-----------+----------------+--------------+-------+-------------+ | id | name | phone | vendor_group | email | hourly_rate | +----+-----------+----------------+--------------+-------+-------------+ | 1 | IBM | (408)-298-2987 | 1 | | 0.00 | | 2 | Microsoft | (408)-298-2988 | 1 | | 0.00 | +----+-----------+----------------+--------------+-------+-------------+ 2 rows in set (0.00 sec)
email列中填充了空值,而不是NULL值,hourly_rate列填充了0.00
添加表中已存在的列
MySQL將發(fā)生錯誤
alter table vendor add column vendor_group int not null;
操作結果:
ERROR 1060 (42S21): Duplicate column name 'vendor_group'
檢查表中是否已存在列
對于幾列的表,很容易看到哪些列已經存在,如果有一個飲食數(shù)百列的大表,那就比較費勁了
select if(count(*) = 1, 'Exist', 'Not Exist') as result from information_schema.columns where table_schema = 'test' and table_name = 'vendor' and column_name = 'phone';
查詢結果:
+--------+ | result | +--------+ | Exist | +--------+ 1 row in set (0.00 sec)
在where子句中,我們傳遞了三個參數(shù):表模式或數(shù)據(jù)庫,表名和列名。我們使用if函數(shù)來返回列是否存在。
在表中已有字段后添加列名
alter table 表名 add column 列名 類型 after 已有列名 comment '注釋信息'; alter table students add column class varchar(40) after student_name comment '學生班級';
刪除列
alter table 表名 drop column 列名; alter table students drop column grade;
參考
https://www.begtut.com/mysql/mysql-add-column.html
總結
到此這篇關于MySQL向表中添加列的文章就介紹到這了,更多相關MySQL表中添加列內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MySQL8.0報錯Public?Key?Retrieval?is?not?allowed的原因及解決方法
這篇文章主要給大家介紹了MySQL8.0報錯Public?Key?Retrieval?is?not?allowed的原因及解決方法,文中通過代碼示例和圖文介紹的非常詳細,有遇到相同問題的朋友可以參考閱讀一下2024-01-01
mysql error 1130 hy000:Host''localhost''解決方案
本文將詳細提供mysql error 1130 hy000:Host'localhost'解決方案,需要的朋友可以參考下2012-11-11
MySQL對JSON類型字段數(shù)據(jù)進行提取和查詢的實現(xiàn)
本文主要介紹了MySQL對JSON類型字段數(shù)據(jù)進行提取和查詢的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
MySQL數(shù)據(jù)庫設計之利用Python操作Schema方法詳解
這篇文章主要介紹了MySQL數(shù)據(jù)庫設計之利用Python操作Schema方法詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。2017-11-11
親手教你怎樣創(chuàng)建一個簡單的mysql數(shù)據(jù)庫
數(shù)據(jù)庫是存放數(shù)據(jù)的“倉庫”,維基百科對此形象地描述為“電子化文件柜”,這篇文章主要介紹了親手教你怎樣創(chuàng)建一個簡單的mysql數(shù)據(jù)庫,需要的朋友可以參考下2022-11-11
MySQL存儲引擎中MyISAM和InnoDB區(qū)別詳解
存儲引擎說白了就是如何存儲數(shù)據(jù)、如何為存儲的數(shù)據(jù)建立索引和如何更新、查詢數(shù)據(jù)等技術的實現(xiàn)方法。因為在關系數(shù)據(jù)庫中數(shù)據(jù)的存儲是以表的形式存儲的,所以存儲引擎也可以稱為表類型(即存儲和操作此表的類型)2016-12-12

