i got to execute loads of the following queries
UPDATE translations SET translation = (SELECT description FROM content WHERE id = 10) WHERE id = 1;
Now, i use the load data infile to do inserts and replacements but what i want in esense is to update just 1 field for each row in that table with out messing with the keys. What could be the syntax for this, note that the queries affect existing rows only.
Thanx
Use CREATE TEMPORARY TABLE to create a temporary table.
Then use LOAD DATA INFILE to populate that temporary table.
Then execute your UPDATE translations SET translation = ... to set the 1 field from a SELECT of the temporary table, JOINed with the real table. example syntax below:
UPDATE realTable, tmpTable
SET realTable.price = tmpTable.price
WHERE realTable.key = tmpTable.key
Related
I have table which have 20 million records . I have recently added another column to that table.
I need to update the data into that column.
I'm using MYSQL community edition, when I execute the direct update like this :
Update Employee SET Emp_mail = 'xyz_123#gmail.com'
System Getting hanged and Need to close the execution abruptly.
But when I update the statement with filter condition it is executing fine.
Update Employee SET Emp_mail = 'xyz_123#gmail.com' where ID <= 10000;
Update Employee SET Emp_mail = 'xyz_123#gmail.com' where ID >= 10000 AND ID <= 10000 ;
--------
--------
no of Times
Now I'm looking for looping script where I can execute in chunk wise.
For example in SQL it is like this but I'm not sure of MYSQL:
BEGIN
I int = 0 ;
cnt = 0 ;
while 1 > cnt
SET i = i + 1;
Update Employee SET Emp_mail = 'xyz_123#gmail.com' where ID >= cnt AND ID <= I
END
Note : this is a random script syntax wise there may be some errors . Please ignore it.
I'm looking for Looping in MYSQL
In a row based database system as MySQL, if you need to update each and every row, you should really explore a different approach:
ALTER TABLE original_table RENAME TO original_table_dropme;
CREATE TABLE original_table LIKE original_table_dropme;
ALTER TABLE original_table ADD emp_mail VARCHAR(128);
INSERT INTO original_table SELECT *,'xyz_123#gmail.com'
FROM original_table_dropme;
Then, maybe keep the original table for a while - especially to transfer any constraints, primary keys and grants from the old table to the new - and finally drop the %_dropme table.
Each update, in a row based database, of a previously empty column to a value, will make each row longer than it originally was, and require a reorganisation, internally. If you do that with millions of rows, the effort needed will increase exponentially.
I have many files, for example: 20170319, 20170320 ...
For each file I have 2 columns, one for username and the other was data.
I've created a table
create table A(user varchar(35), date date, data varchar(35), primary key(user, date));
Then, I want to load those files into database, and use filename as specific date in date field.
Can I still use sth like:
Load data infile '20170320' into table A
The answer is that you cannot do this in MySQL alone, you need to use an external program or script that builds the load data infile statements with the appropriate SET clause derived from the name of the file:
The SET clause can be used to supply values not derived from the input
file. The following statement sets column3 to the current date and
time:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, column2)
SET column3 = CURRENT_TIMESTAMP;
The reason for this is that:
load data infile cannot use the file name as an input variable
neither MySQL prepared statements using the prepare statement, nor stored procedures are allowed to use the load data infile statement.
I am trying to insert some data using Load data infile into a mysql table that already has some data. The table contains id and name. My csv file contains three fields: id, name and code. The table schema also has these three fields, but currently, the table has NULL for the code field. I am trying to insert the code from csv to an existing row in the table if it matches the name, else I am trying to insert a complete new row.
The code I have tried is as follows:
LOAD DATA INFILE 'table1.csv'
INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#code, #name, #other_columns)
IF EXISTS (SELECT * FROM table1 where name=#name);
BEGIN
set code=#Code;
END
ELSE
BEGIN
set code=#Code, name=#name;
END
By doing so, I am getting a mysql syntax error, but am unable to figure it out. Can anyone point me in the right direction, or suggest to me another approach? I have thousands of new rows to insert and thousands of existing rows to modify based on the certain field, (name in this case).
MySQL does not allow the LOAD DATA INFILE statement inside a stored program, which is where the IF statement appears. Break up your task into two parts. First, LOAD DATA INFILE into a temporary table. Then create a stored program that replaces the loaded data into your table1 from the temporary table.
In my project I have two code paths that interact with MySQL during first time setup. The first step is the database structure creation, in here, a user has the ability to pick and choose the features they want - and some tables may not end up being created in the database depending on what the user selects.
In the second part, I need to preload the tables that did get created with some basic data - how could I go about inserting rows into these tables, only if the table exists?
I know of IF NOT EXISTS but as far as I know, that only works with creating tables, I am trying to do something like this
INSERT INTO table_a ( `key`, `value` ) VALUES ( "", "" ) IF EXISTS table_a;
This is loaded through a file that contains a lot of entries, so letting it throw an error when the table does not exist is not an option.
IF (SELECT count(*)FROM information_schema.tables WHERE table_schema ='databasename'AND table_name ='tablename') > 0
THEN
INSERT statement
END IF
Use information schema to check existence if table
If you know that a particular table does exist with at least 1 record (or you can create a dummy table with just a single record) then you can do a conditional insert this way without a stored procedure.
INSERT INTO table_a (`key`, `value`)
SELECT "", "" FROM known_table
WHERE EXISTS (SELECT *
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'table_a')) LIMIT 1;
LOAD XML LOCAL INFILE 'file1.xml'
INTO TABLE my_table
ROWS IDENTIFIED BY '<product>'"
Is it possible to use this function to update a table?
I used REPLACE INTO TABLE my_table but that only added new rows and it did not update the existing rows.
LOAD XML LOCAL INFILE 'file1.xml'
REPLACE
INTO TABLE my_table
ROWS IDENTIFIED BY '<product>'"
See: http://dev.mysql.com/doc/refman/5.5/en/load-xml.html
Note that:
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 12.2.5, “INSERT Syntax”.
Probably MySQL is unable to delete the exiting rows due to foreign key restrictions.
You can fix this by:
SET FOREIGN_KEY_CHECKS=0;
...load xml
SET FOREIGN_KEY_CHECKS=1;