I have a CHAR type column in table A, and all the strings in the column correspond one-to-one to integers in table B. What I want is to replace all of the strings in the CHAR column of table A with their corresponding INTs from table B. I know I could write a script that makes a new column and populates it with the relevant data, but it seems like there should be an easier way to do it from within MySQL. Is there a way to do this with a UPDATE or ALTER TABLE statement?
Well, simply UPDATING the column with the int value will work:
UPDATE table1
SET table1.column = table2.column
WHERE <condition>
but the column will still be a CHAR column, so afterwards you still have to do an ALTER TABLE to convert the column into an INT.
Though this should not make a problem with casting I would prefer to add a new INT column, fill it with an UPDATE similar to the one above and then remove the old column - always assuming the table is not that big that the repeated ALTER TABLE locks for too long.
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 have a table full of traffic accident data with column headers such as 'Vehicle_Manoeuvre' which contains integers for example 13 represents the vehicle manoeuvre which caused the accident was 'overtaking moving vehicle'.
I know the mappings from integers to text as I have a (quite large) excel file with this data.
An example of what I want to know is percentage of the accidents involved this type of manoeuvre but I don't want to have to open the excel file and find the mappings of integers to text every time I write a query.
I could manually change the integers of all the columns (write query with all the possible mappings of each column, add them as new column, then delete the orginial columns) but this sould take a long time.
Is it possible to create some type of variable (like an array with first column as integers and second column with the mapped text) that SQL could use to understand how text relates to the integers allowing me to write a query below:
SELECT COUNT(Vehicle_Manoeuvre) FROM traffictable WHERE Vehicle_Manoeuvre='overtaking moving vehicle';
rather than:
SELECT COUNT(Vehicle_Manoeuvre) FROM traffictable WHERE Vehicle_Manoeuvre=13;
even though the data in the table is still in integer form?
You would do this with a Maneeuvres reference table:
create table Manoeuvres (
ManoeuvreId int primary key,
Name varchar(255) unique
);
insert into Manoeuvres(ManoeuvreId, Name)
values (13, 'Overtaking');
You might even have such a table already, if you know that 13 has a special meaning.
Then use a join:
SELECT COUNT(*)
FROM traffictable tt JOIN
Manoeuvres m
ON tt.Vehicle_Manoeuvre = m.ManoeuvreId
WHERE m.name = 'Overtaking';
I currently have a database that uses a calculated value for sorting. The calculation is cached in a string column, but the value is guaranteed to be a natural number. I'm trying to change the column's data type to speed up sorting, but I can't think of a quick solution.
My current plan is something along the lines of:
ALTER TABLE
items
ADD COLUMN isort INT;
followed by:
UPDATE
items
SET
isort = CAST(sort AS DECIMAL(11, 0));
and finally:
ALTER TABLE
items
DROP COLUMN sort;
ALTER TABLE
items
RENAME COLUMN isort TO sort;
Am I approaching this the wrong way? Seems like a big fuss about a pretty straightforward change.
You can just change the column to a different type
ALTER TABLE table1 MODIFY col1 INTEGER NOT NULL;
I have a table with columns: [id, value] (id = integer PK, value= integer). I'd like to update the value column for a given id, retrieveing the old value. Is posible to do this in a single query? or I need to make a query and then an insert?
Thanks in advance
You could create a trigger that puts the old value in another table (or even into another column of the current table). Other than that, you'd need to do two queries.
I have a table in my db where I store records of user actions. Currently the column that contains user IDs is set to int(11), however i am making some changes to my code where I will be adding temporary user IDs.
To differentiate the temporary IDs from the regular ones, I prepend 0 to the id.
Example: 4 -- regular user; 023 -- temporary
However when I populate this ID into ym table the zero gets discarded. What field type do I need to change it to to keep all IDs in tact?
You could change it to an varchar if you want to prefix the id's with a 0
But you might want to try this.
Add a new column:
ALTER TABLE `your_table` ADD COLUMN `temp_id` INT(11) NULL AFTER `original_id`;
Then migrate your id's
UPDATE `your_table` SET temp_id = `original_id`;
I think you'll have to go with a varchar field but note that this will eliminate your auto_increment if you have one.
The user ID is an int and ints are binary numbers. A leading zero is the SAME as the number without a leading zero.
I would suggest negating the number to indicate a temporary id.
You can't add a 0 before an int ( (01 == 1) -- mostly but I'm not going to get into the vagaries of that).
Just add a type column. You can always drop the column later.