Sorting by time not working truly in MySql - mysql

I think title is clear what is my problem. I am working with MySql I just wanted to know what was the first registration on my website and when I sorted the table according to timestamp, the id of people who registered on the website were not true. The Image bellow will show what do I mean:
and this is the code what phpMyAdmin created:
SELECT *
FROM `table`
ORDER BY `table`.`timestamp` ASC
LIMIT 0 , 30
whats wrong with my table time? and why id is not ordered same as timestamps

If we assume that id is an auto-increment field, then this is almost certainly due to the fact that timestamp column is not assigned at the same physical time the id column is assigned. For example, if you have a multi-page registration from a website application, the initial object may be inserted into the database to contain the initial data fields gathered on the first page. But perhaps the timestamp is only set after the final page of registration is complete. So depending on the time the user takes to complete registration, the timestamp can be out-of-sync with the id order.
There are multiple ways to work around this. One way is to assign timestamp when the record is first inserted into the database, or you could use a DB insert trigger to populate the timestamp field instead.
There is also the possibility that timestamp represents the date and time the user last modified their profile (or otherwise caused modification of the database record). In this case, the timestamp could be changing to a "future" value while the id stays constant.

Related

Is there a way to know the number of records added into the SQL database after a particular date and time

The table doesn't have any date time column. I want to if there is any inbuilt keyword which can does that.
I want to know all commits done after a particular date.
If flashback is enabled on the database you can get records on the table in an around a particular date range in Oracle.(It purely depends on if its enabled and for how long the flashback needs to be kept)
You can query to see the data in the table as of 3 days back as follows
select *
from table as of timestamp sysdate-3

How to build database logic?

I have got a form with 3 input fields (date, time, telephone) and I need to build a database where I can select date and time from drop down menu (two separate tables where one has got date values another one has got time values).
Both of these date and time tables have another column with value 0 or 1. If this value is 0 you can select that time, but if the value is 1 it can't: e.g. I select the date and it shows me available times, so then I can add a telephone number and submit all values to another table (date, time, telephone I guess) - but if the time is already taken it can't be selected.
If all time slots are selected already you can't select that date because the date value changes from 0 to 1.
It's my first database project so I don't really understand things in database architecture yet. I don't need the code I'll figure it out, I just need to see how the database looks with all the tables, primary keys etc.

New records since last query in MySQL View

I am looking for a way to create a View that when queried will automatically only retrieve new records since the last query. My tables have a timestamp field for all entries, so for a simple example I can
SELECT * WHERE timestamp >= 'blah'
but I don't know how to determine what blah should be from the last query. So if the View was queried at 11:00 and then again at 12:00, the query at 12:00 should only return records added since 11:00. And so on... This all needs to be accomplished in the View, the end user should simply be able to query the View and get the results.
Is this possible?
There are two ways:
Store last access date time in database per user persistent session
table, if you have one. On next view call to database, use the
previous latest access time in the session to filter rows starting
from.
Store last access date time in user virtual session at client
environment. On every call to server, send last access date time as
well. So that server uses it to filter rows starting from.
I prefer to use second option that process won't write any data in database tables.
As there may be an unread record that slips through undetected (say it came less than a second since the last one accessed, so it has the same timestamp), set a column to auto increment (typically labelled id) and check for entries using it e.g. in PHP save the last accessed record in a $lastId variable, and use:
$sql="SELECT * WHERE `id` > '$lastId'";

SQLite logic error while retrieving person name

I have a doubt regarding the sqlite command that should be written for getting the name of MIN(count) and MAX(timestamp) and after retrieving, timestamp should be set for current timestamp amd and count should be incremented by 1.
For the first time, the table will be updates sa follows -
and for the next time, i have to update the timestamp for lil to current timestamp. Then for the third time, jil's timestamp must be updated.
I have tried to work with this, but I am getting logical errors, the table is not getting updated as per the requirement. How can I implement this?
I have solved the problem by making changes in the database. Initially, I have set the timestamp in descending order. then I wrote command to to display name with min(count)and min(timestamp) and updated in the same order. This worked for me.

Timestamp is generated for next record, as opposed to current record

I have a slight issue with MS-Access, which is as follows.
I have a table with a Timestamp column (the format of the field is Date/Time, the default value is Now()). The issue is that whenever I create a new record, the timestamp is set for the next record I am going to create, as opposed to the record I am creating.
This means that I create record 50, and the Timestamp is set for record 51. If I come back a week later, and create record 51, the Timestamp for record 51 will be a week out, and the timestamp will be set for record 52, which I will be creating at some point in the future.
You can re-create the problem by firing up MS-Access, creating a new table with a couple of fields, one of which is Date/Time and setting the Default Value of this field to Now().
Is this by design, or am I doing something dumb? If it is by design, how can I implement the type of Timestamp that I want (one where the Date/Time is set as the record is created) in MS-Access? If I am doing something dumb, what exactly am I doing?
Edit: Below is a screenshot of a newly created Access table:
I add some text to record one, the Timestamp gets set for record two:
I allow some time to pass, put some data into record two, and the timestamp doesn't change, and now record three has a timestamp:
If I close and open the table, the Timestamp for the (New) record gets updated to whenever I opened the table:
I allow some time to pass, update the record and the Timestamp stays at the time I opened the table:
As is already revealed in the comments, this problem comes from editing in the table with Now() set as the Default for your TimeStamp field.
I suggest you create a form instead of editing in the table. If you want it to look similar just use a datasheet form. Then on the Form's BeforeUpdate event put code in like this:
Me!TimeStamp = Now()
As a side note, I wouldn't use TimeStamp as a field name. Some RDBMS such as SQL Server have a data type called TimeStamp. It's best to avoid using field names that are data types or reserved words. Moving an Access database to SQL Server is extremely common and you could have problems when you try to do it.
Instead, I would create two fields. One called DateTimeEntered and another called DateTimeModified. I consider these two fields to be necessary in pretty much every table I make. If you ever want to do any kind of synchronization of records you'll wish you had at least a DateTimeModified field.