mysql refference key issue, phpmyadmin error code 150 [duplicate] - mysql

This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 7 years ago.
I am trying to create two table as below,
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` ( `Id` int(11) NOT NULL
AUTO_INCREMENT, `UserType` varchar(32) DEFAULT NULL, `FirstName`
varchar(64) DEFAULT NULL, `LastName` varchar(64) DEFAULT NULL,
`Email` varchar(64) DEFAULT NULL, `CompanyName` varchar(64) DEFAULT
NULL, `Telephone` varchar(64) DEFAULT NULL, `Country` varchar(64)
DEFAULT NULL, `Website` varchar(64) DEFAULT NULL, `JobTitle`
varchar(64) DEFAULT NULL, `Active` int(1) DEFAULT NULL, `Notes`
text, `DateOfRegistration` datetime DEFAULT NULL, PRIMARY KEY
(`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Table structure for table `login`
--
CREATE TABLE IF NOT EXISTS `login` ( `Id` int(11) NOT NULL,
`Username` varchar(64) NOT NULL, `Email` varchar(64) NOT NULL,
`Password` varchar(64) NOT NULL, `FailedAttemptCount` int(2) NOT
NULL, `LastLogin` datetime NOT NULL, `UserLevel` int(1) NOT NULL,
`IsVerified` int(1) NOT NULL DEFAULT '0', `VerificationKey`
varchar(256) NOT NULL, `VerifiKeyCreated` datetime NOT NULL,
PRIMARY KEY (`Id`), FOREIGN KEY (`Id`) REFERENCES users.Id, UNIQUE
KEY `username` (`Username`), UNIQUE KEY `Email` (`Email`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to add a foreign key i.e. login.Id references users.Id, but I am getting error(code 150) in phpmyadmin.
Thanks in advance for your help!
Best Regards,
Rajib

It should be
REFERENCES `users`(`Id`)
instead of
REFERENCES users.Id
cf. http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Related

error 105 mysql FOREIGN KEYS

I am trying to create database table and get them connected by mysql FOREIGN KEYS. I have ensured that my data types are identical. I also ensure that my tables are made before added the FK. Any advice would be greatly appreciated.
CREATE TABLE IF NOT EXISTS `af_feeds` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`hash` char(255) NOT NULL,
`seed_id` int(64) NOT NULL,
`category_id` int(64) NOT NULL,
`title` varchar(255) NOT NULL,
`description` text,
`content` longtext,
`publishing_date` varchar(255) NOT NULL,
`link` text NOT NULL,
`status` int(1) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `hash` (`hash`),
FOREIGN KEY (`seed_id`) REFERENCES `af_seeds`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (`category_id`) REFERENCES `af_categories`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE
MySQL said:
1005 - Can't create table 'estafeed_rss.af_feeds' (errno: 150) (Details…)
--
-- Table structure for table `af_categories`
--
CREATE TABLE IF NOT EXISTS `af_categories` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`status` int(1) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
-- --------------------------------------------------------
--
-- Table structure for table `af_seeds`
--
CREATE TABLE IF NOT EXISTS `af_seeds` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
`category_id` int(64) NOT NULL,
`loading_times` int(64) NOT NULL,
`status` int(1) NOT NULL,
`loading_each` int(64) NOT NULL,
`last_loading` int(64) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

#1452 - Cannot add or update a child row: a foreign key constraint fails ... Jutts

I have created tables in MySQL Workbench as shown below :
Categories Table
CREATE TABLE IF NOT EXISTS `categories` (
`categoryId` int(11) primary key auto_incremnt NOT NULL,
`categoryName` varchar(200) DEFAULT NULL,
`categoryDescription` varchar(200) DEFAULT NULL,
`categoryPicture` varchar(100) DEFAULT NULL,
`PostDate` varchar(255) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT '1',
`LastInsertID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=45 ;
Products Table
CREATE TABLE IF NOT EXISTS `products` (
`proId` int(11) NOT NULL,
CONSTRAINT `categoryId` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ,
`proName` varchar(100) DEFAULT NULL,
`proPath` varchar(90) DEFAULT NULL,
`proPrice` float DEFAULT NULL,
`proQuantity` decimal(38,0) DEFAULT NULL,
`proImage` varchar(100) DEFAULT NULL,
`PostDate` varchar(50) NOT NULL,
`status` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
I'm having a bit of a strange problem, I'm trying to add a foreign key
to one table that references another, but it is failing for some
reason. With my limited knowledge of MySQL, the only thing that could
possibly be suspect is that there is a foreign key on a different
table referencing the one I am trying to reference.
Please help me out as soon as possible .
Thanks in advance
You should create the categoryId column before assigning it in a foreign key,
categoryId int(11) NOT NULL,
CREATE TABLE IF NOT EXISTS `products` (
`proId` INT(11) NOT NULL,
`categoryId` INT ,
`proName` VARCHAR(100) DEFAULT NULL,
`proPath` VARCHAR(90) DEFAULT NULL,
`proPrice` FLOAT DEFAULT NULL,
`proQuantity` DECIMAL(38,0) DEFAULT NULL,
`proImage` VARCHAR(100) DEFAULT NULL,
`PostDate` VARCHAR(50) NOT NULL,
`status` INT(11) NOT NULL DEFAULT '1',
FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`)
) ENGINE=INNODB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Missing Value in form when creating a table in phpmyadmin SQL

I am trying to make a table but every time I type my sql code I get an error "Missing value in form". I am sure there is no problem with the code rather something else.
Here is my code
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
)
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

