Unix Timestamp in MySQL phpMyAdmin - mysql

is there any possibility that there is a field called 'time' in the table and there are 3 more fields, and when I insert the 3 fields, the time field is set default to fill in the current UNIX Timestamp? It will be much appreciated if you help me out!

I just inserted the timestamp from PHP to the table.

exactly there is no any default unix_timestamp for mysql. you can set the field as a datetime or timestamp and set its default to CURRENT_TIMESTAMP. then use strtotime function to change back to unix. or set type to int or bigint ordouble and use time() function on every insert row.
I prefer myself to use second way.

Related

How to apply index for datetime field in sql

I am trying to delete the record from the specific date range .
So I have used the below query
delete from `table_name` where `date`<1580947200
I have used datetime as data type for the date column
It takes long time to execute and sometimes it gets stuck while executing this query.
Can anyone say how to apply index for this query
Your issue is not indexing. Your issue is that your telling MySQL to numerically compare a non-numeric (DATETIME) column against a numeric value (Unix timestamp).
i have used datetime as datatype for date column
This means the column is a DATETIME column. But;
1580947200
looks to me like a Unix Timestamp value.
Unix Timestamp is not the same as a MySQL DateTime entity.
Therefore; you need to CAST these two types to the same for the comparison; To do this you can use the UNIX_TIMESTAMP() MySQL Function.
DELETE FROM `table_name` WHERE UNIX_TIMESTAMP(`date`) < 1580947200
If you do wish to add an index to the date column you can read this Q&A.

MySQL date column auto fill with current date

I have a table where I have a date column. Is there a way for MySQL to auto fill this field whenever I insert a new registry with the current date? Or is this made automatically by default?
P.S.: I'm using PHPMyAdmin
Although it is an old post, maybe this image will help as it is more explicit:
(For phpMyAdmin users)
This configuration sets that field with a value like:
2015-12-11 07:50:47
PS: Note that the timestamp will set the time OF your server!! (i.e. the example above got the time from Pacific Time (07:50:47) but it could have been from a Spanish user at 16:50:47 local time) Keep this in mind.
Also, if you already have a "Created Date" you might need another column that updates the modification date whenever there is an update:
You only need to set on update CURRENT TIME STAMP in Attributes Field.
Ready to rock!
Set Default to in your mySql query
CURRENT_TIMESTAMP
you have to use
now()
function where you want to fill current time.
i.e.:
INSERT INTO user_rights (`user_id`,`right`,`group_id`,`created_date`) VALUES ( '42', '160', '1', now());
I realize this may not be a direct answer to the question but I do believe this is the most useable solution.
I highly recommend using a DATETIME or TIMESTAMP data type for the column in question.If you are utilizing a fairly current version of MySQL, MySQL will do the work for you.
Details:
To be very clear, as of 5.6.5, for both the TIMESTAMP & DATETIME datatypes, you can do the following:
Set a DEFAULT value of the current date & time (using NOW() or one of its aliases such as CURRENT_TIMESTAMP)This means every time you insert a new row into this table a TIMESTAMP or DATETIME column with this default will get the current date and time
Set an ON UPDATE constraint that will UPDATE a column to the current date & time when, (you guessed it) the row is updated
Here's how:
An Example in a CREATE TABLE statement:
CREATE TABLE t1 (
ts1 DATETIME ON UPDATE CURRENT_TIMESTAMP
,ts2 DATETIME DEFAULT NOW()
);
Please note that DATETIME can be replaced with TIMESTAMP for effectively the same functionality.
Additionally I suggest the use of the DATETIME data type over TIMESTAMP as DATETIME has a much larger range of dates it can support. It's worth mentioning that TIMESTAMP is smaller for those few cases that matters. For more details please read my answer here: https://stackoverflow.com/a/26117532/1748266
I have added this to my table and it works
ALTER TABLE Medewerkers ADD med_created TIMESTAMP DEFAULT now();
When you insert data into your record it update automatically the med_created
MySQL unfortunately doesn't allow specifying values other than constants as the default for columns other than TIMESTAMPs.
This is a feature available in MySQL versions 8.0+, but for older versions the only solution for a database defined default would be to use a trigger.
You can do something like this from the SQL screen
ALTER TABLE `table_name` CHANGE `created_at` `created_at` TIMESTAMP NOT NULL

