MYSQL Trigger Copy Entire Row while insert,update - mysql

I have two Table namely admin_user,admin_user_bak
the structure of the table admin_user is
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and structure of table admin_user_bak have all the fields of admin_user and additionally a field bak_user_id . its auto increment id..
CREATE TABLE IF NOT EXISTS `admin_user_bak` (
`bak_user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`bak_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
here my trigger is
CREATE TRIGGER ins_admin_user BEFORE UPDATE ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( NEW.user_id, NEW.user_name, NEW.emailid, new.password, NEW.roll, NEW.created, NEW.modified, NEW.last_login, NEW.status);
END
my purpose is i want to back up all events. insert update delete of a particular record. not all record. i write for insertion. its not working
any idea.. thanks

i want a insertion trigger but you had written update on in your trigger so it's not work.
You use INSERT ON
CREATE TRIGGER ins_admin_user BEFORE INSERT ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( old.user_id, old.user_name, old.emailid, old.password, old.roll, old.created, old.modified, old.last_login, old.status);
END

Related

MySQL insert using transaction

I have following structure on mysql database:
sqlfiddle
What I want to do is:
To select DISTINCT industry from Company table
To insert into Industry table first and get auto incremented ID
With this ID to insert again into IndustryTranslation table and set "language"="en"
To insert Company's id and newly generated Industry's id into MapCompanyIndustry table
I know that it's not possible with one statement. But definitely it's possible with transaction. Can't figure out how to achieve this result with one transaction.
Any suggestions?
Schema
CREATE TABLE `Industry` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `IndustryTranslation` (
`industryID` int(4) unsigned NOT NULL,
`language` varchar(5) NOT NULL,
`name` varchar(255) NOT NULL,
`confirmed` tinyint(1) DEFAULT '0',
PRIMARY KEY (`industryID`,`language`),
KEY `language` (`language`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Company` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`imageUri` varchar(255) DEFAULT NULL,
`countryID` int(3) unsigned DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`verified` tinyint(1) DEFAULT NULL,
`industry` varchar(255) DEFAULT NULL,
`headquarters` varchar(255) DEFAULT NULL,
`uri` varchar(255) DEFAULT NULL,
`createdAt` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `countryID` (`countryID`)
) ENGINE=InnoDB AUTO_INCREMENT=4004 DEFAULT CHARSET=utf8;
CREATE TABLE `MapCompanyIndustry` (
`companyID` int(10) unsigned NOT NULL,
`industryID` int(4) unsigned NOT NULL,
PRIMARY KEY (`companyID`,`industryID`),
KEY `industryID` (`industryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

update another table when updated mysql trigger

I have 2 tables, students and Math. I want to UPDATE existing data on Math.Last_name when I update the students.Last_Name using the ID I am updating in students.
Students table
CREATE TABLE `STUDENTS` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL AUTO_INCREMENT,
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`LRN`)
)COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=123456789112;
MATH TABLE
CREATE TABLE `Math` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
`Level` VARCHAR(3) NOT NULL,
`UT1` VARCHAR(3) NOT NULL,
`Q1` VARCHAR(50) NULL DEFAULT NULL,
`UT2` VARCHAR(50) NULL DEFAULT NULL,
`Q2` VARCHAR(50) NULL DEFAULT NULL,
`UT3` VARCHAR(50) NULL DEFAULT NULL,
`Q3` VARCHAR(50) NULL DEFAULT NULL,
`UT4` VARCHAR(50) NULL DEFAULT NULL,
`Q4` VARCHAR(50) NULL DEFAULT NULL,
`FINAL GRADE` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`LRN`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT;
MY TRIGGER
CREATE TRIGGER `STUDENTS_after_update` AFTER UPDATE ON `STUDENTS` FOR EACH ROW BEGIN
UPDATE Math
SET Last_Name = NEW.Last_Name
WHERE LRN IN (SELECT LRN FROM Math where LRN = NEW.LRN);
END
You do not need a sub select in the update command, since for each row it will pick up the new LRN value and will update all of them in the MATH table where the new.LRN matches in the MATH table. Here how the trigger would look like
delimiter //
create trigger `STUDENTS_after_update` after update on `STUDENTS`
for each row
begin
update Math
set Last_Name = NEW.Last_Name
where LRN = NEW.LRN ;
end;//
delimiter ;
Note that I have added delimiter which is needed when you run them in the Mysql CLI, some other user interface like PHPMyadmin you need to select the delimiter from the user interface.

Finding out if there is a daily insert into mysql table

I have this table which should be getting daily input regarding Vehicle mileage Readings.
CREATE TABLE `table_vehicle_info` (
`id` int(11) NOT NULL ,
`reg_no` varchar(50) NOT NULL,
`vehicle_type` int(11) NOT NULL,
`engine_no` varchar(50) NOT NULL DEFAULT "DDDD",
`chassis_no` varchar(50) ,
`model` int(11) DEFAULT NULL,
`picture` varchar(50) ,
`rent` double ,
`eng_power` int(11) DEFAULT NULL,
`color` varchar(12) DEFAULT NULL,
`rent_status` varchar(50) DEFAULT NULL,
`reg_city` varchar(50) DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
`sub_type` varchar(50) DEFAULT NULL,
`purchase_date` date DEFAULT NULL,
`purchase_amount` double DEFAULT NULL,
`sum_insured` double DEFAULT NULL,
`token_tax_amount` double DEFAULT NULL,
`token_period_from` date DEFAULT NULL,
`reg_placed_at` varchar(50) DEFAULT NULL,
`funded_by` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=99 DEFAULT CHARSET=latin1;
CREATE TABLE `tbl_readings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicle_id` int(11) NOT NULL,
`km_driven` int(11) NOT NULL,
`dt_of_reading` datetime NOT NULL,
`dt_of_entry` datetime NOT NULL,
`entry_user` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `vehicle_id` (`vehicle_id`),
CONSTRAINT `tbl_readings_ibfk_1` FOREIGN KEY (`vehicle_id`) REFERENCES `table_vehicle_info` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;
INSERT INTO table_vehicle_info (id,reg_no,vehicle_type )
VALUES (1, "B-5484", 12 );
INSERT INTO tbl_readings VALUES (NULL,1,100,'2015-6-1',NOW(), 'user');
I want to generate a report showing which vehicle reading isn't entered daily.
You can try following query, it returns all vehicle_id from tbl_readings for which there is no next day entry.
SELECT vehicle_id
FROM `tbl_readings`
WHERE DATE(DATE_ADD(dt_of_entry,INTERVAL 1 DAY)) NOT IN
(SELECT DATE(dt_of_entry) FROM `tbl_readings`)
EDIT
Try this,
SELECT t1.vehicle_id
FROM `tbl_readings` t1
LEFT JOIN (SELECT vehicle_id,
DATE(DATE_ADD(dt_of_reading,INTERVAL -1 DAY)) as reading_date
FROM `tbl_readings`) t2
ON t1.vehicle_id = t2.vehicle_id AND t1.dt_of_reading = t2.reading_date
WHERE t2.vehicle_id IS NULL
GROUP BY t1.vehicle_id
HAVING COUNT(t1.vehicle_id) > 1;
check the fiddle

#1415 - Not allowed to return a result set from a trigger

DELIMITER $$
CREATE TRIGGER `msg_after_insert`
AFTER INSERT ON `meter_reading`
FOR EACH ROW
BEGIN
select meter_reading.*,a.amount as Light_Reading_Amt,b.amount as Power_Reading_Amt from meter_reading inner join master_unit a on a.min_unit<=msg1 and msg1<=a.max_unit and doc>=a.date_of_enter_value_fm inner join master_unit b on b.min_unit<=msg2 and msg2<=b.max_unit and doc<=b.date_of_enter_value_to;
INSERT INTO `smsd`.`outbox` (DestinationNumber,TextDecoded) VALUES (NEW.mobno, CONCAT_WS(NEW.msg1,NEW.Light_Reading_Amt,' ',NEW.msg2,NEW.Light_Reading_Amt));
END $$
DELIMITER ;
My databases are
1. mr_sms (tables are : 1.meter_reading 2.master_unit)
--meter_reading table--
CREATE TABLE IF NOT EXISTS `meter_reading` (
`serno` int(50) NOT NULL AUTO_INCREMENT,
`cno` varchar(50) DEFAULT NULL,
`accn_no` varchar(20) DEFAULT NULL,
`loc` varchar(20) DEFAULT NULL,
`type_of_accn` varchar(50) NOT NULL,
`rank` varchar(25) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`mobno` varchar(15) DEFAULT NULL,
`msg1` int(100) DEFAULT NULL,
`msg2` int(250) NOT NULL,
`status` varchar(30) NOT NULL,
`doc` date NOT NULL,
`remarks` varchar(100) DEFAULT NULL,
PRIMARY KEY (`serno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
--master_unit table--
CREATE TABLE IF NOT EXISTS `master_unit` (
`u_id` int(11) NOT NULL AUTO_INCREMENT,
`min_unit` int(150) NOT NULL,
`max_unit` int(200) NOT NULL,
`rate` varchar(20) NOT NULL,
`amount` varchar(20) NOT NULL,
`duty` varchar(20) NOT NULL,
`dutyamount` varchar(20) NOT NULL,
`date_of_enter_value_fm` date NOT NULL,
`date_of_enter_value_to` date NOT NULL,
PRIMARY KEY (`u_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
2. smsd (table is : outbox)
--
-- Table structure for table `outbox`
--
CREATE TABLE IF NOT EXISTS `outbox` (
`UpdatedInDB` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`InsertIntoDB` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendingDateTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendBefore` time NOT NULL DEFAULT '23:59:59',
`SendAfter` time NOT NULL DEFAULT '00:00:00',
`Text` text,
`DestinationNumber` varchar(20) NOT NULL DEFAULT '',
`Coding` enum('Default_No_Compression','Unicode_No_Compression','8bit','Default_Compression','Unicode_Compression') NOT NULL DEFAULT 'Default_No_Compression',
`UDH` text,
`Class` int(11) DEFAULT '-1',
`TextDecoded` text NOT NULL,
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`MultiPart` enum('false','true') DEFAULT 'false',
`RelativeValidity` int(11) DEFAULT '-1',
`SenderID` varchar(255) DEFAULT NULL,
`SendingTimeOut` timestamp NULL DEFAULT '0000-00-00 00:00:00',
`DeliveryReport` enum('default','yes','no') DEFAULT 'default',
`CreatorID` text NOT NULL,
PRIMARY KEY (`ID`),
KEY `outbox_date` (`SendingDateTime`,`SendingTimeOut`),
KEY `outbox_sender` (`SenderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `outbox`
--

What's the best way to move MySQL to Parse.com

I have an iOS app that currently uses MySQL as the database backend to store about 2000 records and 10,000 photos. I want to refactor my Objective-C to use Parse instead of the current MySQL and I'm wondering what would be the best way to move my MySQL data to Parse?
Here is the current MySQL structure of my database.
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `mysqlToParsePlatform`
--
-- --------------------------------------------------------
--
-- Table structure for table `awesome_authentication`
--
CREATE TABLE `awesome_authentication` (
`authentication_id` int(11) NOT NULL auto_increment,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
`role_id` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL,
`deny_access` int(11) NOT NULL,
PRIMARY KEY (`authentication_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_categories`
--
CREATE TABLE `awesome_categories` (
`category_id` int(11) NOT NULL auto_increment,
`category` varchar(100) NOT NULL,
`category_icon` varchar(100) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_news`
--
CREATE TABLE `awesome_news` (
`news_id` int(11) NOT NULL auto_increment,
`news_content` text NOT NULL,
`news_title` varchar(100) NOT NULL,
`news_url` varchar(100) NOT NULL,
`photo_url` varchar(200) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL,
PRIMARY KEY (`news_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_photos`
--
CREATE TABLE `awesome_photos` (
`photo_id` int(11) NOT NULL auto_increment,
`photo_url` varchar(200) NOT NULL,
`thumb_url` varchar(200) NOT NULL,
`store_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL,
PRIMARY KEY (`photo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10167 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_ratings`
--
CREATE TABLE `awesome_ratings` (
`rating_id` int(11) NOT NULL auto_increment,
`rating` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`store_id` int(11) NOT NULL,
PRIMARY KEY (`rating_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_reviews`
--
CREATE TABLE `awesome_reviews` (
`review_id` int(11) NOT NULL auto_increment,
`review` text NOT NULL,
`store_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL,
PRIMARY KEY (`review_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_stores`
--
CREATE TABLE `awesome_stores` (
`store_id` int(11) NOT NULL auto_increment,
`store_name` varchar(100) NOT NULL,
`store_address` varchar(160) NOT NULL,
`store_desc` text NOT NULL,
`lat` varchar(20) NOT NULL,
`lon` varchar(20) NOT NULL,
`sms_no` varchar(30) NOT NULL,
`phone_no` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
`website` varchar(100) NOT NULL,
`category_id` int(11) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
`featured` int(11) NOT NULL,
`is_deleted` int(11) NOT NULL,
PRIMARY KEY (`store_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2027 ;
-- --------------------------------------------------------
--
-- Table structure for table `awesome_users`
--
CREATE TABLE `awesome_users` (
`user_id` int(11) NOT NULL auto_increment,
`full_name` varchar(100) NOT NULL,
`username` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`login_hash` varchar(200) NOT NULL,
`facebook_id` text NOT NULL,
`twitter_id` text NOT NULL,
`email` varchar(100) NOT NULL,
`deny_access` int(11) NOT NULL,
`thumb_url` varchar(100) NOT NULL,
`photo_url` varchar(100) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
After spending a lot of time looking for answers, trying to research migration services and looking into writting custom scripts, I decided to try the easiest route first. And that worked!
Hopefully this will help others looking to move similar records from MySQL to Parse.
In order to get my 2,000+ stores records and 10,000+ photos out of the MySQL database, I went to phpMyAdmin and exported the awesome_stores and awesome_photos tables to two separate CSV files using the setting pictured below.
Once you have your CSV files, open your Parse Data Browser and go to the Core tab and look under the Data section pictured below and click on Import.
That will bring up the import dialog box. That looks like this. Name your new Custom Class and then add your .CSV. Make sure that phpMyAdmin does not add an extra line to head of your .csv. If it does, you will get an error when trying to do the import to Parse.