What is the key attribute in the table definition - mysql

What are these key attributes, I'm not able to search in the Mysql manual:
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`)
full table structure is here:
CREATE TABLE IF NOT EXISTS `node` (
`nid` int(10) unsigned NOT NULL auto_increment,
`vid` int(10) unsigned NOT NULL default '0',
`type` varchar(32) NOT NULL default '',
`language` varchar(12) NOT NULL default '',
`title` varchar(255) NOT NULL default '',
`uid` int(11) NOT NULL default '0',
`status` int(11) NOT NULL default '1',
`created` int(11) NOT NULL default '0',
`changed` int(11) NOT NULL default '0',
`comment` int(11) NOT NULL default '0',
`promote` int(11) NOT NULL default '0',
`moderate` int(11) NOT NULL default '0',
`sticky` int(11) NOT NULL default '0',
`tnid` int(10) unsigned NOT NULL default '0',
`translate` int(11) NOT NULL default '0',
PRIMARY KEY (`nid`),
UNIQUE KEY `vid` (`vid`),
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1041 ;

From the manual:
KEY is normally a synonym for INDEX.

Related

"1062 Duplicate entry" on SELECT

I've got a SELECT query that's returning the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user_id-name email#address.com--2018-11-04 01:24:38' for key ''
(I replaced the user_id, name and email for privacy)
SELECT users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created,
MAX(cards.card_id) AS latest_card_id, MAX(cards.date_created) AS latest_date_card, MAX(punches.date_created) AS date_lastvisited
FROM users
INNER JOIN cards ON cards.user_id = users.user_id
INNER JOIN punches ON punches.card_id = cards.card_id
WHERE punches.business_id=1626
GROUP BY users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created
ORDER BY users.fullname;
Here are the table definitions for cards, punches and users:
CREATE TABLE `cards` (
`card_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`printjob_id` int(11) DEFAULT NULL,
`temp` varchar(256) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`whitelabel_id` int(11) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`card_id`),
KEY `printjob_id` (`printjob_id`),
KEY `active` (`active`),
KEY `user_id` (`user_id`),
KEY `business_id` (`business_id`)
) ENGINE=InnoDB AUTO_INCREMENT=576475 DEFAULT CHARSET=latin1;
CREATE TABLE `punches` (
`punch_id` int(11) NOT NULL AUTO_INCREMENT,
`punch_count` int(11) unsigned NOT NULL DEFAULT '1',
`employeebonus` tinyint(1) NOT NULL DEFAULT '0',
`details` text,
`card_id` int(11) DEFAULT NULL,
`behaviour` set('basicpunch','employeebonus','visitsregularly','broughtfriend','mytreat','firstcustomer','dailyrepeater','visitslocations','facebookshare','mostfrequent','longabsence','registerer','employeepromoter','luckiest','enroll','opportunist') NOT NULL DEFAULT 'basicpunch',
`location_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`employee_id` int(11) DEFAULT NULL,
`v1` float DEFAULT NULL,
`eventcounter` smallint(5) unsigned NOT NULL DEFAULT '0',
`date_local` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`punch_id`),
KEY `business_id` (`business_id`),
KEY `employee_id` (`employee_id`),
KEY `card_id` (`card_id`),
KEY `date_created` (`date_created`),
KEY `behaviour_id` (`behaviour`),
KEY `location_id` (`location_id`),
KEY `employeebonus` (`employeebonus`),
KEY `punch_count` (`punch_count`)
) ENGINE=InnoDB AUTO_INCREMENT=807402 DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`fullname` varchar(60) DEFAULT NULL,
`email` varchar(50) NOT NULL,
`undeliverable` tinyint(4) DEFAULT '0',
`phone_formatted` varchar(25) DEFAULT NULL,
`phone_unformatted` varchar(15) DEFAULT NULL,
`password_hash` char(64) DEFAULT NULL,
`password_salt` char(6) DEFAULT NULL,
`reset_hash` binary(32) DEFAULT NULL,
`date_reset` datetime DEFAULT NULL,
`stripecustomer_id` int(11) DEFAULT NULL,
`stripe_customer_id` varchar(40) DEFAULT NULL,
`stripe_customer_access_id` varchar(40) DEFAULT NULL,
`problemwithpayment` tinyint(1) NOT NULL DEFAULT '0',
`enterprisemanager_whitelabel_id` int(11) NOT NULL DEFAULT '0',
`god` tinyint(1) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
KEY `email` (`email`),
KEY `phone_unformatted` (`phone_unformatted`),
KEY `date_reset` (`date_reset`),
KEY `undeliverable` (`undeliverable`)
) ENGINE=InnoDB AUTO_INCREMENT=16809 DEFAULT CHARSET=latin1;
I don't understand how I can get this error with a SELECT query. When I remove the MAX() functions, I no longer get the error.

mysql #1005 - Can't create table 'xxx.house_auctions' (errno: 150) (Details…)

I have no idea why this error is happening. Below is the chain of tables that I use. I don't think this should be anything too difficult. Would appreciate all the help I can get with this.
mysql #1005 - Can't create table 'xxx.house_auctions' (errno: 150) (Details…)
CREATE TABLE IF NOT EXISTS `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`group_id` int(11) NOT NULL DEFAULT '1',
`account_id` int(11) NOT NULL DEFAULT '0',
`level` int(11) NOT NULL DEFAULT '1',
`vocation` int(11) NOT NULL DEFAULT '0',
`health` int(11) NOT NULL DEFAULT '150',
`healthmax` int(11) NOT NULL DEFAULT '150',
`experience` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `vocation` (`vocation`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `houses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner` int(11) NOT NULL,
`paid` int(10) unsigned NOT NULL DEFAULT '0',
`warnings` int(11) NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`rent` int(11) NOT NULL DEFAULT '0',
`town_id` int(11) NOT NULL DEFAULT '0',
`bid` int(11) NOT NULL DEFAULT '0',
`bid_end` int(11) NOT NULL DEFAULT '0',
`last_bid` int(11) NOT NULL DEFAULT '0',
`highest_bidder` int(11) NOT NULL DEFAULT '0',
`size` int(11) NOT NULL DEFAULT '0',
`beds` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `owner` (`owner`),
KEY `town_id` (`town_id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `house_auctions` (
`house_id` INT UNSIGNED NOT NULL,
`player_id` INT NOT NULL,
`bid` INT UNSIGNED NOT NULL DEFAULT 0,
`limit` INT UNSIGNED NOT NULL DEFAULT 0,
`endtime` BIGINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`house_id`,`player_id`),
FOREIGN KEY (`house_id`) REFERENCES `houses` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
I think the house_auctions columns have different types than the ones you want to reference. Try this (note INT(11) instead of INT UNSIGNED):
CREATE TABLE IF NOT EXISTS `house_auctions` (
`house_id` INT(11) NOT NULL,
`player_id` INT(11) NOT NULL,
`bid` INT UNSIGNED NOT NULL DEFAULT 0,
`limit` INT UNSIGNED NOT NULL DEFAULT 0,
`endtime` BIGINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (`house_id`,`player_id`),
FOREIGN KEY (`house_id`) REFERENCES `houses` (`id`) ON DELETE CASCADE,
FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
) ENGINE=INNODB;

mysql create table with foreign keys failing

I am trying to create a tables in a db that have foreign keys. what am i missing?
CREATE TABLE `users` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userName` varchar(24) NOT NULL DEFAULT '',
`password` varchar(32) NOT NULL DEFAULT '',
`firstName` varchar(32) NOT NULL DEFAULT '',
`lastName` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
CREATE TABLE `topLevelPermissions` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userId` bigint(10) NOT NULL DEFAULT '0',
`user` char(1) NOT NULL DEFAULT '0',
`group` char(1) NOT NULL DEFAULT '0',
`someField` char(1) NOT NULL DEFAULT '0',
`someOtherField` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
FOREIGN KEY (`userId`) REFERENCES users (`id`) ON DELETE CASCADE
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
and i see nothing related to foreign keys. in fact when i
show create table topLevelPermissions;
i see
CREATE TABLE `topLevelPermissions` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`userId` bigint(10) NOT NULL DEFAULT '0',
`user` char(1) NOT NULL DEFAULT '0',
`group` char(1) NOT NULL DEFAULT '0',
`someField` char(1) NOT NULL DEFAULT '0',
`someOtherField` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `userId` (`userId`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
Innodb only supports foreign key constraints. So switch to Innodb if you want to utilize foreign key constraints.

I can't create a foreign key in my table - #1005

#1005 - Can't create table 'forum.#sql-da8_f' (errno: 150)
I keep getting this error when i want to apply a foreign key constraint to my table. I don't know what could be the problem. I learned that one will need to use an InnoDB to be able to use a FOREIGN KEY on Mysql. Doesn't that come with Mysql by default?
Btw, here
ALTER TABLE boards
ADD FOREIGN KEY (CategoryId)
REFERENCES categories(CategoryId)
EDIT:
Here's how i created my table
CREATE TABLE IF NOT EXISTS `boards` (
`BoardId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`CategoryId` int(11) DEFAULT '0',
`ChildLevel` int(11) DEFAULT '0',
`ParentId` int(11) DEFAULT '0',
`BoardOrder` int(11) DEFAULT '0',
`LastMessageId` int(11) DEFAULT '0',
`MessageUpdatedId` int(11) DEFAULT '0',
`Groups` varchar(255) DEFAULT '',
`ProfileId` int(11) DEFAULT '0',
`BoardName` varchar(255) DEFAULT '',
`BoardDescription` text,
`NumberOfTopics` int(11) DEFAULT '0',
`NumberOfPosts` int(11) DEFAULT '0',
`CountPosts` int(11) DEFAULT '0',
`HiddenPosts` int(11) DEFAULT '0',
`HiddenTopics` int(11) DEFAULT '0',
PRIMARY KEY (`BoardId`),
UNIQUE KEY `BoardId` (`BoardId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And here's my "categories" table:
CREATE TABLE IF NOT EXISTS `categories` (
`CategoryId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`CategoryOrder` int(11) DEFAULT '0',
`CategoryName` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`CategoryId`),
UNIQUE KEY `CategoryId` (`CategoryId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ;
The problem is, that the types don't match: your primary key on categories is a bigint unsigned while your foreign key in boards is of type int. E.g. change the boards table:
CREATE TABLE IF NOT EXISTS `boards` (
`BoardId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`CategoryId` bigint(20) unsigned DEFAULT '0',
-- ...
)
See this demo.

#1005 - Can't create table errno: 150 Magento

I am trying to create a new table for magento and I am trying to reference existing magento tables. From what I googled, the problem that I am getting can be 1 of the 2 issues.
The FK must have a index
The PK must exist before the FK can reference
In both cases, I believe I did both of these correctly. Below is existing table schemas
ALREADY EXISTING TABLES BEING REFERENCED
CREATE TABLE IF NOT EXISTS `core_store` (
`store_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(32) NOT NULL DEFAULT '',
`website_id` smallint(5) unsigned DEFAULT '0',
`group_id` smallint(5) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`sort_order` smallint(5) unsigned NOT NULL DEFAULT '0',
`is_active` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`store_id`),
UNIQUE KEY `code` (`code`),
KEY `FK_STORE_WEBSITE` (`website_id`),
KEY `is_active` (`is_active`,`sort_order`),
KEY `FK_STORE_GROUP` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores' AUTO_INCREMENT=9 ;
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(32) NOT NULL DEFAULT '',
`lastname` varchar(32) NOT NULL DEFAULT '',
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(40) NOT NULL DEFAULT '',
`password` varchar(100) NOT NULL DEFAULT '',
`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` datetime DEFAULT NULL,
`logdate` datetime DEFAULT NULL,
`lognum` smallint(5) unsigned NOT NULL DEFAULT '0',
`reload_acl_flag` tinyint(1) NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`extra` text,
`failures_num` smallint(6) NOT NULL DEFAULT '0',
`first_failure` datetime DEFAULT NULL,
`lock_expires` datetime DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `UNQ_ADMIN_USER_USERNAME` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Users' AUTO_INCREMENT=25 ;
Table I am trying to create
CREATE TABLE `oro_dashboard`
( `id` int unsigned NOT NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(64) NOT NULL default '',
`created_by` int unsigned NOT NULL default '0',
`created_at` date,
`layout` varchar(255) NOT NULL default '',
`default_store_id` int,
PRIMARY KEY (`id`),
KEY `IDX_ORO_DASHBOARD_CREATED_BY` (`created_by`),
CONSTRAINT `FK_ORO_DASHBOARD_CREATED_BY_ADMIN_USER_USER_ID` FOREIGN KEY (`created_by`) REFERENCES `admin_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ORO_DASHBOARD_DEFAULT_STORE_ID_CORE_STORE_STORE_ID` FOREIGN KEY (`default_store_id`) REFERENCES `core_store` (`store_id`) ON DELETE SET NULL ON UPDATE CASCADE )
ENGINE=INNODB charset=utf8 COLLATE=utf8_unicode_ci
In magento, I found there is in deed a table called admin_user that does have a column called user_id. There is a core_store table that does have a column called store_id. and both my columns have INDEXES made.
Does anyone have any clue what the issue maybe ? Below is my error message
#1005 - Can't create table 'db.oro_dashboard' (errno: 150)
It is necessary to have data type to be same for the foreign key and its corresponding parent key. The data type is different of the foreign key and its referencing parent key in your table that is why it is giving an error.
created by is int unsigned while userid is medium int in parent table
same is problem in the second foreign key
reference http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
When defining foreign keys, the data type must be the same.
You are defining core_store.store_id as smallint(5) unsigned and so the referencing column must be the same: oro_dashboard.default_store_id.
Do also with oro_dashboard.created_by
Final oro_dashboard CREATE TABLE query,
CREATE TABLE `oro_dashboard`
( `id` int unsigned NOT NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(64) NOT NULL default '',
`created_by` mediumint(9) unsigned NOT NULL default '0',
`created_at` date,
`layout` varchar(255) NOT NULL default '',
`default_store_id` smallint(5) unsigned,
PRIMARY KEY (`id`),
KEY `IDX_ORO_DASHBOARD_CREATED_BY` (`created_by`) ,
CONSTRAINT `FK_ORO_DASHBOARD_CREATED_BY_ADMIN_USER_USER_ID`
FOREIGN KEY (`created_by`)
REFERENCES `admin_user` (`user_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ORO_DASHBOARD_DEFAULT_STORE_ID_CORE_STORE_STORE_ID`
FOREIGN KEY (`default_store_id`)
REFERENCES `core_store` (`store_id`)
ON DELETE SET NULL ON UPDATE CASCADE
)
SQLFIddle Demo
It appears that the column definition for default_store_id does not match core_store.store_id. it should be smallint(5) unsigned in your table. created_by has the same problem, although it did not prevent the table from being created