Will MySQL ALTER TABLE reformat field data? - mysql

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

Related

Change MySQL numerical field to char using a function

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.

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 to change bigint to int in mysql, with table having foreign keys

I recently noticed that there are several bigint type fields in one of my databases that could be replaced with type "int". The problem is that the database is already online and in use and also that there are foreign keys in action, so when I try changing the data type in the offline DB, mysql won't let me and produces the error message: "#1025 - Error on rename of (...)".
So how can I change these fields while keeping the foreign keys happy? (and not wiping out the existing DB!)
You can alter a column from BIGINT to INT as long as there is not any data in the column that would not fit into the smaller INT datatype.
ALTER TABLE mytable
MODIFY COLUMN mycolumn INT(11);
You could try:
Add new columns with proper types.
Copy values from old columns to new columns.
Rename columns.
Update constrains (maybe in two steps -- DROP and CREATE).
Drop old columns.
You can't reduce the size of field when it holds data. You can only expand them.
What you can do is export all the data, disable the keys, empty the tables, change the field sizes & import the data again.

How to alter MySQL table without losing data?

In my application, I make some changes and upload them to a testing server. Because I have no access to the server database I run ALTER commands to make changes on it.
Using a method I ran the following command on server:
ALTER TABLE `blahblahtable` ADD COLUMN `newcolumn` INT(12) NOT NULL
After that, I found that the all the data of the table has been removed. Now the table is blank.
So I need to alter the table without removing his data. Is there any way to do that?
Your question is quite obvious. You're adding a new column to the table, and setting it to NOT NULL.
To make things clearer, I will explain the reaction of the server when you run the command:
You add a new column, so every row of the table has to set a value for that column.
As you don't declare any default value, all the rows set null for this new column.
The server notices that the rows of the table have a null value on a column that doesn't allow nulls. This is illegal.
To solve the conflict, the invalid rows are deleted.
There are some good fixes for this issue:
Set a default value (recommended) for the column you're creating.
Create the column without the NOT NULL, set the appropiate values, and then make the column NOT NULL.
You can create a temp table, pass all the information from the table you want to alter, and then return the info to the altered table.

How do you assign a column default in MySQL to another column's value?

I want to add a new column to a table in MySQL database that should get the value of another column in the same table. Is this possible? If so, how do you do it?
Starting with MySQL 5.0.2 you can write a stored procedure linked to a TRIGGER which can examine the new column each time a row is inserted, and automatically copy the other column's value into the new column if no value was supplied for the new column.
You Can use this following two command to do your work.
This command will be used to add new column in you table. Remember data type from where you want to copy to use those data type in new Column.
ALTER TABLE table_name ADD new_column_name VARCHAR(60);
2: This command to copy data from old column name to new column name.
UPDATE table_name SET new_column_name = old_column_name;
Then if you want to delete previous column, Then you can use following command
ALTER TABLE table_name DROP COLUMN old_column_name;
As of 20101212 mysql does not support defaulting 2 timestamp columns, which means you can't do a 'created' and 'updated' on the same table.
If this is what you were trying to do, then the trigger with the stored proc is the way to go.
create a view, and you can select the same column twice and give it different name, then the application can use the view instead use the table directly.