0

I have two database tables: TABLE 1 and TABLE 2, both have the same number of columns and have the same columns, the problem is that TABLE 2 has a different order of it columns, So I want to arrange the columns of TABLE 2 like they are arranged in TABL1, but i Dont know how to do that.

I'll be very grateful if you can help me.

  • See this: https://stackoverflow.com/questions/34818/sql-server-does-column-order-matter. If you want to reorder the columns you'll have to rebuild the table. I also recommend going to https://dba.stackexchange.com/ for db questions. – poodle Jun 29 '18 at 04:05
  • 3
    You have to drop the table and recreate it with the columns in the correct order. It's important to note that the order of columns in a table only matters to the database engine itself, not sql, or any client applications. If you want to reorder the columns, why not make a view with the columns in the right order? – David Buttrick Jun 29 '18 at 04:09
  • For SQL Server, you can re-arrange your column order in design mode on SQL Server Management Studio. – Abdul Rasheed Jun 29 '18 at 05:32
  • Which [DBMS product](https://en.wikipedia.org/wiki/DBMS) are you using? "SQL" is just a query language, not the name of a specific database product. Please add a [tag](https://stackoverflow.com/help/tagging) for the database product you are using `postgresql`, `oracle`, `sql-server`, `db2`, ... –  Jun 29 '18 at 06:40

1 Answers1

0
  1. Re-create Table2 as create table Table2_Temp as select <sequence of fields like Table1> from Table2. After that re-name tables: Table2 -> Table2_Old (or just drop this one), Table2_Temp -> Table2. This is the simplest way and can be realized in any version of DB.

  2. Change order of columns using non-standard SQL language facilities but if it's possible in using a type of DB.

Meow Meow
  • 637
  • 6
  • 17