How to apply index for datetime field in sql - mysql

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.

Related

convert multiple epoch time to date in sql together

I am having`a whole mysql table of approx 40,000 rows with a column named 'epoch_time' and there is epoch time in it and I want to convert that whole table's 'epoch_time' to a equivalent 'date' together in a single sql query and I'm doing this is in php_my_admin . Thanks in advance.
I guess by epochtime you mean UNIX-style timestamps, that is, number of seconds since 1970-01-01T00:00Z. If my guess is wrong, so is the rest of my answer.
First you add a new column to the table.
ALTER TABLE mytable ADD COLUMN datestamp DATETIME AFTER epochtime;
This names the new column datestamp and puts it right after epochtime in the list of columns.
Then you update the whole table to populate the new column from the old using FROM_UNIXTIME(). Omitting the WHERE clause makes the update work on the whole table (careful!).
UPDATE mytable SET datestamp = FROM_UNIXTIME(epochtime);
Finally, if you wish you can drop the old column.
UPDATE TABLE mytable DROP COLUMN epochtime;
If I were you I'd try all this on a copy of your database to ensure it is correct before doing it on your production database.
If your epochtime values already have the TIMESTAMP data type, they are already stored internally as UTC (f/k/a GMT) times. The update operation I suggested will convert them to local time according to the settings on your server.
If your application has users in multiple time zones, you may wish to keep using the TIMESTAMP datatype: it honors time zone settings. If your epoch times are stored in an INT column, you can create your new column with the TIMESTAMP rather than DATETIME type by substituting this line for the first one in my instructions above.
ALTER TABLE mytable ADD COLUMN datestamp TIMESTAMP AFTER epochtime;

MYSQL query disallowing a timestamp column having two values from the same date

For example if a user inserts '2017-03-13 12:16:18.0' into the timestamp column,
the same user should not be allowed to enter another value in this column IF IT'S ON THE SAME DAY i.e 2017-03-13 (in this case). Or ultimately, update the timestamp column with the previously inserted value ('2017-03-13 12:16:18.0') each time the user tries to insert a timestamp date twice ON THE SAME DAY. I hope I've been explicit enough.
Below is a non-functioning query I came up with, but it shows what I would like the query to do ultimately. Thanks for your help and feedbacks.
INSERT INTO hr.entry(id,entry_time)
VALUES (45,
CASE WHEN '13-03-2017'= CAST(SYSDATE() AS date) THEN
(UPDATE hr.entry
SET entry_time =
(SELECT entry_time
FROM hr.entry
WHERE id=45
AND CAST(entry_time AS date)= CAST(SYSDATE() AS date) )
ELSE
SYSDATE());
You could add a DATE column to your table, and add a unique index to that column. Then, when you insert the timestamp into the timestamp column, you could also insert the date from that timestamp into the DATE column. Attempts to insert a timestamp whose date component already exists in that table would cause MySQL to throw an error.
I think you are going to need a trigger, unless you store the timestamp as a string using YYYY-MM-DD HH:MI:SS format. I don't really recommend that.
So, create a trigger that updates a column called timestamp_date. This simply extracts the date part of the timestamp.
With this column, you can define a unique index:
create unique index entry_userid_timestampdate on entry(userid, timestamp_date);
This will then enforce your condition.
If you decide that you want to store the timestamp as a string, you don't need the trigger (although will need to manually set the "timestamp"). Instead, you can use a prefix:
create unique index entry_userid_timestampstr on entry(userid, left(timestamp_date, 10));

mysql - convert unix timestamps to datetime and how add other columns to the query?

mysql - I have a table , a column of unix timestamps called time, which I have already changed to datetime format, but I can't select other columns of my table.
To change the unix timestamps I have used the following statement:
SELECT
from_unixtime (time)
FROM
calllog
and changed to datetime format successfully, but when I add other columns to the select query the result is 1 in all the time columns. What can be done?

conversion of column datatype from varchar to Date in sql query

i have a column whose datatype is Varchar and i use a select query which fetch the data from given range based on that coloumn.now i want to convert this column from varchar to date so that my query works fine. i am using Mysql as backend database . i dont want to change the column datatype to Date in Mysql Schema but change at run time through some sql function
Use CAST()
http://forums.devshed.com/firebird-sql-development-61/casting-a-varchar-to-date-477361.html
http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html#function_cast

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.