I want to add a time/date column in a db table. Is this sufficient (or wrong)? Any suggestions are welcome
CREATE TABLE timestamp_n_datetime (
id INT AUTO_INCREMENT PRIMARY KEY,
ts TIMESTAMP,
dt DATETIME
);
I want to add a time/date column in a db table. Is this sufficient (or wrong)? Any suggestions are welcome
CREATE TABLE timestamp_n_datetime (
id INT AUTO_INCREMENT PRIMARY KEY,
ts TIMESTAMP,
dt DATETIME
);
Iām uncertain why youād store both a Timestamp and a Datetime. Whatās your use case?
I would just like a Datetime
here ya go ā
CREATE TABLE datetime_only_no_timestamp
( id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY
, dt DATETIME
);
Iām assuming youāre aware of the differences? eg. supported ranges, UTC conversion, etc.
no, not aware
You should give this at least a quick read. Thereās quite a bit to it but I think if thereās anything you may need to consider you should recognize it as such when you see it.
https://dev.mysql.com/doc/refman/8.0/en/datetime.html
The
DATE
,DATETIME
, andTIMESTAMP
types are related. This section describes their characteristics, how they are similar, and how they differ.
Thank you. Regarding your "here ya go CREATE TABLE etc
wouldnāt it be ALTER TABLE? since the date time will be a new column in an existing db table?
Hello,
DATETIME and TIMESTAMP datatype is same, however, the range differs for them. i.e. DATETIME supports ā1000-01-01 00:00:00.000000ā to ā9999-12-31 23:59:59.999999ā whereas TIMESTAMP supports ā1970-01-01 00:00:01.000000ā to ā2038-01-19 03:14:07.999999ā. in most of the cases, we need either one of them. we may need both of them specifically in very exceptional cases.
Thank you.
Wouldnāt it be ALTER TABLE(instead of CREATE TABLE)? since the date time will be a new column in an existing db table?
It depends. Your OP has a CREATE TABLE example with both timestamp and datetime fields. You then posted that you would ājust like a datetimeā. It could be inferred that because the example was a CREATE TABLE, that you wanted a CREATE TABLE answer.
If you are wanting to add a datetime field to an existing table then yes, it should be an ALTER TABLE instead.
Thank you. I tried this and itās reporting that this is not a valid query:
ALTER TABLE datetime_only_no_timestamp (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt DATETIME
);
any additional help will be welcomed
Well, two means to go about it:
1: DROP TABLE IF EXISTS datetime_only_no_timestamp; CREATE TABLEā¦
2: ALTER TABLE datetime_only_no_timestamp DROP COLUMN ts;
You canāt alter a table that doesnt exist,
and the ALTER syntax requires either a series of ADD, DROP, or ALTER/MODIFY (depending on engine) subclauses to identify WHAT you want to alter about the table. It doesnt accept a schema, because it would be ambiguous how you wanted to modify the table.
EDIT: Make the drop flexible to the existence state of the table.
Thanks for your reply, Iām trying to add a time stamp to an existing db table, what would you suggest?
Well if itās an existing table with data in it, destroying the table would generally be considered a bad idea. So iād alter it. Instead of DROPPING a column, ADD a column. (See link in post 14)
Thanks again.
Would this be valid:
ALTER TABLE users
ADD time_date (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
dt DATETIME
);
ā¦no.
Why are you trying to add an integer and a datetime?
ALTER TABLE users
ADD time_date DATETIME;
thanks again
what happened when you tested it? ā¢
@ChrisjChrisj,
Is it possible to copy and download the table?
I would prefer to use PhpMyAdmin locally to make changes because invalid SQL statements have no effect on the table.
Also try the āformatā option because invalid script is highlighted.