I want to update the status column in my table, but the date column of the message is also updated. How can I only update the status column only without the date field ?
Data type:
Status => Varchar
messgae_date => Timestamp
Query :
"UPDATE mytable SET status='1' WHERE status='0'"
Thank's before ..
Please edit your question and post the entire DDL statement for creating the table. I believe it will become apparent.
Your post said that message_date is a timestamp. That's a special datatype in MySql and MariaDb that can autoupdate itself at insert or whenever any other data in a row is changed.
An auto-updated column is automatically updated to the current
timestamp when the value of any other column in the row is changed
from its current value. An auto-updated column remains unchanged if
all other columns are set to their current values. To prevent an
auto-updated column from updating when other columns change,
explicitly set it to its current value. To update an auto-updated
column even when other columns do not change, explicitly set it to the
value it should have (for example, set it to CURRENT_TIMESTAMP).
Again, please post the table DDL statement. The message_date column will probably look something like this.
`message_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Related
I created database but not added date column, data added last 1year but not show data added current date, possible to get data added current date, please help me
my database name studen
name
age
city
but not added date column, possible to get student joining date
No, it's not possible. If you need to know when a row was added, you should have a column like
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
and if you want to know when the row was modified, you can use a column like
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
If you don't have anything like this in the table, MySQL doesn't save the dates automatically.
i have table called post_data having username,email,pass,cam_name,upd_date(of type datatime) fields in that. but the problem is when i insert username, email pass into the table that upd_date(datatime) is updated with current data and time. how can i restrict that ...
I even change the datatype form DATETIME to TIMESTAMP
No use..still it stores current date and time when i update only certain columns.
can any tell me ...
You have set the default datetime for that column. You need to alter the column.
alter table post_data alter column upd_date drop default;
If your column doesn't accept NULL, then it will implicitly set default value. You need to alter you table to allow null.
Data Type Default Values
If a column definition includes no explicit DEFAULT value, MySQL
determines the default value
ALTER TABLE post_data MODIFY upd_date datetime null;
I have a feeddate column with the info below..
type : timestamp
attributes : on update CURRENT_TIMESTAMP
default : CURRENT_TIMESTAMP
and when I used PHP to INSERT INTO the rows with this code ..
$badgefeed = "INSERT INTO d VALUES ('','".$userID."','Badge','','".$badgeNAME."','".$badgeTYPE."','".$badgelv."','','')";
$badgefeedQ = mysql_query($badgefeed);
(feeddate is on the last column that NULL)
This feeddate doesn't update and be like 0000-00-00 00:00:00
but it's gonna work when I used PHP to UPDATE something that already had in the table.
Did I do anything wrong with my feeddate structure or the INSERT code is incorrect ?
Your query should be:
$badgefeed = "INSERT INTO d VALUES ('','".$userID."','Badge','','".$badgeNAME."','".$badgeTYPE."','".$badgelv."','')";
Don't use any value for the last column as you are using the timestamp as default value in MySQL. Just omit the last (blank) value from your query. In such a way, value for the concerned column will be considered null and thus default timesamp will will be used.
Insead of '' use null.
This way, mysql manages the value itself.
If you specify any value, the server will take your value, regardless other triggers, settings.
If you don't include the column in the insert statement or leave it blank (null), it will get set by the server, according to the defaults. In this case the default being on update CURRENT_TIMESTAMP
I am getting the error
Error Code: 1062. Duplicate entry '0' for key 1
when inserting a new value to a table where the primary key is set to auto_increment. This column is set to int(11) data type and when you select the max value of the column it is 16000. (Note: I can manually add a new row in with a random high number fine, the issue is purely with auto_increment).
If I do this:
SHOW TABLE STATUS FROM database LIKE 'tablename'
The value of auto_increment is set to NULL.
Strangely enough I tried to replicate the table, with the plan to just copy all the existing data over and when I recreated the same table with a new name and there is no data in the table the auto_increment next value is also set to null.
This particular table has a lot of columns so I wonder if maybe I have hit on another limit of some sort?
I resolved the issue. For some reason the column was no longer set to auto-increment. It had worked fine for the previous 13,000 records so I am not sure why the flag had suddenly been removed.
I'm creating a change log db that's the exact same as my active db except it has a changedate DATE field at the end.
The db is basically one primary key id, and about 50 other columns of various data types. The script I have in php is it tries to insert new ids and if it gets the error message for duplicate primary key, then it should get that row, insert it into my backup db with a curdate() call as the final date value, delete the entry from my first db, then insert the new entry.
I have all the other parts of the script finished except the part where I have to insert everything from the first table + an extra column for curdate(). Or if there's a better solution to my problem of inserting into a backup database when a duplicate primary key comes in when there's a fairly high amount of rows please share that.
You could do an INSERT INTO SELECT:
INSERT INTO `backupTable` SELECT *, NOW() FROM `originalTable` WHERE id = '$id';
You have to specify the ID for the entry you wish to copy to your backup db. You have also to be sure, that the IDis not already in your backup table. You can use REPLACE INTO to workaround this case.
REPLACE INTO `backupTable` SELECT *, NOW() FROM `originalTable` WHERE id = '$id';
basicly, you can create a TIMESTAMP column with CURRENT_TIMESTAMP as default value.
when you insert a row to that table, the current date/time will be automaticly inserted.
is that what you were looking for ?
BTW: i would recommend to kill the problem at it source and make sure a duplicate primary key will not be inserted to the datatable..
to do that, you can use the SELECT LAST_INSERT_ID();