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
Related
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.
I want to remove all spaces in front and after the entries in all my columns in all my tables. For one column I use UPDATE table_name SET column_name= LTRIM(RTRIM(column_name)) which works well. However, I would have to copy and paste all the names in order to do this for all columns and tables.
Is there a simpler way, e.g. by looping trough somehow? A loop for one table would be very useful already!
Cheers!
You can create a dynamic query wich will loop against INFORMATION_SCHEMA.COLUMNS table and construct your final string wich will updates all the columns.
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name';
will gives you the name of all columns. Then you can filter on column type or whatever you want.
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
I want to create a new table with rows of the column names. Essentially, I want to put the output of a decribe statement into a new table, so that I can use a where clause to only extract certain column names. I'm using sparksql.
How do I do this? Thanks.
No need to create an additional table just use either SHOW COLUMNS command instead of describe with filter, or query information_schema.columns table with a select.
I want to alter multiple tables using regex,
ALTER tablename DROP INDEX feed
the following are the table names
wp_1_ai1ec_event_feeds
wp_12_ai1ec_event_feeds
wp_14_ai1ec_event_feeds
wp_19_ai1ec_event_feeds
and so on..
I tried using regex in place of tablename
alter wp_[0-9]+_ai1ec_event_feeds drop index feed
but I get syntax error. How to achieve this?
You can fetch the table names using SHOW TABLES, then do the ALTER queries for each of these tables.
To fetch the table names, you can use the simple LIKE matching:
SHOW TABLES LIKE 'wp\_%\_ai1ec\_event\_feeds';
This will work as long as there are no tables like wp_letters_ai1ec_event_feeds.