I want to update only one field in a mysql table.
I have an "ad_id" which is unique.
The field "mod_date" is a TIMESTAMPS field, which is the one I need to update.
UPDATE main_table
SET main_table.mod_date = NOW()
WHERE classified.ad_id = $ad_id";
I haven't tested this yet because I am afraid it might update all rows.
So I have two questions:
Is there anyway to prevent MySql to update more than 1 row?
Is this sql code correct for updating one row only?
Thanks
If ad_id is unique, it will only update one row (if $ad_id is valid, zero otherwise).
If your worried about an update like this, rewrite it as a select to confirm which rows it will operate on before running it.
Your query doesn't look like it would work as such because it checks for field ad_id in table classified which hasn't been defined in the statement. If this is just a partial query and you're joining the classified table somewhere in the query there's not enough info here to tell how many rows will be modified.
You can add LIMIT 1 to the end of the query to make it update only the first row the query finds, but if you're not sure what the query does the first row might not be the one you want to modify.
As a side note I do have to say that if you're afraid to try and see what the query does, it means that either you don't have a backup of the database or you're working directly with a production database, and both of those options sound pretty scary.
What's the relation between main_table and classified?
For example...
UPDATE header h
INNER JOIN detail d ON d.id_header = h.id_header
SET h.name = 'New name'
WHERE d.id_detail = 10
will update name in the header table for specific id_detail.
In your case if ad_id is unique then you can be sure that MySQL will update only one row.
Related
I can't seem to find an answer to my problem, but I got a database where I would like to change the row in a column to affect all the other rows in the same column, is this possible?
(I can't post an image because of reputation?! - but my column is called Active and there's 2 rows right now - the first one has the value 'ja' and the second one is NULL)
'ja' and NULL are the only two values I will be using, so when the second row is set to 'ja', I would like the first row to change to NULL. But as I can see it might not be possible, but if anyone knows more than me, I would be very happy!
It's Mysql database I use sql queries and php.
I'd keep things simple and run two queries.
One to set everything back to null
The second to set the active record to 'ja'
I'm guessing that there's probably a foreign key in the table too so that would be part of the update statement too.
As a matter of "style" I'd prefer to see an active column having a 1/0 or y/n type of value rather than using null all the time.
That is a complicated database design. The first thing that comes to mind is a trigger updating all other rows whenever a row gets 'ja'. However this will fail, because you cannot update the same table you are already updating.
It is generally a bad idea to design a table such that all rows must contain value y when one row contains value x. I would solve this with by removing the "active" column altogether and replace it with a one row table containing a column active_id. So there is always just one id active and the others are implicitely inactive.
If you want to stick with your database design, a possible update statement would be:
update mytable
set active = case when id = 123 then 'ja' else null end;
However the dbms doesn't guarantee that there will always be exactly one record with 'ja'. It's up to you to be careful about it.
PS: I agree with Sarah King on the NULL issue. NULL means "I don't know", but you know very well that the records are not active. So this should be a non-nullable column with values "ja" and "nein" or whatever you prefer. In MySQL you would rather use a BOOLEAN column.
Can this be done in a single query? The table has an auto increment field which I need to know the number to fill url field in the table.
Table
id(AI) | title | url
What I am expecting is something like
INSERT INTO table (title,url) VALUES ('name','CONCATENATION OF title AND ID');
I am currently doing this using 2 queries.
1.Writing the fields except URL.
Getting the id using mysqli_insert_id()
2.Updating the above written row.
P.S : The Table has other fields as well so changing the db design isnt really possible in this case.
It can't be done atomically. In theory, you could SELECT MAX(id) + 1 FROM yourtable, but please, please don't - although this is not guaranteed to give you the right result and is definitely not a safe approach.
This seems like bad practice, anyway. Why not concatenate the title and ID when you fetch it? Why must it be concatenated on insert?
I will not comment on the design of your database -- you are the judge of that. Just bear in mind that the following command gets the next auto-increment-ID for the specified table, and that this number could change in an instant if another user accesses the table before your code can use it.
I am using this code myself in a project for a similar reason to your own, and it works for me because the table is updated only a few times per day and never by more than one person at a time.
SELECT Auto_increment FROM information_schema.tables WHERE
table_name = '$name_of_your_table';
To be clear, this code gets the auto-increment ID that will be given to the next table entry for the specified table.
I use phpmyadmin to manually change the id (auto_increment) of some row data, therefore when I run my program to create a new row data, it returns me an error of "duplicated entry id and thus could not create the row".
This problem seems classical but I don't know the right keywords or phrases to look it up on google. Thank you.
Try
ALTER TABLE tablename AUTO_INCREMENT=X
Where X is some number higher than the highest ID.
I haven't tested this, but it may work:
ALTER TABLE tablename AUTO_INCREMENT=(SELECT MAX(id)+1 FROM tablename)
(It may fail due to selecting from the same table that is being altered)
Ok, i have a checklist system that i am working on. There is a page that pulls data from the database and displays it as a checklist. I am trying to create a button that when pushed will reset the database to a certain state that i want. Basically, i have a button that sends an ajax callout to a php page that executes an UPDATE query. This query is as follows:
UPDATE $table SET value='$value', comments='$comments', editedBy='$editedBy', editedDate='$editedDate' WHERE projectId='$projectId';
I set the variables first of course, that's not my question. Just pretend they have data. My question is how can i repeat this query so that every row table x that has a projectId of n is updated? I'm guessing this involves a for loop?
SIDE NOTE: Since this query is just setting the value to false and making the comments, editedBy, and editedDate fields blank for every row in table x that has a projectId of n, is there a better way of doing this other than the UPDATE query?
Thanks for any help!
As long as you don't sepcify a LIMIT in your UPDATE query, it will update every row it finds that satisfies your where clause.
Now, if you're updating the projects table and projectID is your primary key, you'll need to run a loop to update other projectIDs. If you're not updating the project table, then your update query will update any record that has a foreign key match to the project ID you specified.
Does that help?
I have two tables in the database(videos and viewData) .
Im trying to build a script that runs for each record in the "videos" table and does something using the "videoID" field for that specific entry in the "videos" table. The does something part would be dumping some data into the viewData table.
Would I need to store all the records in an array before calling the loop? An example of a loop like this would be really helpful. Also in a way that could be potentially scalable that wouldn't hurt the server too much if there were a 1000+ records in the "videos" table.
Thanks,
Dave
Try to avoid the loop at all costs. Think set based processing, which means handle the entire set of rows within one SQL command.
I'm not entirely sure what you are attempting to do, as your question is a little vague. however, here are two possibly ways to handle what you are trying to do using set based thinking.
You can do a JOIN in an UPDATE, essentially selecting from the parent table and UPDATEing the child table for all rows in a single UPDATE command.
UPDATE c
SET Col1=p.Col1
FROM ParentTable p
INNER JOIN ChildTable c On p.ParentID=c.ParentID
WHERE ...
you can also INSERT based on a SELECT, so you would create one row from each row returned in the SELECT, like:
INSERT INTO ChildTable
(Col1, Col2, Col3, Col4)
SELECT
p.ColA, p.ColB, 'constant value', p.ColC-p.ColD
FROM ParentTable p
WHERE...
Working with a database in the loop isn't a good practice. It is good to select all table data into an array by one query and work with this array in future.
Do you have access by other means to MySQL tables? Like with MySQL Administrator or another tool, even by command line?
This is because it would be much more time, resources and everything else, doing that directly in the database, through a query or a database function.
I would do that this way.
But for the sake of clarity, unless you are storing the videos themselves inside database tables, 1000 records are not a problem. Maybe 10,000 would be.
General tip: do just what you need to do.
If you only need to operate upon data, do this on the database.
If you only need to check one field in one table, use SELECT your_field FROM your_table instead of SELECT * FROM your_table.