MySQL update values using set - mysql

I want to update the value in my handicap column with the value in handicapChange column
My Table looks like:
Name | Handicap | Points | HandicapChange
Joe Bloggs | 21.00 | 39 | 20.50
What I want to do is update the Handicap column with the value(20.50) in HandicapChange column.
I was thinking something like:
update table comp SET Handicap = HandicapChange
Thanks

You should use UPDATE comp SET Handicap = HandicapChange WHERE Name = 'Joe Bloggs', otherwise you'll set these values for every record currently present in the table.

UPDATE table SET Handicap = HandicapChange will work, but it will assign the value of HandicapChange to Handicap to EVERY row in the table. If this isn't what you want, make sure you add a WHERE clause targeting only those rows that you actually want to change.

If you want to update the entire table than use that query. If you want to narrow down your set of records to update you'll need to specify parameter using WHERE.
update table comp SET Handicap = HandicapChange WHERE Name = 'Joe Bloggs'
See MySQL for more info.

Related

How to implement Update Mechanism in MySql?

My Use Case :
I am trying to create a GUI and implement it with MYSQL Database. The problem I am facing is the scenario when I have to update a certain entry in the Database.
I know that we can update an Entry in MYSQL database using :
ALTER TABLE <TABLENAME> SET <PARAMETERS=NEW VALUES> WHERE <CONDITION> ;
For eg : If I want to change the name of the guy who id is 2 , I have to write :
ALTER TABLE StudentInfo SET Name='ABC' WHERE id=2 ;
But the problem is , in a GUI based environment , a user can choose to update any particular value wihtout having a constant condition like id in the previous example.
In the UI , the user can opt to select anything from the parameters and modify it and then click the update button.
Now How will I figure out what <CONDITION> to put in the MYSQL query when I need to update the database ?
Any help would be greatly appreciated !
you update a by using the UPDATE command not ALTER, which will change table. Your gui already knows ho tow identify the row in your case for example by the column name
UPDATE StudentInfo SET Name='ABC' WHERE Name='QUERTY';
SEE example
CREATE TABLE StudentInfo(
Name VARCHAR(20),
class int,
section VARCHAR(2),
roll_no int
);
INSERT INTO StudentInfo VALUES ('abc',12,'A',18), ('xyz',12,'A',17),('QUERTY',12,'A',16)
UPDATE StudentInfo SET Name='ABC',class = 15,section = 'B',roll_no= 99 WHERE Name='QUERTY';
SELECT * FROM StudentInfo
Name | class | section | roll_no
:--- | ----: | :------ | ------:
abc | 12 | A | 18
xyz | 12 | A | 17
ABC | 15 | B | 99
db<>fiddle here
The main problem is to identify the correct row, so you should have a field that is unique.
Like an id auto_increment, that is invisible for the user, but you can identify every row and use this id to update the row.
UPDATE StudentInfo SET Name='ABC' WHERE id = 3;
So that if you have two rows with John Smith you still could update the right one

Modify All varchar elements in a MySQL column

In a MySQL database,
I have a table License with a few example rows as presented below:
ID | Key | Location
1 25 C:/Public/lics/1885-0001.lic
3 21 C:/Public/lics/1885-0006.lic
There are many such rows, which I would like to modify as given below:
ID | Key | Location
1 25 C:/Licenses/1885-0001.lic
3 21 C:/Licenses/1885-0006.lic
One of the columns from all the rows get modified. How do I update the table to make this change across all rows.
Judging from the docs I posted in my comment, I think you should do something like this:
UPDATE License SET Location = REPLACE(Location, 'C:/Public/lics', 'C:/Licenses');
UPDATE License
SET Value = REPLACE(Location, 'Public/lics', 'Licenses')

MySQL update column based on another column data manipulation

I have a table looks like below:
id name value newColumn
============================
1 Joe 22 null
2 Derk 30 null
newColumn is a newly added column and I would like to update the data for each and every rows with a specific format: concat value and a specific string -CIF
End result should be something like below:
id name value newColumn
============================
1 Joe 22 22-CIF
2 Derk 30 30-CIF
May I know how to construct such update query in mySQL?
This is a fairly basic update:
update table t
set newColumn = concat(value, '-CIF');

Update data in database using SQL

I have one table in my database. Field of table are describe below.
ID | NAME | QUALIFICATION
1 | ABC | Phd
2 | XYZ | MBA
3 | ADS | MBA
Now my problem is related to update QUALIFICATION record. Suppose if I update record of QUALIFICATION, it should be append new value to existing value.
For example, I am going to update record of id=1. Now I update "QUALIFICATION" MCA then it should add MCA to the existing record Phd, separated with comma. Output will looks like below.
ID | NAME | QUALIFICATION
1 | ABC | Phd,MCA
2 | XYZ | MBA
3 | ADS | MBA
When "QUALIFICATION" is null then the update should not be add comma before MCA.
Thats a bad database design never store the data as comma separated string, this will make things messy in future.
You should think of normalizing the table something as for the student your table should look like
- id primary key auto_incremented
- name
- other columns related to student
Then another table as student_qualification
- id primary key auto_incremented
- id_student ( id from student table)
- qualification
So for each student you can add as many qualification as possible to this table and can easily do add/edit/delete data
You can later easily retrieve data using simple joining the table.
first u have to select your existing value of Qualification column
that u want to update
Using
select qualification from tb_name where id = 1
Using above query u will get your qualification column value
suppose in
$qulification
Now update that row using
update set tb_name set qualification = '".$qualification."."your new value" where id = 1
May you can try this
update employee set qualification = qualification || ',MCA' where id = 1
the above will work in oracle
EDIT:
Then you can have the case statement with it
update employee set qualification = case when qualification is null then
'MCA' else qualification || ',MCA' end where id = 1
You can test for NULL in the SET clause, and use concatenation to format the string appropriately.
update student
set qualification = concat(if (qualification is not null
, concat( qualification, ',')
, '' )
, 'MBA')
where id = 1;
Here is a working SQL Fiddle (which also demonstrates behaviour with a NULL qualification).
I agree with #Abhik that this is a bad design for this specific data, and normalization is the better approach for the use case you provide However, there are other use cases where doing this sort of update would be perfectly valid so the question is worthy of a proper answer..

Why does 'on update CURRENT_TIMESTAMP' doesn't update when same data is updated?

When i have a table with the following data:
StatementID(int AI) | created_by(int) | changed_when(onUpdate CURRENT_TIMESTAMP)
--------------------------------------------------------------------------------
7 | 4 | 2013-02-26 12:05:57
8 | 4 | 2013-02-26 12:20:12
I have the following Query:
mysql_query('
UPDATE table
SET created_by = 4
WHERE statementID=8');
When I edit the statement info(other tbl) and it is edited by the same user as last time, the changed_when doesn't update.
Why does the field changed_when not change when I update created_by with the same data?
This behaviour is by design. onUpdate CURRENT_TIMESTAMP fields update when the values of fields change, not when they stay the same.
To achieve what you seem to want, you can do
UPDATE table
SET created_by = 4,
changed_when = null,
WHERE statementID = 8
If in any of the update statement if the value remains same then the timestamp value wont be updated.
From Mysql SITE
If the column is auto-updated, it is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. The column remains unchanged if all other columns are set to their current values. To prevent the column from updating when other columns change, explicitly set it to its current value.
Solution
To update the column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).
Refer