MySQL insert using transaction - mysql

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;

Related

I need to find an open spot in the calendar (MYSQL) and allocate that time and date to a job/work order

I need to find an open date time in the future and allocate that time in the calendar for that employee based on his/her worktimes Thanks in advance
I have a calendar table, employee(recourse) table ,employee working times and jobs (with Duration)
CREATE TABLE `calendar` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Subject` varchar(200) DEFAULT NULL,
`DateField` date DEFAULT NULL,
`EndDate` date DEFAULT NULL,
`TimeField` time DEFAULT NULL,
`EndTime` time DEFAULT NULL,
`job_id` int(11) DEFAULT NULL,
`recourse_employee_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1902 DEFAULT CHARSET=utf8;
CREATE TABLE `rc_resources_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`categoryid` int(11) DEFAULT NULL,
`login_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `jobs` (
`Job_ID` int(11) NOT NULL AUTO_INCREMENT,
`Job_Subject` varchar(150) DEFAULT NULL,
`Job_Description` mediumtext,
`Job_Start_Date` date DEFAULT NULL,
`Job_End_Date` date DEFAULT NULL,
`Job_Duration` float DEFAULT NULL,
`Job_Start_Time` time DEFAULT NULL,
`Job_End_Time` time DEFAULT NULL,
`Job_employee` int(11) DEFAULT NULL,
PRIMARY KEY (`Job_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1875 DEFAULT CHARSET=utf8;
CREATE TABLE `work_hours` (
`wh_id` int(11) NOT NULL AUTO_INCREMENT,
`recourse_id` int(11) unsigned NOT NULL,
`wh_day_of_week` int(11) unsigned NOT NULL,
`wh_start_time` time NOT NULL,
`wh_end_time` time NOT NULL,
PRIMARY KEY (`wh_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

Subquery returning null

Got this confusing issue where I need to get data from several tables and join them into one result.
This query was working fine:
select simcard.*,customer.name as cuname,mi,ma,co, tot from simcard, customer,
(SELECT s.sim_id AS ssim_id, min(datetime) AS mi, max(datetime) AS ma, FLOOR(sum(t.WEIGHT) / 1000) AS tot,
(SELECT count(datetime)
FROM transactions t
WHERE (t.SIM_ID = ssim_id) AND t.ROWTYPE LIKE '$D' GROUP BY t.sim_id) as co
FROM simcard s, transactions t
WHERE (s.sim_id = t.sim_id) AND t.ROWTYPE LIKE '$Z'
AND ( s.customer_id =1 ) GROUP BY s.sim_id) as T
WHERE (sim_id = ssim_id) AND (simcard.customer_id = customer.id) GROUP BY simcard.SIM_ID
But when I add a new customer and new simcard to that customer, it will return empty. I'm guessing this is because there is not transaction with that sim_id in the transaction able.
I've tried to left join but I'm just getting errors.
I've narrowed it down to the first subquery, removing the second one did not result in an non-empty result, but because both subqueries uses the transaction table I'm guessing that both will need some left join.
Structure:
DROP TABLE IF EXISTS `customer`;
CREATE TABLE IF NOT EXISTS `customer` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`NAME` varchar(40) DEFAULT NULL,
`API` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`ID`),
KEY `CUSTOMER_ID` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabellstruktur `simcard`
--
DROP TABLE IF EXISTS `simcard`;
CREATE TABLE IF NOT EXISTS `simcard` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`SIM_ID` char(20) DEFAULT NULL,
`CUSTOMER_ID` char(10) DEFAULT NULL,
`SCALES_ID` char(10) DEFAULT NULL,
`NAME` varchar(40) DEFAULT NULL,
`ACTIVE` char(1) DEFAULT 'N',
`EMAIL` varchar(255) DEFAULT NULL,
`TARGET_WEIGHT` varchar(255) DEFAULT NULL,
`LICENSE` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`ID`),
KEY `SIM_ID` (`SIM_ID`),
KEY `CUSTOMER_ID` (`CUSTOMER_ID`),
KEY `SCALES_ID` (`SCALES_ID`),
KEY `SIM_ID_2` (`SIM_ID`,`CUSTOMER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Tabellstruktur `transactions`
--
DROP TABLE IF EXISTS `transactions`;
CREATE TABLE IF NOT EXISTS `transactions` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`SCALES_ID` char(10) DEFAULT NULL,
`SIM_ID` char(20) DEFAULT NULL,
`ROWTYPE` char(2) DEFAULT NULL,
`DATETIME` datetime DEFAULT NULL,
`TRANSACTIONTIME` int(11) DEFAULT NULL,
`NAME` varchar(255) DEFAULT NULL,
`TRANSACTIONNUMBER` int(11) DEFAULT NULL,
`DATA0` varchar(255) DEFAULT NULL,
`DATA1` varchar(255) DEFAULT NULL,
`DATA2` varchar(255) DEFAULT NULL,
`DATA3` varchar(255) DEFAULT NULL,
`DATA4` varchar(255) DEFAULT NULL,
`DATA5` varchar(255) DEFAULT NULL,
`DATA6` varchar(255) DEFAULT NULL,
`DATA7` varchar(255) DEFAULT NULL,
`DATA8` varchar(255) DEFAULT NULL,
`MEMORY` varchar(255) DEFAULT NULL,
`MATERIAL` varchar(255) DEFAULT NULL,
`DENSITY` int(11) DEFAULT NULL,
`VEHICLETYPE` char(1) DEFAULT NULL,
`TOOLNAME` varchar(255) DEFAULT NULL,
`WEIGHT` int(11) DEFAULT NULL,
`TRANSACTIONID` int(11) DEFAULT NULL,
`group_id` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`ID`),
KEY `SIM_ID` (`SIM_ID`),
KEY `TRANSACTIONID` (`TRANSACTIONID`),
KEY `SCALES_ID` (`SCALES_ID`),
KEY `SIM_ID_2` (`SIM_ID`,`TRANSACTIONID`),
KEY `idx_transactions_ROWTYPE` (`ROWTYPE`),
KEY `idx_transactions_DATETIME` (`DATETIME`),
KEY `idx_transactions_DATA0` (`DATA0`),
KEY `idx_transactions_DATA1` (`DATA1`),
KEY `idx_transactions_DATA2` (`DATA2`),
KEY `idx_transactions_DATA3` (`DATA3`),
KEY `idx_transactions_DATA4` (`DATA4`),
KEY `idx_transactions_DATA5` (`DATA5`),
KEY `idx_transactions_DATA6` (`DATA6`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
COMMIT;
Expected result:
ID SIM_ID CUSTOMER_ID SCALES_ID NAME ACTIVE EMAIL TARGET_WEIGHT LICENSE cuname mi ma co tot
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8 10100421287868 1 61 M-61M Y NULL NULL 0 testcustomer 2018-04-26 00:00:00 2018-08-08 00:00:00 14908 529446

Cant create a relation in mysql - Error creating foreign key on id_produk (check data types)

here is my table :
CREATE TABLE `pesanan` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ref_number` varchar(50) NOT NULL DEFAULT '',
`id_produk` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`qty` int(11) NOT NULL,
`total` varchar(50) NOT NULL DEFAULT '',
`tanggal` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
i want to create a relation of id_produk to table produk.id and id_user to user.id, here is the other table :
CREATE TABLE `produk` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_kategori` int(11) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT '',
`slug` varchar(100) NOT NULL DEFAULT '',
`judul` varchar(100) NOT NULL DEFAULT '',
`harga` varchar(10) DEFAULT '',
`target` int(11) DEFAULT NULL,
`desc` text,
`cover` varchar(100) DEFAULT NULL,
`tanggal` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1;
and user table
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`suspended` int(11) DEFAULT NULL,
`level` varchar(11) DEFAULT NULL,
`nama` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`alamat` text,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
when i try to relation it , it give me this error :
Error creating foreign key on id_produk (check data types)
The columns should have the exact same data type in both tables. In your case in the "produk" and "user" tables they are unsigned, which means they go from 0 to 4294967295 but in the "pesanan" table they are signed which means they go from -2147483648 to 2147483647. See more here.
If the server allowed you to create such foreign keys, it would possibly create situations where, for example, a user is added with and ID of 2147483648 that cannot ever be referenced in the "pesanan" table.
You should change the "id_produk" and "id_user" columns in the "pesanan" table to be unsigned.

FlashChat installation hiccup! SQL syntax error?

I've done away with the (14) and (11) but when trying to install tufat's Flashchat, I keep getting this error:
Could not create DB table 'smf_fc_bans'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 '(14) NOT NULL, userid int(11)
default NULL, banneduserid int(11) default NULL, r' at line 1
Granted I'm still such a novice. If someone could help me I would be so very very grateful.
Table structure for table bans:
CREATE TABLE `bans` (
`id` int NOT NULL auto_increment,
`created` timestamp NOT NULL,
`userid` int default NULL,
`banneduserid` int default NULL,
`roomid` int default NULL,
`ip` varchar default NULL,
KEY `id` (`id`),
KEY `userid` (`userid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
Table structure for table connections:
CREATE TABLE `connections` (
`id` varchar(32) NOT NULL default '',
`updated` timestamp NOT NULL,
`created` timestamp NOT NULL,
`userid` int default NULL,
`roomid` int default NULL,
`state` tinyint(4) NOT NULL default '1',
`color` int default NULL,
`start` int default NULL,
`lang` char(2) default NULL,
`ip` varchar(16) default NULL,
`tzoffset` int default '0',
`chatid` int NOT NULL default '1',
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
KEY `roomid` (`roomid`),
KEY `updated` (`updated`)
) ENGINE=MyISAM;
Table structure for table ignors:
CREATE TABLE `ignors` (
`created` timestamp NOT NULL,
`userid` int default NULL,
`ignoreduserid` int default NULL,
KEY `userid` (`userid`),
KEY `ignoreduserid` (`ignoreduserid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
Table structure for table messages:
CREATE TABLE `messages` (
`id` int(11) NOT NULL auto_increment,
`created` timestamp NOT NULL,
`toconnid` varchar(32) default NULL,
`touserid` int(11) default NULL,
`toroomid` int(11) default NULL,
`command` varchar(255) NOT NULL default '',
`userid` int default NULL,
`roomid` int(11) default NULL,
`txt` text,
PRIMARY KEY (`id`),
KEY `touserid` (`touserid`),
KEY `toroomid` (`toroomid`),
KEY `toconnid` (`toconnid`),
KEY `created` (`created`)
) ENGINE=MyISAM AUTO_INCREMENT=14 ;
Table structure for table rooms:
CREATE TABLE `rooms` (
`id` int NOT NULL auto_increment,
`updated` timestamp NOT NULL,
`created` timestamp NOT NULL,
`name` varchar(64) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`ispublic` char(1) default NULL,
`ispermanent` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `ispublic` (`ispublic`),
KEY `ispermanent` (`ispermanent`),
KEY `updated` (`updated`)
) WNGINW=MyISAM AUTO_INCREMENT=5 ;
Table structure for table users:
CREATE TABLE `users` (
`id` int NOT NULL auto_increment,
`login` varchar(32) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`roles` int NOT NULL default '0',
`profile` text,
PRIMARY KEY (`id`),
KEY `login` (`login`)
) ENGINE=MyISAM AUTO_INCREMENT=2 ;`
The varchar datatype requires a parameter like varchar(50)
CREATE TABLE `bans` (
`id` int NOT NULL auto_increment,
`created` timestamp NOT NULL,
`userid` int default NULL,
`banneduserid` int default NULL,
`roomid` int default NULL,
`ip` varchar( requires a number ) default NULL, <-- HERE
KEY `id` (`id`),
KEY `userid` (`userid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
You also have an error in
CREATE TABLE `rooms` (
`id` int NOT NULL auto_increment,
`updated` timestamp NOT NULL,
`created` timestamp NOT NULL,
`name` varchar(64) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`ispublic` char(1) default NULL,
`ispermanent` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `ispublic` (`ispublic`),
KEY `ispermanent` (`ispermanent`),
KEY `updated` (`updated`)
) WNGINW=MyISAM AUTO_INCREMENT=5 ;
WNGINW=MyISAM AUTO_INCREMENT=5 ;
Should be
ENGINE=MyISAM AUTO_INCREMENT=5 ;

Error in MySQL database installer syntax for chat system

I have spent the past week mulling over this single MySQL document with no luck as to figuring out what/where the error is that's stopping it from properly installing it's self so I can have the chat system it's for up and running. I would go to the developer but he's essentially been unresponsive for over a year now so I have given up on help from him. The error I am getting upon trying to install it is the following :
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 '(14) NOT NULL, userid int(11) default NULL, >banneduserid int(11) default NULL, r' at line 1
The MySQL file reads as follows :
#--------------------------------------------------------------------------
# Table structure for table `bans`
#--------------------------------------------------------------------------
CREATE TABLE `bans` (
`created` timestamp(14) NOT NULL,
`userid` int(11) default NULL,
`banneduserid` int(11) default NULL,
`roomid` int(11) default NULL,
`ip` varchar(16) default NULL,
KEY `userid` (`userid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
#--------------------------------------------------------------------------
# Table structure for table `connections`
#--------------------------------------------------------------------------
CREATE TABLE `connections` (
`id` varchar(32) NOT NULL default '',
`updated` timestamp(14) NOT NULL,
`created` timestamp(14) NOT NULL,
`userid` int(11) default NULL,
`roomid` int(11) default NULL,
`state` tinyint(4) NOT NULL default '1',
`color` int(11) default NULL,
`start` int(11) default NULL,
`lang` char(2) default NULL,
`ip` varchar(16) default NULL,
`tzoffset` int(11) default '0',
`chatid` int(11) NOT NULL default '1',
PRIMARY KEY (`id`),
KEY `userid` (`userid`),
KEY `roomid` (`roomid`),
KEY `updated` (`updated`)
) ENGINE=MyISAM;
#--------------------------------------------------------------------------
# Table structure for table `ignors`
#--------------------------------------------------------------------------
CREATE TABLE `ignors` (
`created` timestamp(14) NOT NULL,
`userid` int(11) default NULL,
`ignoreduserid` int(11) default NULL,
KEY `userid` (`userid`),
KEY `ignoreduserid` (`ignoreduserid`),
KEY `created` (`created`)
) ENGINE=MyISAM;
#--------------------------------------------------------------------------
# Table structure for table `messages`
#--------------------------------------------------------------------------
CREATE TABLE `messages` (
`id` int(11) NOT NULL auto_increment,
`created` timestamp(14) NOT NULL,
`toconnid` varchar(32) default NULL,
`touserid` int(11) default NULL,
`toroomid` int(11) default NULL,
`command` varchar(255) NOT NULL default '',
`userid` int(11) default NULL,
`roomid` int(11) default NULL,
`txt` text,
PRIMARY KEY (`id`),
KEY `touserid` (`touserid`),
KEY `toroomid` (`toroomid`),
KEY `toconnid` (`toconnid`),
KEY `created` (`created`)
) ENGINE=MyISAM AUTO_INCREMENT=14 ;
#--------------------------------------------------------------------------
# Table structure for table `rooms`
#--------------------------------------------------------------------------
CREATE TABLE `rooms` (
`id` int(11) NOT NULL auto_increment,
`updated` timestamp(14) NOT NULL,
`created` timestamp(14) NOT NULL,
`name` varchar(64) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`ispublic` char(1) default NULL,
`ispermanent` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `ispublic` (`ispublic`),
KEY `ispermanent` (`ispermanent`),
KEY `updated` (`updated`)
) ENGINE=MyISAM AUTO_INCREMENT=5 ;
#--------------------------------------------------------------------------
# Table structure for table `users`
#--------------------------------------------------------------------------
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`login` varchar(32) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`roles` int(11) NOT NULL default '0',
`profile` text,
PRIMARY KEY (`id`),
KEY `login` (`login`)
) ENGINE=MyISAM AUTO_INCREMENT=2 ;
My guess is that there is an older call/request that is out dated and no longer used for the version of MySQL I have installed on my host ( Version : 5.5.19 ). I really appreciate any help I can get with this so I can finally stop running off of a flat file ( the other install option this thing has ) and integrate it into my CMS. Thank you for your time!
-Reiz
Remove the (14) from all your timestamp types like that
... column_name timestamp NOT NULL ...
In MySQL versions after 4.1 the timestamp datatype has no properties added in brackets.
MySQL Docu 4.1
MySQL Docu 5.1
So in your case just drop the (14) after all appearances of timestamp in the sql. So, e.g., change this
`created` timestamp(14) NOT NULL,
to this
`created` timestamp NOT NULL,