Update MySQL without specifying column names - mysql

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.

If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.

You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html

No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.

Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html

I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.

Related

How can I update all records in an Inventory Table to add "/invent" to the beginning of an existing file path using a single query?

I want to add "/invent" at the beginning of the file path in the invImage and invThumbnail columns.
This is what I have in mind,
SELECT FROM inventory
SELECT CONCAT("/invent")
AS invImage, invThumbnail;
Since it is not easy to undo mistakes in SQL, a confirmation of my potential solution will be helpful.
Your current version will overwrite the value in the invImage column, but your stated goal is to
a) prepend the string to the existing value, and
b) do this in two columns, not just one
WHERE invImage = "/images"; also won't match any of the rows, because none of them contain exactly that value. I'll assume you want to update all rows whose invImage value starts with "/images".
Therefore, try this:
UPDATE
inventory
SET
invImage = CONCAT("/invent", invImage),
invThumbnail = CONCAT("/invent", invThumbnail),
WHERE
invImage LIKE "/images%";
My suggestion: add temporarily two columns to the table: tmp_invImage and tmp_invThumbnail.
Then run the query:
UPDATE table SET
tmp_invImage = CONCAT("/invent",invImage),
tmp_invThumbnail = CONCAT("/invent",invThumbnail)
After the update, look if the values of tmp_invImage and tmp_invThumbnail are correct.
Then update the original columns:
UPDATE table SET
invImage = tmp_invImage),
invThumbnail = tmp_invThumbnail
and delete the tmp_ columns.

How do I remove an email domain value and add a new one in a column - mysql

