INSERT works in SQLite but not MySQL - mysql

This table I created in a SQLite database:
CREATE TABLE [tickets] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER NULL,
[size] FLOAT NULL,
[phone] VARCHAR(10) NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT NULL
);
Now I convert this table in mysql
CREATE TABLE tickets (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER NULL,
size FLOAT NULL,
phone VARCHAR(10) NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT NULL
);
INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
When I am inserting those values I got an error ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
I am able to insert all those values in SQLite, but I am not able to insert all those values into MySQL. Help me?

You might want to convert
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
to
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and concatenate the values from SQLite, or alter their type
date date DEFAULT NULL,
time time DEFAULT NULL,
and add the current date/time with a trigger.

As far as I know, MySQL will not allow this. If you want to insert the current time into the second field, consider using a trigger to update the record when inserting.
DELIMITER $$
CREATE TRIGGER curtime BEFORE INSERT ON tickets FOR EACH ROW
BEGIN
SET NEW.time = CURRENT_TIMESTAMP();
END;
$$
DELIMITER ;

Related

Trigger for inserting and updating multiple records in the same table in MySQL

I have table
CREATE TABLE `seniority_master` (
`EMPNO` varchar(4) NOT NULL,
`NAME` varchar(40) DEFAULT NULL,
`GENDER` varchar(10) DEFAULT NULL,
`CATGRY` varchar(10) DEFAULT NULL,
`DOB` date DEFAULT NULL,
`DESG` varchar(40) DEFAULT NULL,
`DESGTYPE` varchar(100) DEFAULT NULL,
`SENIORITY` int(4) NOT NULL,
`EMPTYPE` varchar(45) DEFAULT NULL,
`TYPE` varchar(45) DEFAULT NULL,
`QUAL` varchar(200) DEFAULT NULL,
`BRANCH` varchar(30) DEFAULT NULL,
`DOJ` date DEFAULT NULL,
`DOCA` date DEFAULT NULL,
`PAYSCALE` varchar(45) DEFAULT NULL,
`OTHERDESC` varchar(45) DEFAULT NULL,
PRIMARY KEY (`EMPNO`,`SENIORITY`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `seniority_master` VALUES ('1389','MUNNA','Male','OBC','1959-10-31','SENIOR RECORD SORTER (ASS-I)','',2,'STAFF','P03','4th','AGRA','1982-07-01','2007-07-01','12100-24500','');
INSERT INTO `seniority_master` VALUES ('1391','RAJENDER SINGH','MALE','UR','1965-07-09','AM (GENERAL)','GENERAL',40,'OFFICERS','H1','INTER ','AGRA','1984-06-22','2013-09-05','16400-40500','');
INSERT INTO `seniority_master` VALUES ('1392','MADAN GOPAL','Male','OBC','1966-07-04','SENIOR OFFICE MANAGERS-I (GENERAL)','',6,'STAFF','L1','10TH','AGRA','1984-08-17','2015-04-17','15600-30500','');
In the above data the seniority(2,40,6) of the employee is going to change every time when the promotion takes place based on the type(P03,H1,L1) of the employee.
What we really need is a Trigger that updates the seniority of all the other employees for the type when one of the employee is updated or inserted or deleted.
Ex: Suppose empno 4620 has seniority 5 and type A1, when we delete the particular empno in seniority_master table, the seniority of below employees of the same type to be updated automatically by increment or decrement based on the seniority. Similar procedure for updation and insertion operations to done.
I am able to create AFTER INSERT and AFTER UPDATE triggers for my program requirement.
DELIMITER $$
CREATE TRIGGER after_seniority_insert
AFTER INSERT ON seniority_master
FOR EACH ROW
BEGIN
INSERT INTO emp_positions
( empno,
position,
dop)
VALUES
( NEW.empno,
NEW.desg,
NEW.doca );
END$$
DELIMITER ;
After update:
DELIMITER $$
CREATE TRIGGER after_seniority_update
AFTER UPDATE ON seniority_master
FOR EACH ROW
BEGIN
IF (old.desg != new.desg &&
old.doca != new.doca) then
INSERT INTO emp_positions
( empno,
position,
dop)
VALUES
( NEW.empno,
NEW.desg,
NEW.doca );
END IF;
END$$
DELIMITER ;
Help me out for above scenario for creating trigger.

MySQL -> Create table with only hours & minutes

I need to save only hours (FORMAT 24) in a column 'HORAINICIAL' of my table. I don't want YYYY/MM/DD.
CREATE TABLE IF NOT EXISTS `claritytractor`.`turno` (
`IDTURNO` INT(11) NOT NULL AUTO_INCREMENT,
`NOMBRE` VARCHAR(150) NULL DEFAULT NULL,
`CANTIDADHORAS` INT(11) NULL DEFAULT NULL,
`HORAINICIAL` DATE NULL DEFAULT NULL,
`COLACION` INT(11) NULL DEFAULT NULL,
`FECHAMODIFICACION` DATETIME NULL DEFAULT NULL,
`STATUS` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`IDTURNO`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
This is my INSERT:
INSERT INTO `claritytractor`.`turno` (`NOMBRE`, `CANTIDADHORAS`, `HORAINICIAL`, `COLACION`, `FECHAMODIFICACION`, `STATUS`) VALUES ('Normal', 8, '08:00:00', '1', '2016-12-20', '0');
If you are not storing a full date, it is best to use simpler data types to store the value, and modify the value on its way in and out.
Define your field HORAINICIAL as INT instead of DATE, and then use either the number of hours (e.g. 8) directly, or use HOUR() to convert a DATE value into just hours. When extracting the time back out, it can be converted back to a timestamp given the source date if needed.
For more information on the HOUR() command, read this.

MYSQL trigger after insert => insert into second table => before insert trigger not working

I got this datebase structure:
CREATE TABLE `user` (
`usr_id` bigint(20) NOT NULL AUTO_INCREMENT,
`usr_mail` varchar(64) NOT NULL,
`usr_creationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`usr_id`)
);
CREATE TABLE `user_activation` (
`usract_id` bigint(20) NOT NULL AUTO_INCREMENT,
`usr_id` bigint(20) NOT NULL,
`usract_key` text NOT NULL,
`usract_used` int(11) NOT NULL DEFAULT '0',
`usract_creationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`usract_usagedate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`usract_expiredate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER `USER_INSERT_ADD_ACTIVATION` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_activation (usr_id, usract_key)
VALUES (NEW.usr_id, UUID());
CREATE TRIGGER `USRACT_INSER_SET_EXPIREDATA` BEFORE INSERT ON `user_activation`
FOR EACH ROW
SET NEW.usract_expiredate = DATE_ADD(NOW(), INTERVAL 1 DAY);
When I insert data into the table user_activation my user_activation.usract_expiredate gets updated to the next day.
If I insert data into the table user a row is insert into user_activation but user_activation.usract_expiredate will not get set to the next day.
Seems like the second trigger is not being executed.
Am I doing something wrong with the triggers?
Well you need only one trigger to achieve this.
DROP TRIGGER `USRACT_INSER_SET_EXPIREDATA`;
DROP TRIGGER `USER_INSERT_ADD_ACTIVATION`;
CREATE TRIGGER `USER_INSERT_ADD_ACTIVATION` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_activation (usr_id, usract_key,usract_expiredate)
VALUES (NEW.usr_id, UUID(),DATE_ADD(NOW(), INTERVAL 1 DAY));

how to write create table date query in phpmyadmin which store by default current date if date not given?

I am trying to create a table (phpMyAdmin) by using the following query:
CREATE TABLE login_detail(
Id int(11) primary key auto_increment,
userName varchar(100) not null,
userPassword varchar(100) not null,
created_at Date DEFAULT CURRENT_DATE
);
but it showing error at CURRENT_DATE. Can anyone solve this problem?
Its not supported.
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. 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 a TIMESTAMP column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
you just use current time stamp .below code i tested in phpmyadmin.it working fine
CREATE TABLE login_detail(Id int(11) primary key auto_increment, userName varchar(100) not null,userPassword varchar(100) not null, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Try the following:
CREATE TABLE IF NOT EXISTS `login_detail` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(100) NOT NULL,
`userPassword` varchar(100) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
INSERT INTO `login_detail` (`Id`, `userName`, `userPassword`) VALUES
(1, 'aaa', 'bbb'),
(2, 'aaa', 'bbb');
To get the date and-or time in the format you want use DATE_FORMAT
SELECT `Id`, `userName`, `userPassword`, DATE_FORMAT(`created_at`, '%e %b %Y') AS `created_at_date`, DATE_FORMAT(`created_at`, '%H:%i:%s') AS `created_at_time` FROM `login_detail`;
http://sqlfiddle.com/#!9/ea673/1

Create trigger to update column value when update or insert happens in the table

I am new to writing mysql triggers.
here is the create sql statement
CREATE TABLE BRAND_PROFILE_MAPPING (
PROFILE_MAPPING_ID BIGINT(20) NOT NULL AUTO_INCREMENT,
DOTLOOP_PROFILE_ID BIGINT(20) NULL DEFAULT NULL,
BRAND_PROFILE_ID VARCHAR(255) NULL DEFAULT NULL,
PROFILE_TYPE VARCHAR(90) NULL DEFAULT NULL,
PROFILE_NAME VARCHAR(225) NULL DEFAULT NULL,
SOURCE VARCHAR(255) NULL DEFAULT NULL,
PROFILE_HASH VARCHAR(32) NULL DEFAULT NULL,
ENTERED_DATE DATETIME NULL DEFAULT NULL,
CHANGED_DATE DATETIME NULL DEFAULT NULL,
PRIMARY KEY (PROFILE_MAPPING_ID)
);
i created table based on above structure.here i have 2 fields ENTERED_DATE DATETIME
CHANGED_DATE DATETIME , date columns i want to write trigger if any insert operation both date fields should be inserted if there is any update operation,changed date field should be updated.
Appreciate your help.Thanks in advance!!.
Remove profile_mapping_id from list of values as it is auto incremental.
CREATE TRIGGER `brand_profile_mapping_trigger` AFTER INSERT ON brand_profile_mapping FOR EACH ROW
BEGIN insert into **brand_profile_mapping_log** values(NEW.dotloop_profile_id,NEW.brand_profile_id,NEW.profile_type,NEW.profile_name,NEW.source,NEW.profile_hash);
END
If you want both columns to be populated by triggers you can do it in the following way
CREATE TRIGGER brand_profile_mapping_before_insert_trigger
BEFORE INSERT ON brand_profile_mapping -- you have to use BEFORE event
FOR EACH ROW
SET NEW.entered_date = CURRENT_DATE,
NEW.changed_date = CURRENT_DATE;
CREATE TRIGGER brand_profile_mapping_before_update_trigger
BEFORE UPDATE ON brand_profile_mapping -- you have to use BEFORE event
FOR EACH ROW
SET NEW.entered_date = OLD.entered_date, -- preserve the original value, so it can't be overridden
NEW.changed_date = CURRENT_DATE;
Here is a SQLFiddle demo