phpMyAdmin - can I export an "alter table" query? - mysql

I add a few fields in a table and want to quickly modify the same table in different database - so I want phpMyAdmin to generate the alter table query for selected fields. Is this possible?

You can play with information_schema.
select concat('alter table ',table_schema,'.',table_name, '.....;')
from information_schema.tables
where table_name = 'your_table_name'
Then you can run all the queries.

I don't think this is possible with phpmyadmin, But you can use a tool like heidisql(windows desktop application) where it displays all the sql it runs when you do changes using the gui. You can copy this sql and run it in other dbs.

Related

Run an ALTER TABLE across all like tables (containing the same schema) in MySQL database

I have a database that contains a table per client, each table has the same columns in them. We're talking a few thousand client tables.
I need to add new columns to each of these tables for new development but cannot find a way to recurse all the tables in the database to add the columns. I know MS SQL has something sp_MSforeachtable which does maybe what I'm asking but I don't know if MySQL has anything similar?
As per this previous answer in SO you could do something like this :
select concat('ALTER TABLE `',table_name,'` ADD `test` INT NOT NULL AFTER `column_x`;')
from information_schema.tables
where table_schema = 'your_db_name'
Then remove the few tables not about client (here's hoping client is the only entity to have "private table") or if possible add a condition on the table_name (like AND table_name LIKE "client_%"), and execute the whole batch.
The 2nd solution, to use procedure, is too complex (for me) for this use case, but maybe someone more skilled than me in PLSQL won't agree.

is it possible to update the information_schema table in order to add leading text to column names?

I am trying to execute this SQL as root:
UPDATE information_schema.COLUMNS SET COLUMN_NAME = CONCAT('emp',COLUMN_NAME) WHERE TABLE_SCHEMA = 'edsdb'
But get the following error:
SQL Error [1044] [42000]: Access denied for user 'root'#'localhost' to database 'information_schema'
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user 'root'#'localhost' to database 'information_schema'
These tables that I need to 'fix' the column names off have a lot of columns in them. Is it possible to do this directly in SQL?
As per the MySQL documentation (here), information_schema contains read-only tables which are views so you can't change it:
The INFORMATION_SCHEMA database contains several read-only tables.
They are actually views, not base tables, so there are no files
associated with them, and you cannot set triggers on them. Also, there
is no database directory with that name.
You need to use ALTER TABLE command instead, e.g.:
ALTER TABLE `edsdb` CHANGE `id` `empid` INT;
is it possible to update the information_schema table in order to add leading text to column names?
DO NOT TRY THIS. Instead, update your tables to rename the columns you need to rename.
There are versions of MySQL that correctly prevent this.
There are older versions that don't. If you manage to update the system tables (actually the ones in the mysql schema) in one of those versions, you'll corrupt your MySQL server.
Please don't ask how I know this. :-)
No, you can't edit the system table or view. Those are read only. But you don't need to update as well, you can just make a SELECT query and display the same
SELECT CONCAT('emp',COLUMN_NAME) as new_column_name
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'edsdb'
Probably, you can consider creating a VIEW with the above SELECT statement for future

Create table on every database

The way this database is setup (not my design) is that every user has their own database with many tables, so if a user ever decides to leave us we can just give them their DB and they have all of their data. The issue is, when I create a new form or feature I need to create a table in each one of these databases (50+) and it takes a huge amount of my time. Is there any way to create a table in each database?
Run the following query, then execute the output.
If the table already exists in the schema it will still run. However, if the table already exists and you want to redefine it, you can tune this statement to drop those table and recreate them.
SELECT DISTINCT CONCAT('CREATE TABLE IF NOT EXISTS ', TABLE_SCHEMA, '.define table here')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA NOT IN
('mysql', 'information_schema', 'performance_schema');
You could do it programmatically using a programming language like PHP or Python.
Here's the pseudocode that would need to be implemented.
get array of user databases by selecting from information_schema
for each database:
SQL : "create table if not exists <database>.tableName ..."
end for each

search simple way to alter multi tables in one time in mysql

i have a lot of tables that start with some prefix ,
and i want to alter this tables
what is the simple way to do this (instead run over all tables)
i mean something like :
ALTER TABLE LIKE tablenameprefix% ADD INDEX `NewIndex1` (`field`);
how can i do this ?
thanks
EDIT :
can i do a kind of loop not in stored procedure ?
by select the names of tables from
SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE 'tableprefix%'
You can't. What you could do though is write a stored procedure that enumerates all tables looking for your prefix and performs the necessary changes.
Given that ALTER TABLE syntax doesn't allow multiple table names, you cannot do this. You need to go through all tables in turn:
ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification]
Link: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
When I wanted to change multiple table's engine from MyISAM to InnoDB, instead of writing a loop I just made a full DB dump and opened it in a text editor. In the text editor I just simply changed all MyISAM words to InnoDB.
I know that this ain't proper solution but for me it was easier then writing a routine for this.
You would have to write a loop, according to the documentation you just specify the table name.
I made a stupid mistake of putting all Wordpress tables with my product tables, fortunately all Wordpress tables start with a wp_ prefix and all my other product tables have no this wp_ prefix.
I created another database named wordpress, now I want to move all tables start with wp_ to that database.
Here is what I did:
SELECT CONCAT('ALTER TABLE olddb.', table_name, ' RENAME wordpress.', table_name, ';')
FROM information_schema.tables
WHERE table_schema='olddb' AND table_name LIKE 'wp%'
INTO OUTFILE '/tmp/move_to_wordpress';
SOURCE /tmp/move_to_wordpress;
That's it.

Generate table DDL via query on MySQL and SQL Server

Is there an easy way to extract table DDL information, via a query, using either Ms or My SQL server? (preferably both?)
For example, using MySQL Administrator / Navicat for MySql, there is a "DDL" function, which generates the "create table foo (....)" script.
Is there any way to get this information from a query itself, such as:
Select DDL from foo where table_name='bar';
Any have the "Create table bar (.....)" returned to me?
If not - any suggestions?
it's mysql-specific, but SHOW CREATE TABLE <table-name> gives you the DDL for a table.
You have to create that yourself.
You can query INFORMATION_SCHEMA.COLUMNS for the column name and data type.
You can't get the CREATE Table text in a cross platform way, but you can get enough information to build it yourself from the INFORMATION_SCHEMA views.