I have an admin users db, I want to create a column with type datetime for edits, to know when and admin acc was edited, I will introduce the time with php and MySQLi, im on phpmyadmin, when I try to create the column it says:
#1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'editado' at row 1
MySQL code is:
ALTER TABLE `admins` ADD `editado` DATETIME NOT NULL AFTER `password`;
Tried to execute on SQL but nothing, how can I do this?
If the table is not empty when you add a column, you need to first add the column as NULLABLE, update all the records in the table assigning a value to the column, then you can change the column you added from NULLABLE to NOT NULL. So something like this should be done:
ALTER TABLE admins ADD editado DATETIME NULL AFTER password;
UPDATE admins SET editado = '1900-01-01 00:00:00';
ALTER TABLE admins MODIFY editado DATETIME NOT NULL;
Not sure if the last query is correct syntax as I don't write MySQL often but thats what should happen.
Related
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
I have DATETIME value in my database which is CURRENT_TIMESTAMP by default, and this column can be empty. When I try creating objects in PHP and inserting it into the database, it still asks me to insert that DATETIME value.
My question is: how can I insert object into database without defining that DATETIME value? Shouldn't the database enter this value automatically?
When I am updating that object, I don't want to enter that value at all.
You could use DEFAULT keyword or do not specify that column at all:
CREATE TABLE myTab(i INT PRIMARY KEY AUTO_INCREMENT,
b TEXT,
c TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
INSERT INTO myTab(b, c)VALUES ('a', DEFAULT);
INSERT INTO myTab(b) VALUES ('b');
SELECT * FROM myTab;
Rextester Demo
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'm creating a database for an application. The table in question will be a log of all purchases made through the app, and one of the columns in the table will store the current date & time in DATETIME() (YYYY-MM-DD HH-MM-SS format).
I know the SQL function Now() returns the current system date and time in this format, but can I create the table so that each new purchase automatically stores the current system time? I'm trying to do something like this:
CREATE TABLE purchase_log (
//bunch of code
timestamp DATETIME() NOT NULL DEFAULT Now(),
//more code
);
So that by default each entry into purchase_log will automatically store the value returned by Now(), but I don't think this is valid SQL syntax. What would be the best way to do this?
Thanks!
CREATE TABLE purchase_log (
`addedwhen` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
I used the fieldname "addedwhen" as "timestamp" is a MySQL reserved word.
Ref: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
but I don't think this is valid SQL syntax.
How? It's absolutely valid; Did you actually tried running that? If not then see here http://sqlfiddle.com/#!9/e0b1a7/1
CREATE TABLE purchase_log (order_id int not null,
order_name varchar(10),
orderdate DATETIME NOT NULL DEFAULT Now()
);
insert into purchase_log(order_id, order_name) values(1,'laptop');
Add a default value to the field as CURRENT_TIMESTAMP.
However, while inserting data into the table, you will need to be careful.Either pass NULL to the field, or ensure proper timestamp.