Can I use 2 date type in 1 mysql table? because for some reason when I insert records for 2 different dates it doesnt work, only the first date is working the other one shows 00-00-00?
Thank you for your time and help
I will assume you are using a TIMESTAMP data type, and you want it to default to CURRENT_TIMESTAMP.
Only one TIMESTAMP column in a table can automatically be set on INSERT or UPDATE - and by default, MySQL goes with the first one.
If you would like to have more than one TIMESTAMP column get updated automatically on INSERT or UPDATE, you will need to turn to triggers.
The docs on the TIMESTAMP data type are a good read!
Edit: if this educated guess does not answer your rather vaguely-phrased question, you will probably want to update your question with an example of the queries you are running, and what exactly is happening/not happening afterward.
Related
I need to be able to set a Null value in DB Oracle if the user wont choose a date.
$createdon = date('m/d/Y', strtotime($_POST['createdon']));
INSERT INTO purchase (CREATEDON) VALUES(to_date('$createdon','mm/dd/yy'))
I am able to insert date if a user choose a date but if the user wont the date that is save on the database is "01/01/1970". Also, if i dont use "to_date" tag i cant insert record on the database.
Any suggestion on what should i do and how?
Thank you.
TO_DATE is OK because - if you're passing a string (which represents a date value), it is a preferable way so that YOU have control over it. Because, if you pass '01.02.20', which is which? Is 01 day or month? Is 02 day, year, ... month? Who knows. Don't depend ond defaults. So, if you say to_date('01.02.20', 'rr.mm.dd'), then everyone (Oracle included) knows what to do.
As of 01/01/1970: if you insert null but still see that value in a column, it means that
column has a default value
database trigger inserted it
Check both of these on that table.
I need some help about database management. I am trying to retrieve data from my database (filtering them by the created_at field).
There will be no problem when I am retrieving data from my database created in today's date.
For example today is 4/17. When I run the insert function today, the value for created_at will be 4/17 as well. So when I go to my web page and display data for 4/17, the data will be right.
But let's say I forgot to fetch data for 4/15, and I need to fetch those data today. When I insert these data in my database now, the created_at will be 4/17, but the adjacent data is actually for 4/15.
Now, when I go to my web page and display data for 4/15, I will get nothing.
As a workaround, I added a date field in my table, and this will contain a specified date, unlike the created_field that takes the server's date. I now use the date field to filter the data in my web page.
However, I think this is somewhat redundant or inefficient approach. Does anyone have any suggestions?
Here is a screenshot of my current table structure:
The accepted answer solves the XY problem. It's probably not the way to solve the actual problem.
There are lots of reasons for putting the current datetime into a database (rather than a datetime which is intrinsic in the data such as an appointment, or a date of birth). While this shouldn't be used for auditing purpose is it is handy for debugging and for dealing with optimistic locking.
But here, you seem to be looking for a transaction control mechanism - a way of identifying what records have been subjected to some action. If it is important to maintain a record of the sequence in which the records were processed, then a date, or even a date time, or even a millisecond timestamp may not be adequate. What happens iwhen you need to apply this operation more than once per day? What if it fails half way through and you need to resume the operation? This mechanism also precludes the notion that there may be more than 2 stati for a record.
Assuming the thing which is being done with the record is part of an ACID transaction, then there are 2 states to consider - before and after. Your data domain should explicitly describe those two states (using a null/non-null date value is merely implicit). If the transaction is not atomic then there will likely be more states to consider.
In the case of MySQL I would implement this as an enum datatype (with a non-null constraint). But more generally I seek to avoid a situation where data is being updated like this by using synchronous operations wrapped in transactions.
Since you are using Laravel, you can simply override the created_at value when creating your model. So for example, you can do the following:
$myModel->created_at = Carbon::parse('2019-04-15');
$myModel->save();
This would set the created_at value to April 15th, not today. Hence you don't need a second date column in your table.
UPDATE
Nonetheless, if you need the time part to still reflect the current time, you can do the following:
$myModel->created_at = Carbon::now()->setYear(2019)->setMonth(4)->setDay(15);
$myModel->save();
i'm new to databases. I have just created my first database, so far so good. Now I looking to add time in to this.
I have certain table, when it's displayed I need it to show the table info + the date that info was added. Also possible the difference in between (5day 5hours ago) or something of the sort.
Do i need to add another column to the data table where the time will be stored? If so is there a way to auto increment that to current date? How do I go about doing that?
Not really sure where to start can someone please point me in the right direction perhaps a tutorial somewhere? Cant seem to find anything solid on this topic
Thanks a bunch!:)
For each record in your DB, you want to know when it has been created?
You have to add a column of type "timestamp". You can then set the default to "current_timestamp".
Note also that you can use the "on update current_timestamp". Instead of saving current time on insertion, it will do so each time your record is updated.
If you need the 2 dates (created_at and updated_at), you'll need 2 columns. EDIT : well you can't :p
There is a datetime as well as a timestamp field. If you use a timestamp field you can have the field automatically populate with the timestamp of when the row was inserted.
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
I suggest trying out http://sqlzoo.net/ for a gentle introduction to SQL.
I'm looking to order certain uploads by time order arrived (descending), but I'm not sure how I would record the time and date of submission using PHP/SQL. I'm pretty new to this, learning by coming up with projects and working through them as best as I can. Thanks for whatever help you can give me.
EDIT: I understand that the those functions exist, I just have no idea how I would implement them.
MySQL supports TIMESTAMP fields that can be automatically updated when the record is updated.
When you specify DEFAULT CURRENT_TIMESTAMP, the current time is inserted in new records. When you specify ON UPDATE CURRENT_TIMESTAMP as well, the timestamp is updated when the record is updated. That way, MySQL can automatically log the time for you.
You can do this using MySQL's NOW function.
Also see MySQL's Date and Time functions : http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
You have to create a column for saving the date and time.
I often use the following two columns:
created DATETIME DEFAULT NOW(),
modified DATETIME DEFAULT NOW()
That's all. If you insert a new record, MySQL will automatically update created and modified to the current date and time.
And if you want to order all uploads by their creation date:
SELECT * FROM uploads ORDER BY created DESC
Also see: http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html#1_5
I'm using a mysql_query() in PHP to insert a value into a table in a database. One of the attributes of that table is "timestamp" which is supposed to be the current time that the query was made. How can I generate this? I tried using "time()" in PHP, but this is not inserting correctly in the query. When I insert what the time() function generates, it shows up as a default in my table, as: "0000-00-00 00:00:00". How can I generate the current time in order to input it into my table?
In your query, you need to set the value of that field to NOW() which is MySQL's function for getting a timestamp of the current time.
If you put your actual query in your question, I can help you with a more specific answer ;)
insert into table (datetime_field) values (now())
php function time() returns a unix timestamp that it's an integer value.
Go to php.net and view the codes for getting date and time and check whether you typed the codes correctly. If you did, then check if the format you are using in your code is similar to the one you are using in your table.