I am working on a function that compares the date created and date modified of images and return the status of each case with PHP + MySQL. However, I realized that the data i'm trying to compare both end up using the CURRENT_TIMESTAMP in MySQL so whenever they are updated they end up having the same dates.
Is there a way to just only save the first date the data is inserted into the database (date created) so it doesn't change based on date modified?
Any help is appreciated, thanks in advance!
UPDATE:
my timestamp columns are configured using "DEFAULT CURRENT_TIMESTAMP" not the "ON UPDATE CURRENT_TIMESTAMP" option. Any other work arounds?
UPDATE2:
Please see below for my table definition.
CREATE TABLE IF NOT EXISTS `images` (
`id` varchar(50) NOT NULL,
`patientid` varchar(8) NOT NULL,
`caseid` varchar(25) NOT NULL,
`image_name` varchar(256) NOT NULL,
`status` int(1) unsigned NOT NULL,
`comments` varchar(4000) DEFAULT NULL,
`mod_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Seems like your timestamp columns are configured with "ON UPDATE CURRENT_TIMESTAMP" option, which automatically updates them.
As there does not seem a way to change this on a column, you have to create a new column without that option.
See the TIMESTAMP manual for details visit timestamp-initialization
Related
I tried running below code in sql command, but the code line start_date date DEFAULT CURRENT_DATE had issues, got no idea why. I needed only date.
But if I replace that with start_date datetime DEFAULT CURRENT_TIMESTAMP
the SQL will run but the idea is I only need to store date and not datetime.
How do I do it without using timestamp and latter converting to only date?
CREATE TABLE advertisement (
id int not null AUTO_INCREMENT,
PRIMARY KEY(id),
summary text DEFAULT null,
featured_image varchar(50) DEFAULT null,
start_date date DEFAULT CURRENT_DATE,
end_date date not null,
link text DEFAULT null,
added_date datetime DEFAULT CURRENT_TIMESTAMP,
updated_date datetime ON UPDATE CURRENT_TIMESTAMP
)
The documentation is quite clear that this works for datetime columns but not date columns:
This means, for example, that you cannot set the default for a date
column to be the value of a function such as NOW() or CURRENT_DATE.
The exception is that you can specify CURRENT_TIMESTAMP as the default
for TIMESTAMP and DATETIME columns.
If you really have your heart set on ignoring the time component, you will have to use a trigger to set the value, rather than a default constraint.
In this way I was able to use CURRENT_DATE function. This code is 100% tested. Note, date value of that instance of point when new row is added is saved.
I used following, SQL first:
CREATE TABLE advertisement (
id int(11) NOT NULL AUTO_INCREMENT,
summary text DEFAULT NULL,
featured_image varchar(50) DEFAULT NULL,
start_date date DEFAULT NULL,
end_date date NOT NULL,
link text DEFAULT NULL,
added_date datetime DEFAULT CURRENT_TIMESTAMP,
updated_date datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
)
and then during adding SQL I used query as
INSERT INTO `advertisement` SET
start_date=CURRENT_DATE(),
end_date='2018-04-02',
added_date='2018-03-01 00:10:33'
Thank you all for your suggestion.
as of MySQL8 this is now legal,
CREATE TABLE t (
d DATE DEFAULT (CURRENT_DATE)
);
note however that the "extra" parentheses is IMPORTANT, because, for unknown reasons, this is somehow illegal:
CREATE TABLE t (
d DATE DEFAULT CURRENT_DATE
);
I know there are many examples of how to use this as well as links to the MySQL documentation. Unfortunately, I am still a in need of clarification on how it actually works.
For instance, The following table structure (SQL code) is one example of what I need to use the INSERT ... OR UPDATE:
CREATE TABLE IF NOT EXISTS `occt_category` (
`category_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`parent_id` int(11) NOT NULL DEFAULT '0',
`top` tinyint(1) NOT NULL,
`column` int(3) NOT NULL,
`sort_order` int(3) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL,
`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
ALTER TABLE `occt_category` ADD PRIMARY KEY (`category_id`);
ALTER TABLE `occt_category` MODIFY `category_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
What I am attempting to insert into this mess are new categories from an API source so there are definitely duplicates.
What I am getting from the API is the following:
[
{
"categoryID": 81,
"name": "3/4 Sleeve",
"url": "3-4sleeve",
"image": "Images/Categories/81_fm.jpg"
}
]
So given the above information; Do I need to change my table structure to check for duplicates coming in from the API?
In MSSQL I would just simply do an IF EXISTS .... statement to check for duplicates. Unfortunately, this is MySQL :(.
If you intend to make use of the INSERT ... ON DUPLICATE KEY UPDATE MySQL Syntax (which is what I understand from your question, as INSERT ... OR UPDATE is not a real MySQL command), then your current table structure is fine and you will NOT have to check for duplicate records.
The way this works is that before writing any new records into your table, the MySQL DB will first check to see if there are any records that have a value in a PRIMARY or UNIQUE key-field (in your case category_id) that is the same value for the corresponding field in the incoming record, if it finds one, it will simply update that record as opposed to writing a new one.
You can read more about this syntax here.
I am developing a system that includes a log history. In the log history is the record of actions taken by every users. Someone suggested that I have to compress the data annually because the database is expected to grow and so that I can save space. I have this table:
delimiter $$
CREATE TABLE `actionhistory` (
`historyID` int(11) NOT NULL AUTO_INCREMENT,
`empID` int(11) NOT NULL,
`item` varchar(50) NOT NULL,
`actionTaken` varchar(15) NOT NULL,
`type` varchar(15) NOT NULL,
`dateActTaken` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`historyID`),
KEY `emp` (`empID`)
) ENGINE=InnoDB AUTO_INCREMENT=429 DEFAULT CHARSET=latin1$$
What is the syntax in doing it? I want to do it annually regardless of how many rows it has per year.
EDIT: I have read about using ROW_FORMAT, KEY_BLOCK_SIZE but since the row per year is not constant, I didn't use it.
You have to take out Year (Y) form field dateActTaken to identify which year is it. Once you have year then you can fetch all records belonging to that year.
As far as compression, you can take it in zip file or you can save yearly data in your other mining databases.
This is the query I have used for creating the table
CREATE TABLE IF NOT EXISTS `logging_api_request_js` (
`id` int(30) unsigned NOT NULL AUTO_INCREMENT,
`log_message` longtext,
`level` varchar(10) DEFAULT NULL,
`ip_address_merchant` varchar(45) DEFAULT NULL,
`ip_address_customer` varchar(45) DEFAULT NULL,
`creationTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11
It showed an error Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
When I googled I saw that in the mysql 5.6. version this issue / restriction has been took off.But i wont be able to upgrade mysql right now.
Is there any work around for this,whith out changing the table structure to dump this using mysql?Please help.Im having little knowledge about the db operations.Do help.Thanks
You could remove ON UPDATE CURRENT_TIMESTAMP and create trigger.
DROP TRIGGER IF EXISTS `update_logging_api_request_js`;
DELIMITER //
CREATE TRIGGER `update_logging_api_request_js` BEFORE UPDATE ON `logging_api_request_js`
FOR EACH ROW BEGIN
SET NEW.updateTime = NEW.creationTime;
END
//
DELIMITER ;
I'm trying to make a table that has two timestamps columns, one will be for when a row is created and the other for when the row is updated. Here's what I tried so far:
CREATE TABLE `tmp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` timestamp NOT NULL ,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
But I'm getting this error:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
You can do this in MySQL 5.6 (available at the time of writing as release candidate but not yet production-ready).
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table.... For any TIMESTAMP or DATETIME column in a table, you can assign the current timestamp as the default value, the auto-update value, or both
taken from https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
This is a MYSQL constraint, You can have only one column whose default value will be the systime.
This question can also be referred
How to add "ON update current timestamp" to existing table column
You could write a trigger to add the created timestamp on inserts seperately
delimiter |
CREATE TRIGGER add_created_ts BEFORE INSERT on `tmp`
FOR EACH ROW
BEGIN
SET NEW.created = current_timestamp;
END
|
delimiter ;