Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add column before existing column?

Tags:

mysql

I have knowledge on sql server but now i worked on MySQL because of my company requirements. Is it possible to add a column before a specified column.

Actually i tried this logic

alter table marks add telugu int after typeofexam;

it works.when I'm trying to do the below logic

alter table marks add telugu int before typeofexam;

where marks is a table name, telugu a column name and typeofexam is a column name, it shows syntax errors.

Will you help me to get the logic.

like image 897
Ajay Avatar asked Jul 17 '13 09:07

Ajay


People also ask

How do I add a column after a specific column?

The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ]; table_name.

How do I change the order of columns in SQL?

Using SQL Server Management StudioIn Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.


2 Answers

Use the FIRST keyword:

alter table marks add telugu int FIRST;

Documentation: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

The order isn't a big deal anyway; it's more of a human thing than a requirement. Most front ends will allow you to re order easily though.

like image 136
exussum Avatar answered Sep 28 '22 11:09

exussum


Actually ordering of column does not matter in a database, you can always create a view which will display in the ordering of column you want.

Here is a nice blog which talks about them for SQL Server but the same is valid for MySQL as well. http://blog.sqlauthority.com/2008/04/08/sql-server-change-order-of-column-in-database-tables/

like image 26
AurA Avatar answered Sep 28 '22 12:09

AurA