how to add a column in certain positon in table?

Hi Expert,

How I can add a cloumn to a exist table in certain position for instance,

Table 1

FirstName varchar(20), Lastname varchar(20).

I want add a column MiddleName varchar(20) in second column and all data can not be lost.

Thanks in advance.
Who is Participating?
DVation191Connect With a Mentor Commented:
ALTER TABLE table_name ADD column_name column-definition [ FIRST | AFTER col_name ]

Alter Table Stu_Table add Stu_Class int(10) AFTER Stu_Name
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
in short: this is not possible unless you recreate the table.
of course, you can rename the table, create new table with proper design, and insert back the data.
when you use the SQL server GUI, you can do that in the design mode, and the GUI will build all the SQL for you.

now, the position of the column should not matter, at least not with serious development, because the database, in the end, should be nothing else than data storage and eventually (part of) the data layer.
s_chilkuryConnect With a Mentor Commented:
Go to the design view of your table, then right click on
the position where you want the new table and select insert column.
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

EvilPostItConnect With a Mentor Commented:
As a side note, if you use the designer to change the ordinal position of the column. Behind the scenes it re-creates the table.

Best option is to follow what angelIII has suggested.
I think DVation191's comment applies to MySQL
dshi15Author Commented:
I need use sql statement, not design view. Could some one write sql statement? How to recreate?

You gonna need to post the table structure first.
Racim BOUDJAKDJIConnect With a Mentor Database Architect - Dba - Data ScientistCommented:
Two things to take into consideration:

> There is no obligation to have the same column order in the table as you'd have in a display.
> Appart from amount of space used and under the assumption of relevant indexing, order of columns is totally irrelevant in a database.  You can create *ANY* order you need in a view.

This is one of the typical examples where the finality does not worth the effort of reordering the columns...

Hope this helps
I Agree.
Anthony PerkinsCommented:
>>Could some one write sql statement? How to recreate?<<
If you persist and in case you missed the point, here it goes again:
Use SSMS to make the change but do not save instead copy the script generated and use that in your code.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.