SQL Key Column doesn't exist when ALTER TABLE - mysql

ALTER TABLE book
ADD FOREIGN KEY (category_id)
REFERENCES category (category_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
Whenever I run this ALTER TABLE statement, it keeps coming back with, "Error Code: 1072. Key column 'category_id' does not exist."
I'm sure this is an easy fix, but I'm having problems adding a foreign key to one of my tables. I just started learning SQL last week so any help would be appreciated!
My CREATE TABLE statements are as follows:
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
This Table has the Primay Key category_id but below table has no Column
as "category_id" which is being referenced in the Alter Command.Add
Column "category_id in below table and then execute Alter command.
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null
) ENGINE=InnoDB;

Here is the complete script then
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null,
category_id INT not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
ALTER TABLE book
ADD FOREIGN KEY (category_id)
REFERENCES category (category_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

Related

How to copy column values to a new SQL table, remove duplicates and create mappings?

I have a table Excerpt
CREATE TABLE IF NOT EXISTS excerpt(
excerptID INT UNSIGNED NOT NULL AUTO_INCREMENT,
author VARCHAR(45) NOT NULL,
title VARCHAR(255) NOT NULL,
text VARCHAR(2500) NOT NULL,
comments VARCHAR(2500) NOT NULL,
PRIMARY KEY (excerptID)
) ENGINE=INNODB CHARACTER SET utf8mb4;
and want to remove its column Author while distributing its (often duplicate) values and information about these values into two new tables Author
CREATE TABLE IF NOT EXISTS author(
authorID INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL ,
PRIMARY KEY (authorID)
) ENGINE=INNODB CHARACTER SET utf8mb4;
and AuthorMap:
CREATE TABLE IF NOT EXISTS authormap (
excerptID INT UNSIGNED NOT NULL,
authorID INT UNSIGNED NOT NULL,
PRIMARY KEY (excerptID, authorID),
CONSTRAINT authorMapFK FOREIGN KEY (excerptID) REFERENCES excerpt (excerptID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT authorFK FOREIGN KEY (authorID) REFERENCES author (authorID)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=INNODB;
All duplicates should be removed and mappings between excerptID and authorID placed into AuthorMap, i.e eventually all unique author names should be in the table Author, their IDs created and all ExcerptID should be copied to AuthorMap alongside with the newly created AuthorIDs.
How should I do it?
You can create the table authors as:
create table authors (
author_id int auto_increment primary key,
name varchar(255) -- or whatever
);
insert into authors (name)
select distinct author
from excerpt;
Then create the author map table as:
create table excerptAuthors (
excerptAuthorsId int auto_icnrement primary key,
excerptid int,
authorid int
);
And finally:
insert into excerptAuthors (excerptId, authorId)
select e.excerptId, a.authorId
from excerpts e join
authors a
on e.author = a.name;

mysql creating table foreign key

I've created a table :
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
,uName VARCHAR(50)
,uSecondName VARCHAR(50)
,eMail VARCHAR(50)
)
After this I even insert some data without any problems. But when I've tried to create new table with FOREIGN KEY referenced to users.id I've got an error:
CREATE TABLE posts(
id INT(6) AUTO_INCREMENT NOT NULL
,pTitle VARCHAR(155) NOT NULL DEFAULT 'not_set'
,pText TEXT
,pAuthor INT(6)
,PRIMARY KEY(id)
,CONSTRAINT fk_PerAuthor FOREIGN KEY (pAuthor)
REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);
Did I miss something?

trying to create a table relationship with mysql v5.5

Hi all trying to create a relationship between two tables with mysql v5.5 Curious How I go about this...
CREATE TABLE posts(id INT AUTO_INCREMENT,
title varchar(100) NOT NULL,
body varchar(500) NOT NULL,
PRIMARY KEY (id) );
//posts_id not null creating error?
CREATE TABLE comments(id INT AUTO_INCREMENT,
comment varchar(250)NOT NULL,
posts_id NOT NULL,
PRIMARY KEY (id) );
I would like to have a posts_id in comments table share the relationship with the post table id in comments table.
You haven't set posts_id type:
posts_id int(11) NOT NULL
Then your comments table should look like this:
CREATE TABLE IF NOT EXISTS comments (
id int(11) NOT NULL AUTO_INCREMENT,
comment varchar(250) NOT NULL,
posts_id int(11) NOT NULL,
PRIMARY KEY (id),
KEY posts (posts_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And finally, a foreign relation:
ALTER TABLE comments
ADD CONSTRAINT comments_ibfk_1 FOREIGN KEY (posts_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE
Give it a type, like varchar, or INT
posts_id varchar(20) NOT NULL

Errno:150 in MySQL Database

Have an error in my SQL code, but could not understand, where it is. Please, help me to solve that. Here is my code:
CREATE TABLE IF NOT EXISTS Records (
record_id int(11) NOT NULL AUTO_INCREMENT,
record_year year(4) NOT NULL,
record_quarter int(1) NOT NULL,
profit_tax int(11) NOT NULL,
PRIMARY KEY (record_id, record_year, record_quarter),
UNIQUE(record_year, record_quarter)
);
CREATE TABLE IF NOT EXISTS ProductsList (
product_id int(11) NOT NULL AUTO_INCREMENT,
product_name varchar(24) NOT NULL,
PRIMARY KEY (product_id)
);
An error is in that table:
CREATE TABLE IF NOT EXISTS RecordsProducts (
recordproduct_id int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (recordproduct_id),
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
);
referencing column should be INDEX
referencing column should have same data type of referenced column
both referencing, referenced table should be InnoDB
CREATE TABLE IF NOT EXISTS RecordsProducts (
...
...
record_id int NOT NULL, <=== check data type
product_id int NOT NULL,
INDEX(record_id), <===== check INDEXed or not
INDEX(product_id), <====
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
) ENGINE = InnoDB; <=== add 'ENGINE=InnoDB' explicitly

On delete set null creating table in mysql

I am to create a table named patientmedicinecategory . as given below:-
CREATE TABLE patientmedicinecategory
(
pmc int(11) NOT NULL AUTO_INCREMENT,
patient_id int(11) NOT NULL,
med_id int(3) NOT NULL,
cat_id int(3) NOT NULL,
testname varchar(100) NOT NULL,
PRIMARY KEY(pmc),
UNIQUE KEY(patient_id, med_id,cat_id,testname),
FOREIGN KEY(patient_id) REFERENCES patient(patient_id) ON UPDATE CASCADE ON DELETE
CASCADE,
FOREIGN KEY(med_id) REFERENCES medicine(med_id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY(cat_id) REFERENCES category(cat_id) ON UPDATE CASCADE ON DELETE SET NULL
)ENGINE=InnoDB;
my patient table is :-
CREATE TABLE patient
(
patient_id int NOT NULL AUTO_INCREMENT,
salutation ENUM('Mr.','Mrs.','Miss.','Dr.') NOT NULL,
name varchar(50) NOT NULL,
email_id varchar(100),
year int(4) NOT NULL,
month int(3),
day int(3),
sex ENUM('M','F') NOT NULL,
contactno varchar(50),
PRIMARY KEY(patient_id)
)ENGINE=InnoDb;
medicine table is :-
CREATE TABLE medicine
(
med_id int(3) NOT NULL,
name varchar(40) NOT NULL,
PRIMARY KEY (med_id)
)ENGINE=InnoDB;
category table is :-
CREATE TABLE category
(
cat_id int(3) NOT NULL,
name varchar(20) NOT NULL,
PRIMARY KEY (cat_id)
)ENGINE=InnoDB;
But when i try to create this it give the error:-
ERROR 1005 (HY000): Can't create table 'nutech3.patientmedicinecategory' (errno: 150)
I have tried a lot but could not success . please help me . Thanks in advance
If you log in as root and run SHOW ENGINE INNODB STATUS you'll see that the exact error message is this:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
130405 11:55:42 Error in foreign key constraint of table test/patientmedicinecategory:
FOREIGN KEY(cat_id) REFERENCES category(cat_id) ON UPDATE CASCADE ON DELETE SET NULL
)ENGINE=InnoDB:
You have defined a SET NULL condition though some of the
columns are defined as NOT NULL.
This is the offending line:
FOREIGN KEY(cat_id) REFERENCES category(cat_id) ON UPDATE CASCADE ON DELETE SET NULL
MySQL cannot make patientmedicinecategory.cat_id NULL because its defined this way:
cat_id int(3) NOT NULL,
Your options are either:
Allow cat_id to be NULL
Set a different ON DELETE condition