mysql> desc oldtable;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | char(1) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc newtable;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | tinyint(1) | NO | | 0 | |
+------------+--------------+------+-----+---------+----------------+
I would like to port data (dump) from oldtable into newtable. One issue is, earlier the table used char(1) for active which stores value either 'Y' or 'N'. Now the newtable stores it as int either 1 or 0.
How can i fix this before porting data? Should I use shell script for such fix & porting ?
Any sample scripts or tips :)
INSERT INTO newtable
SELECT uid,IF(active='Y',1,0) as active FROM oldtable
should do the trick
INSERT INTO newtable (uid, active)
SELECT uid, IF(active='Y', 1, 0) AS active
FROM oldtable
docs for this syntax here: http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html
Joking version:
INSERT INTO newtable
SELECT uid,FIELD(active,'Y') as active
FROM oldtable
Related
I'm trying to setup a mysql trigger. So lets assume the table is like this
mysql> explain users;
+------------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| purge_date | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+-------------+------+-----+-------------------+-----------------------------+
and
mysql> explain users_details;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
so the trigger i have setup will create a purge_date of NOW on the users table when something is removed from the users_details table. This is fine.
The issue is that sometimes the data for the users_details table comes through incorrectly and then the users update it so then i need to remove the purge_date if they re-insert the data. Now I have made this trigger
CREATE TRIGGER purge_fix AFTER INSERT ON users_details
FOR EACH ROW
BEGIN
UPDATE users
SET purge_date= '0000-00-00 00:00:00'
WHERE users.name = NEW.name;
END;
Which works fine but in some situations there will not be a link between the users_details table and the users table (it will be created at a later stage) so my question is, as the trigger will fail in some situations (which is 100% fine by me), will I be breaking something having the trigger failing?
I want to create a table with data from another table in order to set the id to a certain auto_increment start value.
This is the table I want to populate with data from another table:
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| city | varchar(255) | YES | | NULL | |
| state | varchar(255) | YES | | NULL | |
| state_id | int(11) | YES | | NULL | |
| city_slug | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
After executing this query:
alter table table_temp auto_increment = 40499;
If I do a dummy insert:
insert into table_temp (city, state, state_id, city_slug) values (1, 1, 1, 1);
It does what it's expected, that is, the id starts with the value 40499
select * from table_temp;
+-------+------+-------+----------+-----------+
| id | city | state | state_id | city_slug |
+-------+------+-------+----------+-----------+
| 40499 | 1 | 1 | 1 | 1 |
+-------+------+-------+----------+-----------+
After truncate the table and execute again the alter table query for auto_increment, I try to populate the same table with data from another table:
insert into table_temp (city, state, state_id, city_slug) select city, state, state_id, city_slug from final_location;
However the id starts with a default id value of 1:
select * from table_temp limit 10;
+----+---------+---------+----------+-------------------+
| id | city | state | state_id | city_slug |
+----+---------+---------+----------+-------------------+
| 1 | Abatiá | Paraná | 242 | all-cidade-abatia |
+----+---------+---------+----------+-------------------+
Any suggestions on how to fix this?
I've just found what was wrong, after truncating the table it seems to loose reference of the auto_increment start value. I had to execute again the alter table query in order to set the auto_increment value properly again:
alter table table_temp auto_increment = 40499;
mysql> desc oldtable;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | char(1) | NO | | NULL | |
| field3 | char(256) | NO | | NULL | |
| field4 | char(256) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc newtable;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | tinyint(1) | NO | | 0 | |
| field5 | int(12) | NO | | 0 | |
| field6 | varchar(12) | NO | | 0 | |
| field7 | varchar(12) | NO | | 0 | |
+------------+--------------+------+-----+---------+----------------+
This is similar to my previous query change a field and port mysql table data via script ?
[I would like to port data (dump) from oldtable into newtable. One issue is, earlier the table used char(1) for active which stores value either 'Y' or 'N'. Now the newtable stores it as int either 1 or 0.
How can i fix this before porting data? Should I use shell script for such fix & porting ? Any sample scripts or tips :)]
But this question, How to achieve the same porting,If both tables has different no.of fields
like above?
The answer is similar to the previus question answer:
INSERT INTO newtable (uid, active, field5, field6, field7 )
SELECT uid,FIELD(active,'Y') as active, 0,'',''
FROM oldtable
Then update newTable with new fields values:
update newTable
set
field5 = (select someExpression from someTable5 t where t.uid=newTable.uid),
field6 = (select someExpression from someTable6 t where ...),
field7 = (select someExpression from someTable7 t where ...)
Also, you can define new fields as null allowed and leave this fields without value.
alter table if field is not already exist
ALTER TABLE `table`
ADD( `abc` text NOT NULL,
`xyz` tinyint(1) NOT NULL,
);
if abc or xyz fields are already exist the can not be alter table
if it is possible ?
You can use a SHOW COLUMNS beforehand and construct your query accordingly, adding only fields that are missing.
Example output of SHOW COLUMNS:
mysql> SHOW COLUMNS FROM City;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
I can't comment yet, so I post answer: try this link for detailed example. It queries information_schema.COLUMNS table for column information about database tables.
I have a table:
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| fooID | int(11) | NO | PRI | NULL | auto_increment |
| fooDetails | varchar(200) | YES | | NULL | |
| fooListingID | int(10) | YES | | NULL | |
| fooStatus | tinyint(4) | YES | | 0 | |
+----------------+--------------+------+-----+---------+----------------+
I would like to merge data from a similar table with this table but I would like the data to be alternating so that existing data in this table will all be odd "fooID" and the new inserts will all be the even "fooID".
Ideas?
I've interpreted your problem as you make to make all the existing fooIDs odd, and then merge into that table some new, even fooIDs.
You can accomplish this fairly easily:
#first make all the existing ids odd
UPDATE oldtable SET fooID=fooID*2-1;
#now insert rows from the other table, making sure the ids are even
INSERT INTO oldtable (fooID,fooDetails,fooListingID,fooStatus)
SELECT fooID*2,fooDetails,fooListingID,fooStatus FROM newtable;
You can insert the result of a select statement. Use modulo for alternating ids:
INSERT INTO NewTable
SELECT column1, column2
FROM OldTable
WHERE NOT id % 2