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

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.

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;

How to add foreign key in table using existing column, without losing data?

I have a table in which I need to add foreign key on an existing column. Following is create table:
CREATE TABLE `itemtx` (
`itemTxid` int(11) NOT NULL AUTO_INCREMENT,
`itemcode` varchar(1) NOT NULL,
`weight` decimal(7,3) DEFAULT NULL,
`txtype` varchar(10) DEFAULT 'Pickup',
`tripstopid` int(11) NOT NULL,
`barcode` varchar(25) DEFAULT NULL,
`bagcount` int(11) DEFAULT '1',
PRIMARY KEY (`itemTxid`)
) ENGINE=InnoDB AUTO_INCREMENT=33524 DEFAULT CHARSET=latin1
I need to add foreign key on tripstopid column. I cannot drop or empty table as it contains data. Following is the referenced table:
CREATE TABLE `tripstop` (
`tripstopid` int(11) NOT NULL AUTO_INCREMENT,
`tripid` int(11) DEFAULT NULL,
`locationName` varchar(100) DEFAULT NULL,
`userName` varchar(100) DEFAULT NULL,
`locationid` int(11) DEFAULT NULL,
`datetime` varchar(20) DEFAULT NULL,
`createts` varchar(20) DEFAULT NULL,
`latitude` double DEFAULT NULL,
`longitude` double DEFAULT NULL,
`userid` int(11) DEFAULT NULL,
`tid` int(11) DEFAULT NULL,
PRIMARY KEY (`tripstopid`)
) ENGINE=InnoDB AUTO_INCREMENT=4691 DEFAULT CHARSET=latin1
How can I do this without losing my data?
You can achieve this by following:
ALTER TABLE itemtx
ADD FOREIGN KEY (tripstopid) REFERENCES tripstop(tripstopid);
Verified by creating tables, inserting data in them and then updating table for foreign key, previously entered data is not lost.

Hibernate - How to map two tables on two columns?

I have two tables:
CREATE TABLE `framework_files` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fileName` varchar(777) DEFAULT NULL,
`fileSize` float DEFAULT NULL,
`filePath` varchar(777) DEFAULT NULL,
`fileUrl` varchar(777) DEFAULT NULL,
`linkedObjectID` int(11) DEFAULT NULL,
`linkedObjectName` varchar(250) DEFAULT NULL,
`active` int(11) DEFAULT '0',
`uploadDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`uploadersIP` varchar(77) DEFAULT NULL,
`uploadersIPXFORWARDEDFOR` varchar(77) DEFAULT NULL,
`userName` varchar(777) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `q_idx` (`linkedObjectID`,`linkedObjectName`)
) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=utf8;
And second table
CREATE TABLE `publicSitePortfolioWorks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mainName` varchar(100) DEFAULT NULL,
`tableName` varchar(250) DEFAULT 'publicSitePortfolioWorks',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
I need to map publicSitePortfolioWorks to framework_files on framework_files.linkedObjectID=publicSitePortfolioWorks.id and framework_files.linkedObjectName=publicSitePortfolioWork.tableName .
I.e. publicSitePortfolioWorks have many objects of framework_files. All child objects must be in Set, like
private Set<FrameworkFiles> fileTEST = new HashSet<>(0);
How to map this Set in hbm.xml file? At this moment no foreign key between these two tables.

Speeding up multi-table joins with MySQL

