Update Non key columns Query Pheonix JDBC - mysql

I have problem with updating values in apache phoenix . The below query is throwing JDBC exception. I am new to Pheonix JDBC and confusing with upsert query usage for updating non primary key field values.
String sql = UPSERT INTO mytable (serverName,SationName, product) SELECT serverName,stationName ‘sampleProduct’ FROM mytable WHERE product = ‘sampleProduct’;
The primary key of "myTable" is combination of "serverName" and "StationName". I would like to update value of product column from 'sampleProduct' to 'TestProduct'.

Update the sql query with following line. Hope it will help.
String sql = "REPLACE INTO mytable (serverName,SationName, product)
SELECT serverName,stationName , 'sampleProduct'
FROM mytable WHERE product = 'sampleProduct'";

"The primary key of "myTable" is combination of "serverName" and "StationName". I would like to update value of product column from 'sampleProduct' to 'TestProduct'."
You say nothing about "inserting if the row does not already exist, so I don't see UPSERT as being appropriate. The MySQL code is
UPDATE myTable
SET product = 'sampleProduct'
WHERE serverName = '...'
AND sampleProduct = '...';
(I don't know what values are needed for '...'.)

Related

MySQL Query: UPDATE and/or APPEND

I have a temporary table that I use to insert into the master db.
The temp table is named "temp_table"
The master table is "master"
I currently use the following command to update "master"
SELECT COUNT(*) FROM master;
SHOW COLUMNS FROM master;
INSERT INTO master
SELECT * FROM temp_table
ON DUPLICATE KEY UPDATE email = VALUES(email), phone = VALUES(phone)
Now, I want to be able to append field (counter) from the "temp table" into "master." The field already exists in both tables and I just want to be able to update or append it.
"counter" field in master may be empty or it may contain a number value already.
In cases where the value exists, it should append separated by a comma. Format (88,89,90)
In cases where the it's empty, it should update (88)
Thank you in advance.
I think you want:
on duplicate key update
email = values(email),
phone = values(phone),
counter = case when counter is null
then values(counter)
else concat(counter, ',', values(counter))
end
You can also phrase this with coalesce(), although the expression might be a bit more complicated to understand:
on duplicate key update
email = values(email),
phone = values(phone),
counter = concat(
coalesce(concat(counter, ','), ''),
values(counter)
)

Conditional UPDATE MariaDB (MySQL)

Code:
UPDATE COMPANY SET id='21'
WHERE id='20';
Error:
SQLException: Duplicate entry '21' for key 'PRIMARY'
I want to UPDATE the primary key field in this case it's called 'id' to another value but if the value exists already it throws the error above. How would I do a conditional UPDATE based on if the 'id' doesn't exist in the COMPANY table already, to avoid throwing that error using MariaDB syntax.
NOTE:
I am NOT talking about doing a conditional INSERT that uses "ON DUPLICATE KEY UPDATE" as shown below.
INSERT INTO COMPANY(id,first,last,age)
VALUES('1','Tim','Jones','70')
ON DUPLICATE KEY UPDATE id='1';
You can use UPDATE IGNORE:
UPDATE IGNORE COMPANY
SET id='21'
WHERE id = '20'
See a simplified demo.
You can count the number of values already in the table:
UPDATE COMPANY C CROSS JOIN
(SELECT COUNT(*) as cnt
FROM COMPANY
WHERE id = 21
) CC
SET c.id = 21
WHERE id = 20 AND cnt = 0;
Note: In most databases, you would use NOT EXISTS in the WHERE clause, but MySQL/MariaDB doesn't support references to the table being updated.

How do I update if exist, else insert in MySQL

I have table with AUTO_INCREMENT field defined as PRIMARY_KEY.
I have columns like:
vote_id,vote_user_id,vote_ask_id,vote_comment_id,vote_post_id,vote_type,vote_status
I want to INSERT new records but before I do that I want to check if there is a row with columns(vote_user_id,vote_ask_id,vote_type) as same as the new data I want INSERT.
CONDITIONS:
IF ROW EXISTS
THEN UPDATE tableName SET vote_status=new_value, vote_time=new_time
ELSE
INSERT NEW RECORDS
I have searched the internet and learnt about MySQL ..ON DUPLICATE KEY UPDATE.
I have realize this statement will not be helpful to my task since it only checks for DUPLICATE KEY(...PRIMARY_KEY or UNIQUE FIELD).
I have learnt also on MySQL REPLACE INTO ...and likewise this will not be helpful to my problem since that is also bind to PRIMARY_KEY or UNIQUE index.
I learnt I could use MERGE....USING...statements
but this was giving me errors so i read more about it and I relised it only work in SQL server (Microsoft)
Please how best can someone help me solve this?
I tried this on MERGE staments:
MERGE {$votes_table} WITH (HOLDLOCK) AS VT
USING ({$Qid},{$vote_type},{$CUid},{$vote_status}) AS VTS (vote_ask_id,vote_type,vote_user_id,vote_status)
ON( VT.vote_ask_id = VTS.vote_ask_id AND VT.vote_user_id=VTS.vote_user_id AND VT.vote_type=VTS.vote_type)
WHEN MATCHED THEN
UPDATE SET VT.status=VST.vote_status , VT.vote_time='{$current_time}' WHERE VT.vote_user_id=VTS.vote_user_id AND VT.vote_ask_id=VTS.vote_ask_id AND VT.vote_type=VTS.vote_type
WHEN NOT MATCHED THEN INSERT (vote_ask_id,vote_type,vote_status,vote_user_id,vote_time) VALUES('{$Qid}','{$vote_type}','{$vote_up_status}','{$CUid}','{$current_time}')
In MySQL, use ON DUPLICATE KEY:
INSERT INTO tablename (vote_user_id, vote_ask_id, vote_type, . . . )
VALUES (new_vote_user_id, new_vote_ask_id, new_vote_type . . . )
ON DUPLICATE KEY UPDATE vote_status = VALUES(vote_status), vote_time = VALUES(vote_time);
For this to work, you need a unique index/constraint on the columns that define a unique row:
CREATE UNIQUE INDEX unq_tablename_3 ON tablename(vote_user_id, vote_ask_id, vote_type);
You do want insert ... on duplicate key update: in MySQL, this is the right way to do what you want.
To start with, you need to create a unique constraint on the tuple of concerned columns:
create unique index votes_table_unique_idx
on votes_table(vote_user_id, vote_ask_id, vote_type);
Then, you can do something like:
insert into votes_table(vote_user_id, vote_ask_id, vote_type, vote_comment_id, vote_post_id, vote_status)
values(...)
on duplicate key update
vote_comment_id = values(vote_comment_id),
vote_post_id = values(vote_post_id)
vote_status = values(vote_status)

Copying a database record with relations

I have 2 sets of tables in the same database - A live set and a test set. Each set has two tables in it - table A and table B - with a one-to-many relationship between them.
What I need to do is to select certain records from table A in the test set and copy the records in their entirety, along with their relations in table B, into the live table set. The structure of the sets is identical.
is it possible to do this without having to break the records up manually?
I'm using the doctrine ORM (1.2 I think) in the context of the symfony 1.4 PHP framework.
So far I've been trying something like this:
$record = Doctrine_Core::getTable('testSetTableA')->find(1);
$liveSetTableArecord = new LiveSetTableArecord();
$liveSetTableArecord = $record->copy();
$liveSetTableArecord->save();
But I get the feeling that I'm missing something fundamental. As far as I can tell, there is no method for setting a record in it's entirety from a query object?
I did more of my own research into this problem and as far as I can figure out, using the ORM for this type of operation is just a bad idea in the first place because of all the unnecessary overhead involved.
It's far more efficient just to use a raw "INSERT INTO" statement which is what I'm doing now.
$connection = Doctrine_Manager::connection();
$query = "INSERT INTO Set2tableA SELECT * FROM Set1tableA WHERE id = '$id' ON DUPLICATE KEY UPDATE Set2tableA.id = Set2tableA.id";
$statement = $connection->execute($query);
$statement->execute();
$query2 = "INSERT INTO Set2TableB SELECT * FROM Set1TableB WHERE tableA_id = '$id' ON DUPLICATE KEY UPDATE Set2TableB.id = Set1TableB.id";
$statement = $connection->execute($query2);
$statement->execute();
The 'ON DUPLICATE KEY UPDATE' is necessary because the primary key in table B is non-null and therefor when you try to copy record 1 from table A into Table B mysql finds that there is already an entry with ID=1 and throws an error.

MySQL Conditional Insert. Works in phpMyAdmin but not PHP Script

I am trying to create a conditional INSERT into my MySQL databate from a PHP script. The following SQL syntax works in phpMyAdmin, but not in my PHP Script:
INSERT INTO profiles (id, firstname)
SELECT "22","John" from profiles
WHERE NOT EXISTS (
SELECT * FROM li_profiles
WHERE li_p_firstname = "John"
)
(Note that "id" is the primary key, "firstname" is not a key or unique)
Something weird that might be part of the issue is that when I run that SQL in phpMyAdmin, while it does "work" (meaning that a new record is added with the id "22" and the firstname "John") I get the following warning: "#1062 - Duplicate entry '22' for key 1"
But the table didn't have a previous entry with id of 22. ??!!
What's going on?
Change SELECT to VALUES
INSERT INTO profiles (id, firstname) VALUES("22","John") FROM profiles WHERE NOT EXISTS ( SELECT * FROM li_profiles WHERE li_p_firstname = "John" )
Also, if you are using auto-increment values, you should specify the next value. Also, if its an integer, give an integer (22) not a string ("22")
You'll get a duplicate entry for the iD because you are inserting a new row for each row in the profiles table; for every row in the profiles table there is no John in the li_profiles table. You might try
INSERT INTO profiles (id, firstname)
SELECT "22","John" from profiles
WHERE NOT EXISTS (SELECT * FROM li_profiles
WHERE li_p_firstname = "John")
LIMIT 1;
which would eliminate the duplicate problem (if it works, sorry but I haven't checked this myself).
I figured it out in a different way. (I'm told that the HAVING statement is slow, so I'm not sure that it's the best way... but it the only method I've gotten to work.)
INSERT INTO profiles (id,firstname)
SELECT 22,'John'
FROM li_profiles
WHERE firstname = 'John'
HAVING COUNT(*) = 0;