首页 > 数据库 > mysql教程 > MySQL – Add a Column at Particular Ordinal Position in Tabl_MySQL

MySQL – Add a Column at Particular Ordinal Position in Tabl_MySQL

WBOY
发布: 2016-05-31 08:50:27
原创
1204 人浏览过

Position

bitsCN.com

Sometimes you may need to add a column at a particular ordinal position in the table. In SQL Server there is no option to do it via SQL although you can use Designer from SQL Server Management Studio. In MySQL you can do it as part ofALTER TABLE statement.

Let us consider the following table

CREATE TABLE sales(sales_id INT auto_increment KEY,item_id INT, sales_date DATETIME, sales_amount DECIMAL(12,2));<br>

Suppose you want to add another column named modified_date between item_id and sales_date, you can use the following command

ALTER TABLE sales ADD COLUMN modified_date DATETIME after item_id;

Now the new column named modified_date will be added after item_id. You can check this using the following command

Field		Type	Null	Key	Default	Extra--------------------------------------------------------------sales_id	int(11)	NO	PRI		auto_incrementitem_id	int(11)	YES			<strong>modified_date	datetime	YES</strong>			sales_date	datetime	YES			sales_amount	decimal(12,2)	YES
登录后复制

This is sometimes useful. In relational database the order of column in a table should not matter. The database design and modeling should be such that there should not be any dependency on the order the columns are createdinsideMySQL table. This is the reason, in SQL Server we can’t do this operation via T-SQL and when we do with the help of SSMS, it practically creates and drop the entire table in the background – (a very bad thing).

Reference:Pinal Dave (http://blog.sqlauthority.com)

bitsCN.com
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板