MySQL ALTER TABLE sort columns alphabetically - mysql

I am maintaining a legacy application with MySQL database with most of the tables having 20+ columns and few have 100+. To make the it friendlier I am trying to alter all the tables to arrange all the columns sorted alphabetically.
What would be the appropriate ALTER TABLE queries ?

There is no way for changing column order in a mysql table. However, you can create a new table with the columns in your order, using for example:
CREATE TABLE newtable SELECT a,b,c,d,e,f,g FROM old_table_name
This way, your newly created table will have columns in your defined order, and you can drop the old table and rename the newtable to old name.
In order to create the above mentioned query, you just need to get column names from your old table and sort them, to do that programatically you can use something like this:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='database_name' AND TABLE_NAME = 'old_table_name'
ORDER BY column_name

Related

Create a table and specify data types according to an existing query

Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.

arrange columns of multiple tables mysql

I have a database with hundreds of the same tables however not all tables have the same column order. I don't know which tables and I don't know what order they are in, only that the columns are the same for every table. Is there a way to rearrange all columns in a table? I know with the Alter statement you can rearrange one column but I would like to to all columns at once for a table.
EDIT:
I would need to do this for all tables using the table_schema and then create a query for all. So exporting the database and changing the create table isn't an option since I would need to change it for all tables.
Thanks to CBroe here is a solution:
make an export of only the data of the database and create a new database.
Then run this query in the information_schema:
SELECT CONCAT("Create Table `db_new`.`", TABLE_NAME, "` LIKE `db_old`.`default` ;") as MySQLCMD FROM TABLES where TABLE_SCHEMA = 'db_old'
Then execute all these queries which are generated for you to create all the tables with the same structure (default table).
Then import the data in the new database.

Replace column names, table names and other structures using a query

I want to search the structure of my MySQL db and replace structures that contain or equal "xyz*"... column names and table names are the main thing, but I'd like to search all structures.
Then, I want to replace "xyz*" with "abcdefg".
I doubt one can do this with one query. What you can do is create ALTER statements. I would write a small program (in the language of your choice) and
first I would get the table names like this:
select table_name from information_schema.tables;
The result set has to be checked in a loop, if there is a table named "xyz*" then create an ALTER statement:
RENAME TABLE tbl_name TO new_tbl_name
For the colums, you would have to get each Table again, then all colums for each table:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tbl_name';
the again a loop to change the columns name, this time using ALTER TABLE.
see also:
https://dev.mysql.com/doc/refman/5.1/en/alter-table.html
https://dev.mysql.com/doc/refman/5.0/en/rename-table.html

how to add column to a mysql table by selecting columns from another table

I know how to create table based on columns of other mysql table
as
create table table_name as select column1,column2 from table2;
Now i want to know how to add column to a table by selecting columns from another table,
something like below
alter table table1 add column as select column from table2
Is that possible?
You can get a table's structure with the following query:
select *
from information_schema.table_name
Which you can then use the result to either create or alter another table.

Sql query - how to create column in table

I have in Msql database several tables with lots of rows and columns in them. I need to create a new column in one of these tables.
How do I create an SQL query for inserting a new column in a specific table
via PhpMyAdmin?
alter table table_name add column new_column int after id
In my example I have added a new column that has data type int after id column. Very easy.
Syntax:
ALTER TABLE tableName ADD columnName columnType;
Example:
ALTER TABLE people ADD hometown VARCHAR(50);
You can use this query in phpMyAdmin by going to the SQL tab after selecting your database.
Also, you may use the graphical user interface to add a column by following the steps:
Select the database on the left side of the screen
Select the table on the left side of the screen
Go to the "Structure" tab
Under the list of all existing columns, you have the option to add new fields (columns)
Simple google revealed this and this
ALTER TABLE contacts ADD email VARCHAR(60);
ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
Link: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
The command is ALTER TABLE, you can find the details here