Mysql Error Code 1072
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Unable to create foreign key in mysql; ERROR 1072 up vote 3 down vote favorite I've a user table which looks like this- mysql> SHOW COLUMNS FROM Users; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | user_id | int(10) | NO | PRI | NULL | auto_increment | | username | varchar(50) | YES | | NULL | | | password | varchar(255) | YES | | NULL | | | email | varchar(255) | YES | | NULL | | | phone | varchar(255) | YES | | NULL | | I am trying to create a new table like this- create table jobs (id int, FOREIGN KEY (user_id) REFERENCES Users(user_id)) ENGINE=INNODB; but I am getting error- ERROR 1072 (42000): Key column 'user_id' doesn't exist in table I am sure I am missing something very basic. Please suggest what could be wrong. mysql sql share|improve this question asked Jun 30 '12 at 23:07 user837208 97541840 3 There isn't a column user_id in jobs... –Ben Jun 30 '12 at 23:09 shouldn't it be created as part of jobs table creation? –user837208 Jun 30 '12 at 23:12 You're not creating it though, you need to specify the column name and data-type so that the foreign key has something to use. –Ben Jun 30 '12 at 23:18 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted Try this: create table jobs ( id int, user_id int, FOREIGN KEY (user_id) REFERENCES Users(user_id) ) ENGINE=INNODB; The first user_id in foreign key constraint refers to the table where the contraint is defined and the second refers to the table where it is pointing to. So you need a field user_id in your jobs table, too. share|improve this answer answered Jun 30 '12 at 23:18 Fabian Barney
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a http://stackoverflow.com/questions/11278343/unable-to-create-foreign-key-in-mysql-error-1072 community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up #1072 key column doesn't exist error with MySQL foreign key up vote 2 down vote favorite I've searched for answers might work, but didn't fix. I've created tables like this and when created table http://stackoverflow.com/questions/13812863/1072-key-column-doesnt-exist-error-with-mysql-foreign-key 'hobbies' got error 1072. CREATE TABLE IF NOT EXISTS `project_r`.`user` ( `user_id` INT NOT NULL , `password` VARCHAR(45) NOT NULL , `RealName` VARCHAR(45) NOT NULL , `FamilyName` VARCHAR(45) NOT NULL , `birthdate` VARCHAR(45) NOT NULL , `Homecity` VARCHAR(45) NOT NULL , `school_id` INT NOT NULL , `school_enteryear` INT NOT NULL , `email` VARCHAR(45) NULL , `activestat` VARCHAR(45) NULL , `onlinestat` VARCHAR(45) NULL , `regtime` DATETIME NOT NULL , `avatar` VARCHAR(45) NULL , `status` VARCHAR(45) NULL , `desc` VARCHAR(45) NULL , `self_comment` VARCHAR(45) NULL , `userMsg_id` INT NOT NULL , PRIMARY KEY (`user_id`) , INDEX `fk_user_school1_idx` (`school_id` ASC) , CONSTRAINT `fk_user_school1` FOREIGN KEY (`school_id` ) REFERENCES `project_r`.`school` (`school_id` ) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB; then and create table 'hobbies' CREATE TABLE IF NOT EXISTS `project_r`.`hobbies` ( `hobby_user_id` INT NOT NULL , `favorite_music` VARCHAR(45) NULL , `favorite_game` VARCHAR(20) NULL , `favorite_film` VARCHAR(45) NULL , `favorite_book` VARCHAR(45) NULL , `favorite_sport` VARCHAR(45) NULL , `favorite_cartoon` VA
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies http://stackoverflow.com/questions/27221888/mysql-error-1072 of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a mysql error minute: Sign up mysql error 1072 up vote 0 down vote favorite COMMIT WORK ; DROP DATABASE IF EXISTS Campaign_Database; CREATE DATABASE Campaign_Database; USE Campaign_Database; DROP TABLE IF EXISTS CONTRIBUTOR ; DROP TABLE IF EXISTS CAMPAIGN ; DROP TABLE IF EXISTS PLEDGE ; CREATE TABLE CONTRIBUTOR ( CONTRIBUTOR_ID VARCHAR(50) primary key, Address char(50) not null, City char(50) not null, State char(2) not mysql error code null, Zip INTEGER(6) not null, Home_Phone char (15) )ENGINE = InnoDB; ; CREATE TABLE CAMPAIGN ( CAMPAIGN_ID VARCHAR(50) primary key, Chair_First_Name char (50) not null, Chair_Last_Name char (50) not null, Launch_Date char (11) not null, End_Date char (11) not null, Fundraising_Goal INTEGER NOT NULL )ENGINE = InnoDB; ; CREATE table PLEDGE ( FOREIGN KEY (CONTRIBUTOR_ID) REFERENCES CONTRIBUTOR (CONTRIBUTOR_ID), FOREIGN KEY (CAMPAIGN_ID) REFERENCES CAMPAIGN (CAMPAIGN_ID), Date_Pledged char(11) not null primary key, Amount INTEGER NOT NULL, PAID char(1) not null CHECK (PAID in ('Y','N')), Payment_Date char(11) )ENGINE = InnoDB; ; COMMIT WORK ; This current is the mysql coding I am using for a finals assignment. I have tried several methods but was not successful in creating the two foreign keys within the 'pledge' table. I would be appreciated if I can at least know what went wrong. Thank you. mysql sql share|improve this question asked Dec 1 '14 at 4:53 J_Chong 1 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote You need a CONTRIBUTOR_ID, CAMPAIGN_ID fields in the PLEDGE table. CREATE table PLEDGE ( CONTRIBUTOR_ID VARCHAR(50), CAMPAIGN_ID VARCHAR(