Unable to create a table in database - php

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 ;

Related

PDO query updating a datetime column not in query Part 2

Continuation from this question:
PDO query updating a datetime column not in query
A column in my table called lastLoginDate was being automatically updated even though my prepared statement did not include said column.
Apparently, when I created the new column, a trigger was set.
Upon using the command SHOW CREATE TABLE table_name, I returned the following results:
CREATE TABLE `users_edi` (
`username` varchar(30) NOT NULL DEFAULT '',
`fullname` varchar(50) DEFAULT NULL,
`userlevel` tinyint(1) unsigned NOT NULL,
`ipaddress` varchar(30) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`entrydate` datetime DEFAULT NULL,
`division` varchar(35) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`userid` varchar(32) DEFAULT NULL,
`timestamp` int(11) unsigned NOT NULL,
`job_title` varchar(30) DEFAULT NULL,
`dept` varchar(50) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`lastLoginDate` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, // <-- here
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The table is years old. I just created the column and somehow, a trigger was set to it (I guess).
Regardless, I tried to remove it using the following command:
ALTER TABLE `users_edi`
`lastLoginDate` datetime DEFAULT NULL
But I only get the following error:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`lastLoginDate` datetime DEFAULT NULL' at line 4
How do I remove this trigger using the ALTER TABLE command or any other command?
ALTER TABLE users_edi MODIFY COLUMN lastLoginDate DATETIME DEFAULT NULL;
You might like to read this page on ALTER TABLE: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

Nothing changes when i try to add Check Constraint in my Table

I am trying to add check constraint in my table that prevents adding more data into a table if the sum of rows shop_id is greater than 3. I have written the following code and its just not working. Kindly check this and guide me.
ALTER TABLE kinect_temp_data
ADD CONSTRAINT my_const CHECK (sum(distinct(shop_id))<3)
The above query runs successful,but it does not create any effect and i can still able to add more rows, and when i query this, it display that no check constraint was added.
SHOW CREATE TABLE kinect_temp_data
Output
CREATE TABLE `kinect_temp_data` (
`cart_number` int(11) NOT NULL AUTO_INCREMENT,
`product_id` varchar(50) NOT NULL,
`shop_id` varchar(50) NOT NULL,
`product_name` varchar(50) NOT NULL,
`item_number` varchar(50) NOT NULL,
`image1_path` varchar(50) NOT NULL,
`image2_path` varchar(50) NOT NULL,
`image3_path` varchar(50) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`cart_number`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
Kindly check this and guide me what i am doing wring here.
Thanks.
MySQL don't support check constraints -- they are ignored.
But you can use BEFORE INSERT and BEFORE UPDATE triggers to realize such functionality.
There is good explanation

Datetime and Timestamp Issue while creating MYSQL Table

I am trying to create mysql table in PhpMyAdmin in Hostgator server with the following information but it is showing error
Error : #1067 - Invalid default value for 'CreatedDate'
Table
CREATE TABLE `tbl_sample` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Domain` varchar(100) DEFAULT NULL,
`ClickUrl` varchar(600) DEFAULT NULL,
`CreatedDate` datetime NULL DEFAULT now(),
`ModifyDate` timestamp NULL DEFAULT NULL on update now(),
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The purpose of table is I need to enter only 'Domain' and 'ClickUrl' data using insert command remaining Id(autoincrement),CreatedDate(current date when inserting row),ModifyDate(when update the row) will automatically insert.
The above table is executed successfully in mysql environment in my local system but it is not executing in the mysql environment in hostgator
Check this link]1
Problem with creating Two column with timestamps. have to use trigger to get it done.

PHP/SQL Date Created vs Date Modified

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

AUTO_INCREMENT in two places

I came across following sql statements and you can see that AUTO_INCREMENT is in two different places. Can you explain the different, I know the first one is auto incrementing id. But what does the second one mean?
CREATE TABLE `categories`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`image_path` varchar(200) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB;
Second statement.
CREATE TABLE `categories`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`image_path` varchar(200) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT=4 ;
I referenced http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html. But I couldn't find anything.
The AUTO_INCREMENT in the second statement sets the first number to be used in the id at 4.
`id` int(11) NOT NULL AUTO_INCREMENT
Sets the column name and tells the DB to auto increment the number when a new row is added.
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT=4 ;
Sets the Engine used for the table, the charset and that it should start numbering at 4, not 1.
CREATE TABLE explains this in a bit more detail.
AUTO_INCREMENT
The initial AUTO_INCREMENT value for the table. In MySQL 5.0, this
works for MyISAM and MEMORY tables. It is also supported for InnoDB as
of MySQL 5.0.3.

Categories