MySQL: Trying to insert a value in a timestamp throws an error

I have a table with this column:
last_modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
And it looks like I can not insert a row with a custom timestamp, I get this error:
Incorrect datetime value: '1145868501' for column 'last_modified' at row 1
I am trying to populate this table with data coming from another table, that other table only has a creation_time field which is a DATETIME so I use UNIX_TIMESTAMP(creation_time) to populate the timestamp.
I think the timestamp column with "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" prevents me from inserting my own stuff, am I right? If yes where is the official doc about that, and what is the best solution? Creating a simple timestamp first then alter the table after inserting data?
Thanks!
EDIT: since people are advising me to not use UNIX_TIMESTAMP, I have to say that I didn't want to use that at the beginning, but I got this kind of error:
Incorrect datetime value: '2010-03-28 02:15:51' for column 'last_modified'
So I thought I had to insert a "real" timestamp...
You can explicitedly insert a value in a TIMESTAMP column. Read: TIMESTAMP Properties
The auto-update TIMESTAMP column, if there is one, is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. If all other columns are set to their current values, the TIMESTAMP column does not change. Automatic updating does not apply if the TIMESTAMP column is explicitly assigned a value other than NULL.
Update
Hehe, the error occurs because - well- there was no datetime with '2010-03-28 02:15:51'! This was in the daylight saving time gap (which usually appears some day in March, between 02:00 - 03:00 or 03:00 - 04:00.
See: Daylight Saving Time explanation.
You're trying to put a long integer into a datetime field. That doesn't work. Remove the call to UNIX_TIMESTAMP() and it should work.
The MySQL TIMESTAMP type is almost identical to a DATETIME; it just has some extra auto-update magic. As far as SELECT and UPDATE is concerned, it is a DATETIME.
If the column is always auto-updated, you can remove the property, getters and setters from the Entity.
Doing this way, it will be ignored in all queries.

Which is the correct datatype to store time in SQL?

Here's my table right now (using mysql):
SQL Table: koko_table
name varchar(140)
status varchar(140)
time TIMESTAMP
My issue basically is , I have a form (using php) which user uses to store data only in the status column, my time column captures the time as a permanent data when the user stores in the status column. I think I have not been using accurate DATATYPE for time column, because everytime I visit my database, the time column has different values.
What can be the correct datatype to store time of status input by the user as un-changeable data.
The best data type would be DATETIME.
You can use either TIMESTAMP or DATETIME in MySQL to store date and time.
There are differences though:
TIMESTAMP uses 4 bytes, DATETIME 8 bytes.
Timestamps can be between 1970 and 2038, while datetimes can be between 1000-01-01 00:00:00 and 9999-12-32 23:59:59.
TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the (server's) current time zone for retrieval. This does not happen for DATETIME values where no timezone is implied.
Datetime fields have to be declared in INSERT operations while timestamp fields have the special feature that the first timestamp of a table is (by default) automatically inserted or updated at every INSERT or UPDATE operation with the current timestamp. (That's probably what you are seeing in your scenario.) You can change this behaviour, so only Inserts or only Updates set the timestamp to current timestamp. See MySQL docs: Timestamp properties
To have for example the timestamp automatically stored at Inserts but not changed during Updates, you could set:
ALTER TABLE TableName
CHANGE TimeStampName TimeStampName TIMESTAMP DEFAULT CURRENT_TIMESTAMP ;
I'd prefered DATETIME (the long number) or store it as a formated string like 2011/10/18 13:50. The first one is better in performance and the secound one is easyer to edit on phpMyAdmin or something like that.

Default date in mysql

i am facing issue while creating table where in that one column which is set to default value of current date........i would stress this point "that i need only date not time along with that"....i would be really thankfull to those who tries to help me....
Instead of trying to get MySQL to insert the current date for you when a row is created automatically, you can set it to NOW() in the INSERT query. Or, in the code for your software, you can set the date to the current date in the INSERT query.
MySQL does not support a default of the current date for DATE or DATETIME.
Check out this set yuor field to a 'Date' not 'DateTime'
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
The DATE type is used when you need
only a date value, without a time
part. MySQL retrieves and displays
DATE values in 'YYYY-MM-DD' format.
The supported range is '1000-01-01' to
'9999-12-31'.