I have 3 tables in which I'm trying to preform joins on, and inserting the resulting data into another table. The query is taking anywhere between 15-30 mins depending on the dataset. The tables I'm selecting from and joining on are at least 25k records each but will quickly grow to be 500k+.
I tried adding indexes on the fields but still isn't helping that much. Are there any other things I can try or are joins on this scale just going to take this long?
Here is the query I'm trying to perform:
INSERT INTO audience.topitem
(runs_id, total_training_count, item, standard_index_value, significance, seed_count, nonseed_count, prod, model_type, level_1, level_2, level_3, level_4, level_5)
SELECT 5, seed_count + nonseed_count AS total_training_count,
ii.item, standard_index_value, NULL, seed_count, nonseed_count,
standard_index_value * seed_count AS prod, 'site', topic_L1, topic_L2, topic_L3, topic_L4, topic_L5
FROM audience.item_indexes ii
LEFT JOIN audience.usercounts uc ON ii.item = uc.item AND ii.runs_id = uc.runs_id
LEFT JOIN categorization.categorization at on ii.item = at.url
WHERE ii.runs_id = 5
Table: audience.item_indexes
CREATE TABLE `item_indexes` (
`item` varchar(1024) DEFAULT NULL,
`standard_index_value` float DEFAULT NULL,
`runs_id` int(11) DEFAULT NULL,
`model_type` enum('site','term','combo') DEFAULT NULL,
KEY `item_idx` (`item`(333))
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Table: audience.usercounts
CREATE TABLE `usercounts` (
`item` varchar(1024) DEFAULT NULL,
`seed_count` int(11) DEFAULT NULL,
`nonseed_count` int(11) DEFAULT NULL,
`significance` float(19,6) DEFAULT NULL,
`runs_id` int(11) DEFAULT NULL,
`model_type` enum('site','term','combo') DEFAULT NULL,
KEY `item_idx` (`item`(333))
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Table: audience.topitem
CREATE TABLE `topitem` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`total_training_count` int(11) DEFAULT NULL,
`item` varchar(1024) DEFAULT NULL,
`standard_index_value` float(19,6) DEFAULT NULL,
`significance` float(19,6) DEFAULT NULL,
`seed_count` int(11) DEFAULT NULL,
`nonseed_count` int(11) DEFAULT NULL,
`prod` float(19,6) DEFAULT NULL,
`cat_type` varchar(32) DEFAULT NULL,
`cat_level` int(11) DEFAULT NULL,
`conf` decimal(19,9) DEFAULT NULL,
`level_1` varchar(64) DEFAULT NULL,
`level_2` varchar(64) DEFAULT NULL,
`level_3` varchar(64) DEFAULT NULL,
`level_4` varchar(64) DEFAULT NULL,
`level_5` varchar(64) DEFAULT NULL,
`runs_id` int(11) DEFAULT NULL,
`model_type` enum('site','term','combo') DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=825 DEFAULT CHARSET=utf8;
Table: categorization.categorization
CREATE TABLE `AT_categorization` (
`url` varchar(760) NOT NULL ,
`language` varchar(10) DEFAULT NULL,
`category` text,
`entity` text,
`source` varchar(255) DEFAULT NULL,
`topic_L1` varchar(45) NOT NULL DEFAULT '',
`topic_L2` varchar(45) NOT NULL DEFAULT '',
`topic_L3` varchar(45) NOT NULL DEFAULT '',
`topic_L4` varchar(45) NOT NULL DEFAULT '',
`topic_L5` varchar(45) NOT NULL DEFAULT '',
`last_refreshed` datetime DEFAULT NULL,
PRIMARY KEY (`url`,`topic_L1`,`topic_L2`,`topic_L3`,`topic_L4`,`topic_L5`),
UNIQUE KEY `inx_url` (`url`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
If you add the following indexes, your query will run faster:
CREATE INDEX runs_idx ON audience.item_indexes (runs_id);
ALTER TABLE audience.usercounts
DROP INDEX item_idx,
ADD INDEX item_idx (runs_id, item(333));
Also, item_indexes is utf8, but AT_categorization is latin1, which keeps any indexes from being used. To address this issue, change AT_categorization to utf8:
ALTER TABLE AT_categorization CHARSET=utf8;
Lastly, for the AT_categorization table, the two indexes
PRIMARY KEY (`url`,`topic_L1`,`topic_L2`,`topic_L3`,`topic_L4`,`topic_L5`),
UNIQUE KEY `inx_url` (`url`)
are redundant. So you could DROP these, and simply have the url field be the primary key:
ALTER TABLE AT_categorization
DROP PRIMARY KEY,
DROP KEY `inx_url`,
ADD PRIMARY KEY (url);

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,