So I have a bunch of users in a column that get refreshed as:
Bill#test.comXYZ
Tom#test.comXYZ
John#test.comXYZ
We refresh the database each week and I need to update these appropriate emails to:
Bill#domain.com
Tom#domain.com
John#domain.com
I figured I can use concat to do the latter, but I am stuck on the former issue. Is there a way to split the values (like split Bill#test.comXYZ into Bill - #test.comXYZ and then remove the #TEXT values?).
Anyways, any help will be much appreciated.
You can use the mySQL replace function, i.e.
UPDATE mytable
set myfield = replace (myfield, '#test.comXYZ', 'domain.com')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Duplicate column and add an automatic extension with mySQL. How?

I have two columns with mySQL:
"part_no"
"pdf_link"
I need the "pdf_link" column to automatically grab/duplicate the "part_no" value and add a .pdf extension on the end.
For example: If part_no = 00-12345-998, then pdf_link = 00-12345-998.pdf
I need this to happen every time I insert.
I appreciate the help.
Erik
you can achive this effect by using triggers I think.
http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
CREATE TRIGGER ins_pdf AFTER INSERT ON MY_TABLE SET #pdf_link = concat(#part_no,'.pdf')
Why store this extra computed information in the database? You can do this in the query when you pull it out, or, if needed, you could make a view that does it only as-needed.
Example pseudo query (my brain hurts right now, so this is only an example):
select concat(`part_no`, ".pdf") as `pdf_link` from `parts`;
If you really need this, you could use a trigger to duplicate the data ans add the extra string.

SQL server insert multiple rows and incrementing a int column

I have some rows in a table and need to transfer them to another table. In the destination table i need also to add a field with an incremental value.
I'm doing the following, but i know that something in the insert is wrong, because the incremented value (intCodInterno) is always the same:
INSERT INTO Emp_VISOT.dbo.TBL_GCE_ARTIGOS
( strCodigo ,
strDescricao ,
intCodInterno ,
intCodTaxaIvaCompra ,
intCodTaxaIvaVenda ,
strCodCategoria ,
strAbrevMedStk ,
strAbrevMedVnd ,
strAbrevMedCmp ,
bitAfectaIntrastat
)(
SELECT A.Artigo ,
a.Descricao ,
IDENT_CURRENT('Emp_VISOT.dbo.TBL_GCE_ARTIGOS')+1,
'3' ,
'3' ,
'1' ,
'Un' ,
'Un' ,
'Un' ,
'0'
FROM PRIVESAM.DBO.Artigo A)
What do i need to change so the value is incremented correcty?
Thank you.
EDIT:
I made a small change in the query, and now it works.
I just insert a SELECT in the IDENT_CURRENT inside brackets:
(SELECT IDENT_CURRENT('Emp_VISOT.dbo.TBL_GCE_ARTIGOS')+1)
I got all the rows that i need from the old table to the new with the incremented value.
the IDENT_CURRENT('Emp_VISOT.dbo.TBL_GCE_ARTIGOS')+1
evaluated once when you want to run the query and all the rows will get the same id.
first solution is to iterate over the select result by a loop construct like cursor or whatsoever and insert the incremented index(you do that)
second solution is to make that column in destination table identity
Remove the part with intCodInterno and in SQL Server use the Identity property to automatically increment it for you.
IDENT_CURRENT won't update until the transaction commits, therefore its value remains constant until you insert.
Here are three options for fixing this issue:
Use some kind of counter (#newRowNum) such that for each row in your SELECT query, #newRowNum = #newRowNum +1, and thus your intCodInterno number = IDENT_CURRENT() + #newRowNum. This would probably require a lot of hacking to work though. Don't recommend it.
Insert your rows sequentially using the same business logic you have now - it will be tremendously less performant, however. Don't recommend it.
Set that column in your destination table to be an identity column itself. This is by far the best way to do it.
If you need a custom identity function (I assume there's a reason you're not using an identity column now), you can create one using some of the steps outlined above: http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
In my case , i Inserted rows sequentially using the same business logic. I cannot use auto increment as i have to import old data also into this column. Once you have imported the data then u may go for updating the column for auto increment .

Does replace into have a where clause?

I'm writing an application and I'm using MySQL as DBMS, we are downloading property offers and there were some performance issues. The old architecture looked like this:
A property is updated. If the number of affected rows is not 1, then the update is not considered successful, elseway the update query solves our problem.
If the update was not successful, and the number of affected rows is more than 1, we have duplicates and we delete all of them. After we deleted duplicates if needed if the update was not successful, an insert happens. This architecture was working well, but there were some speed issues, because properties are deleted if they were not updated for 15 days.
Theoretically the main problem is deleting properties, because some properties are alive for months and the indexes are very far from each other (we are talking about 500, 000+ properties).
Our host told me to use replace into instead of deleting properties and all deprecated properties should be considered as DEAD. I've done this, but problems started to occur because of syntax error and I couldn't find anywhere an example of replace into with a where clause (I'd like to replace a DEAD property with the new property instead of deleting the old property and insert a new to assure optimization). My query looked like this:
replace into table_name(column1, ..., columnn) values(value1, ..., valuen) where ID = idValue
Of course, I've calculated idValue and handled everything but I had a syntax error. I would like to know if I'm wrong and there is a where clause for replace into.
I've found an alternative solution, which is even better than replace into (using simply an update query) because deletes are happening behind the curtains if I use replace into, but I would like to know if I'm wrong when I say that replace into doesn't have a where clause. For more reference, see this link:
http://dev.mysql.com/doc/refman/5.0/en/replace.html
Thank you for your answers in advance,
Lajos Árpád
I can see that you have solved your problem, but to answer your original question:
REPLACE INTO does not have a WHERE clause.
The REPLACE INTO syntax works exactly like INSERT INTO except that any old rows with the same primary or unique key is automaticly deleted before the new row is inserted.
This means that instead of a WHERE clause, you should add the primary key to the values beeing replaced to limit your update.
REPLACE INTO myTable (
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
...will provide the same result as...
UPDATE myTable
SET myColumn1 = 'value1', myColumn2 = 'value2'
WHERE myPrimaryKey = 100;
...or more exactly:
DELETE FROM myTable WHERE myPrimaryKey = 100;
INSERT INTO myTable(
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
In your documentation link, they show three alternate forms of the replace command. Even though elided, the only one that can accept a where clause is the third form with the trailing select.
replace seems like overkill relative to update if I am understanding your task properly.