Change MySQL numerical field to char using a function - mysql

I have a table in MySQL DB with a field for storing IP address. It stores it as a number (int). I'd like to change the structure to use string (char) instead.
Is it possible with single ALTER TABLE statement? As far as I can see in Postgres it is possible to specify a conversion function to ALTER TABLE, but I don't see an equivalent for MySQL.
A workaround is of course to:
alter table by adding a new char() field
update that new field from the old one using inet_ntoa
alter table by removing old field
and finally renaming new field to the same name as the old one had
But maybe there is a simpler solution?

You can simply alter the column to varchar. mysql will automatically convert the values. I can confirm this with mysql 5.6.
but before you do this, you should definetely create a backup and maybe try this with a small test table.

Related

Error when adding column with numeric name

I currently have a MariaDB database with columns named after dates : 20200105, 20200914 etc.
If I try to add a column using ALTER TABLE dates ADD COLUMN IF NOT EXISTS (test VARCHAR(255));, it works and the test column is created.
If I type ALTER TABLE dates ADD COLUMN IF NOT EXISTS (20201205 VARCHAR(255));, though (so, with a number replacing "test"), the creation does not work anymore and MariaDB tells me that there is an error with my SQL syntax.
I have tried to put quotes around the column name, but that does not work (not even with "test").
Is there something obvious I am missing ?
Use backticks to escape the column name:
ALTER TABLE dates ADD COLUMN IF NOT EXISTS (`20201205` VARCHAR(255));
But really best practice frowns upon the use of naming your database objects with mandatory backticks. The reason for using a name like 20201205 as a column name is that you will forever be needing to escape it using backticks. Also, from a data design point of view, your data should grow with new dates in terms of increasing the number of records, not columns.

Migrating Varchar to Text in Mysql

I am converting one of the "Varchar" column in mysql table to a text field. I am simply using single alter command to convert the type. When i read about text and varchar came to know that it had difference in storing mechanisms.
Am I supposed to write any migration script to change the type of column and move data or a single type altering command is enough ?
ALTER TABLE table_name MODIFY column_name TEXT NOT NULL;
There is no a big problem to change varchar to text because text supports more data length than varchar, but if the field has a index it must be drop and create new index with prefix col_name(length) (see CREATE INDEX syntax).
According to your data content maybe would be a good idea use fulltext indexes but that implies change your search expressions on that field.
If you are in production environment, the table will be locked meanwhile the migration is in progress to prevent data loss.

How do I change the data type for all columns in MySQL?

I want to change the datatype for all columns in my table mysql.
For instance varchar to double.
alter table myTable alter column vColumn int;
This will work as long as:
-all of the data will fit inside an int
-all of the data can be converted to int (i.e. a value of "car" will fail)
-there are no indexes that include vColumn. If there are indexes, you will need to include a drop and create for them to get back to where you were.
Changing of column types is possible with SQL command ALTER TABLE MODIFY COLUMN (it does not work in every DBMS, however).
Usually you have to remove data anyway, so another option would be to DROP TABLE (=remove it entirely) and create anew with desired columns (with CREATE TABLE). You could also remove just the single column (ALTER TABLE DROP COLUMN) and add a new one (ALTER TABLE NEW COLUMN) with the new definition.
Of course changing a column is so simple only as long as this column is not used in any constraints or keys
For syntax of the above commands see MySQL docs

How do you change an autoincremented columns starting value through liquibase?

I am using MySql for my database. I have found how to set a column's starting autoincrement value when creating a table, but I need to know how to set a new starting value for an existing column. What does the liquibase script look like to do that?
The MySQL syntax is pretty straightforward:
ALTER TABLE mytable AUTO_INCREMENT = val ;
(Note that this is really a table attribute, not a column attribute. There can be only one column in a table declared to be AUTO_INCREMENT.)
This syntax isn't supported in SQL Server or Oracle; Oracle doesn't even have a concept of an "auto_increment" column, apart from a SEQUENCE object and a TRIGGER. SQL Server calls it an IDENTITY property. So I don't know how this statement would be represented in "liquibase" syntax, other than specifying that this statement is native MySQL syntax.
You can use addAutoIncrement (http://www.liquibase.org/documentation/changes/add_auto_increment.html) to change your existing AUTO_INCREMENT column.
Don't forget to specify columnDataType in the addAutoIncrement.
I used this yesterday for our project and it worked (for MySQL).

Will MySQL ALTER TABLE reformat field data?

I need to alter a table to change the Type of a column. If the current column Type is DECIMAL(8,2) and I alter that column to be DOUBLE, will the already-existing data in the column also be changed to reflect the column's new Type?
It should. However, always be safe when altering existing tables.
My preferred way of doing this type of operation is:
make a backup
create a new column
update all rows moving the old data over to new column
inspect the new column for anything wrong and fix if necessary
delete the old column