mysql command to exchange columns - mysql

I ran into a problem where one version of my database has a column that is in the wrong position. Is there a way in mysql to re-index or exchange columns?

ALTER TABLE tbl CHANGE old_col new_col integer AFTER sec_col

Related

MySQL: how to find tables with column(s) that has Extra on update current_timestamp()?

I am working on MariaDB V10.6, and just realized the following extra feature has been automatically added to a timestamp column in a table that I am working on.
on update current_timestamp()
With some help from Google, I was able to remove this unwanted extra from this table.
My question is, is there a way I can find all the table in the database that has this extra and remove them if they are not desired? Some sort of query that I can run against schema?
Thanks.
This is documented behaviour https://mariadb.com/kb/en/timestamp/ ,to find
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'YOURSCHEMA' AND EXTRA ='on update current_timestamp()';
To modify
alter table T modify column yourcolumn timestamp default 0;
But do you really want such a default? And is timestamp a suitable datatype for your purposes?
https://dbfiddle.uk/lnPEBnXq

how to modify a column datatype in mysql so that it saves the previous records in the newly saved format

I just need to change the datatype of my column
however, I used the command
alter *table_name* modify column *column_name* *datatype* and it update the datatype but the previously saved result are in the same previous datatype . I want them to be modified too. Any help? And am I clear with my query?
It's been a while and I've been bouncing between database engines a lot lately so my syntax may be off, but the general idea will still be valid:
SELECT * INTO <table_name>_bak FROM <table_name>;
-- alter your table, leaving nulls in the column
UPDATE <table_name>
SET <column_name> = b.<column_name>
FROM <table_name> t
INNER JOIN <table_name>_bak ON <primary-key-join-clause-here>;
-- disable nulls in your modified column if needed
DROP TABLE <table_name>_bak;

mysql 5.6 alter table drop column if exists

i'm asking this question after googling with no luck. it has been asked before on SOF but that's long ago MySQL Alter syntax to drop a column if it exists. Hope things have changed since.
Is there a straight forward way to drop a column in table if it exists.
I'm using MySql 5.6, i would like to wrap this alter statement around an if to avoid any issue if the script runs twice:
ALTER TABLE xyz
DROP COLUMN abc,
ADD COLUMN ghi DATE NOT NULL AFTER column4;
Thanks
You may check if column exists and after perform the other operations.
See here: MySQL, Check if a column exists in a table with SQL

How long does it take to create a large table in MYSQL?

When I imported a table with 30 million rows from a text file to a MYSQL table it only took 1 minute. However, I realized that I missed a column and that I needed to add it to the table. From the MYSQL command line, I wrote the following command:
create tableC as(tableA.T1, tableB.ZID from tableA, table B where A.ZID = B.ZID)
It's been over one hour and the command has not terminated. Does anyone know the reason why? TableB was already in the MYSQL server.
Not 100% sure but you might be better off altering the table first and adding the column, then doing an update to populate that column
ALTER TABLE tableB ADD COLUMN colA yourColumnDefinition;
UPDATE tableB SET colA = <however you do it>;
I'm not totally sure which table you are adding the column to or how you are computing it. Based on the query you posted it looks like you are creating a mapping table to map two table's IDs to each other. If that is the case you would probably be better off putting a foreign key in one of the tables.
Again, this might not be exactly what you are looking for but if you just want to add a column to a table you already created this might be a better approach.

How can I modify an existing column's data type?

One of the columns in a somewhat large table (~10,000 records) is of the data type DECIMAL(10,0). I'm using MySQL.
I'd like the values to be displayed to 2 decimal places, so I need to alter this to DECIMAL(10,2), without screwing up the table's existing records. How could this be done?
Which DBMS are you using ? you can try like this for MySQL :
alter table tblName modify columnName newDataType;