>데이터 베이스 >MySQL 튜토리얼 >MySQL – Add a Column at Particular Ordinal Position in Tabl_MySQL

MySQL – Add a Column at Particular Ordinal Position in Tabl_MySQL

WBOY
WBOY원래의
2016-05-31 08:50:271200검색

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
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.