How to check if a given data exists in onther table MySql?

I have two tables in MySql Database:
Captain(captain.email)
Members(member.email)
I want when captain table insert data in captain.email then check If members table data in members.email are already exit then data in captain.email not insert in captain table.
How it is possible ?
1.Captain :
CREATE TABLE `captain` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`username_canonical` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`email_canonical` varchar(255) NOT NULL,
`enabled` tinyint(1) NOT NULL,
`salt` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`last_login` datetime DEFAULT NULL,
`locked` tinyint(1) NOT NULL,
`expired` tinyint(1) NOT NULL,
`expires_at` datetime DEFAULT NULL,
`confirmation_token` varchar(255) DEFAULT NULL,
`password_requested_at` datetime DEFAULT NULL,
`roles` longtext NOT NULL COMMENT '(DC2Type:array)',
`credentials_expired` tinyint(1) NOT NULL,
`credentials_expire_at` datetime DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_957A647992FC23A8` (`username_canonical`),
UNIQUE KEY `UNIQ_957A6479A0D96FBF` (`email_canonical`)
)
2.Members :
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`team_id` int(11) DEFAULT NULL,
`fos_user_id` int(11) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`mobile` varchar(45) DEFAULT NULL,
`role` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `email_2` (`email`),
KEY `IDX_45A0D2FF296CD8AE` (`team_id`),
KEY `IDX_45A0D2FF8C20A0FB` (`fos_user_id`),
CONSTRAINT `FK_45A0D2FF296CD8AE` FOREIGN KEY (`team_id`) REFERENCES `team` (`id`),
CONSTRAINT `FK_45A0D2FF8C20A0FB` FOREIGN KEY (`fos_user_id`) REFERENCES `fos_user` (`id`)
)
There is no way to enforce such constraint.
Using declarative referential integrity (DRI) you could create a table that contains all of the columns that you need to build a unique key on.

MySQL count from one table while joining another

I don't know if it's just been a long day or what, but I cannot figure out the query that I need to run here. We have two tables - One for leads generated and one for reports. The leads table has basic lead info, along with the Source (Campaign) of the lead. However, we need to know the number of leads that an ACCOUNT has received within a date range. Here is the relevant table structure:
client_leads:
id
source
date
client_reports:
account
campaign
date
The 'source' column contains the same values as the 'campaign' column. So, how would I achieve the following:
Say there are 10 leads in the leads table, each with the campaign that generated the lead. There are 10 accounts in the reports table, each with hundreds of campaigns. I need to list each account and how many leads it has in the leads table.
I just can't get the logic straight in my head. I've tried everything that I can think of and it's just not working out for me. If you need further explanation, let me know. I'm trying to describe the problem to the best of my ability.
Edit:
CREATE TABLE `client_leads` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`site_id` int(10) DEFAULT NULL,
`ip` varchar(255) DEFAULT NULL,
`source` varchar(255) DEFAULT NULL,
`kw` varchar(255) DEFAULT NULL,
`adgroup` varchar(255) DEFAULT NULL,
`time` time DEFAULT NULL,
`date` date DEFAULT NULL,
`dayweek` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
`postal_code` char(5) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`preferred_phone` varchar(10) DEFAULT NULL,
`alternate_phone` varchar(10) DEFAULT NULL,
`level_of_education` varchar(255) DEFAULT NULL,
`program_of_interest` varchar(255) DEFAULT NULL,
`organic` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `site_id` (`site_id`),
KEY `date_indeces` (`time`,`date`,`dayweek`) USING BTREE,
CONSTRAINT `site_id` FOREIGN KEY (`site_id`) REFERENCES `client_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8
CREATE TABLE `client_reports` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`account` varchar(255) DEFAULT NULL,
`friendly_name` varchar(255) DEFAULT NULL,
`sites_id` int(10) DEFAULT NULL,
`service` varchar(255) DEFAULT NULL,
`date` date DEFAULT NULL,
`campaign` varchar(255) DEFAULT NULL,
`adgroup` varchar(255) DEFAULT NULL,
`keyword` varchar(255) DEFAULT NULL,
`impressions` int(10) DEFAULT NULL,
`clicks` int(10) DEFAULT NULL,
`cost` float DEFAULT NULL,
`max_cpc` float DEFAULT NULL,
`avg_pos` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `stats` (`impressions`,`clicks`,`cost`),
KEY `date` (`date`),
KEY `campaign` (`campaign`),
KEY `adgroup` (`adgroup`),
KEY `keyword` (`keyword`),
KEY `service` (`service`),
KEY `sites_id` (`sites_id`),
CONSTRAINT `sites_id` FOREIGN KEY (`sites_id`) REFERENCES `client_sites` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=109167 DEFAULT CHARSET=utf8
Edit Again:
client_reports table data viewable at http://pastebin.com/T532W3Eq
client_leads table data viewable at http://pastebin.com/9cjWEvck
SELECT cr.account, cr.campaign, cr.date, COUNT(cl.id) AS number_of_leads
FROM client_reports cr
LEFT JOIN client_leads cl
ON cl.source = cr.campaign
GROUP BY cl.source