Adding Foreign Key Error - mysql

I want to add a foreign key from Table Customers, row= "Customer ID" to Table Pet, row= "Customer ID".
-- Table structure for table `Customers`
CREATE TABLE IF NOT EXISTS `Customers` (
`CustomerID` varchar(50) NOT NULL,
`Fname` varchar(50) DEFAULT NULL,
`LName` varchar(20) DEFAULT NULL,
`Tel` varchar(20) DEFAULT NULL,
`Fax` varchar(20) DEFAULT NULL,
`CustType` varchar(20) DEFAULT NULL,
`AdState` varchar(50) DEFAULT NULL,
`City` varchar(20) DEFAULT NULL,
`Zip` varchar(20) DEFAULT NULL,
`Street` varchar(20) DEFAULT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `Customers`
INSERT INTO `Customers` (`CustomerID`, `Fname`, `LName`, `Tel`, `Fax`, `CustType`, `AdState`, `City`, `Zip`, `Street`) VALUES
('AC001', 'All', 'Creatures', '206 555-6622', '206 555-7854', '2', 'WA', 'Tall Pines', '98746', '21 Grace St.'),
('AD001', 'Johnathan', 'Adams', '206 555 7623', '206 555 8855', '1', 'WA', 'Mountain View', '984101012', '66 10th St'),
('AD002', 'William', 'Adams', '503 555 7623', '503 555 7319', '1', 'OR', 'Lakewille', '9740110011', '1122 10th_St'),
('AK001', 'Animal', 'Kingdom', '208 555 7108', '', '2', 'ID', 'Borderville', '834835646', '15 Marlin Lane');
CREATE TABLE IF NOT EXISTS `Pet` (
`ID` varchar(50) NOT NULL,
`CustomerID` varchar(50) NOT NULL,
`Gender` varchar(20) DEFAULT NULL,
`Race` varchar(20) DEFAULT NULL,
`Name` varchar(20) DEFAULT NULL,
`Kind` varchar(20) DEFAULT NULL,
`Birthday` varchar(20) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `Pet`
INSERT INTO `Pet` (`ID`, `CustomerID`, `Gender`, `Race`, `Name`, `Kind`, `Birthday`) VALUES
('AC001-01', '0', 'M', 'Long Ear', 'Bobo', 'Rabbit', '4/8/92'),
('AC001-02', '0', 'F', 'Chameleon', 'Presto Chango', 'Lizard', '5/1/92'),
('AC001-03', '0', 'M', '', 'Stinky', 'Skunk', '8/1/91'),
('AC001-04', '0', 'M', 'German Shepherd', 'Fido', 'Dog', '6/1/90'),
('AD001-01', '0', 'F', 'Potbelly', 'Patty', 'Pig', '2/15/91'),
('AD001-02', '0', 'M', 'Palomino', 'Rising Sun', 'Horse', '4/10/90'),
('AD002-01', '0', 'F', 'Mixed', 'Dee Dee', 'Dog', '2/15/91'),
('AK001-03', '0', 'M', '', 'Jerry', 'Rat', '2/1/88'),
('AK001-07', '0', 'M', 'Beagle', 'Luigi', 'Dog', '8/1/92');
This is the code that I have been using to add the foreign key:
ALTER TABLE Pet ADD CONSTRAINT Pet_FK
FOREIGN KEY (CustomerID) REFERENCES Customers (CustomerID);
And the error message from this is:
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`hospital`.`#sql-523_76e`, CONSTRAINT `Pet_FK` FOREIGN KEY (`CustomerID`)
REFERENCES `Customers` (`CustomerID`))
I am quite a beginner with database and I have no idea what I should try next.
I think that's all. Im still new to this stackoverflow so if I missed any necessary information please tell me and I will add it.
UPDATE***
ALTER TABLE Customers ADD CONSTRAINT Customers_FK
FOREIGN KEY (CustomerID) REFERENCES Pet (CustomerID);
I swapped some positions and the error code I recieve is:
#1215 - Cannot add foreign key constraint

Simple one.
There is an row that contains the CustomerID that can't be matched. So first you need to remove/edit/handle the entry and than add a foreign key.

The CustomerID you're trying to enter in PETS table, does not exist in CUSTOMERS table, and that is why your Foreign Key constraint fails and throws error.
You need to ensure that the CustomerIDs you're entering in your Pets table, exist in Customers table OR simply insert NULL in the PETS.CUSTOMERID field

Enterx is right.
For being able to detect not matching row :
SELECT * FROM Pet p WHERE (SELECT COUNT(*) FROM Customers c WHERE c.CustomerID=p.CustomerID)=0
Just change SELECT * by DELETE for deleting missmatching Pet entry.
You can UPDATE Pet.CustomerID to NULL too. But you have to define CustomerID, from Pet table, with NULL option (and not NOT NULL)

It looks like to me that you inserted values in table Pet's column ID when they should have been inserted in CustomerID and vice-versa.
By the way, it's not really good to have IDs as VARCHARs, specially when they are foreign keys. This makes queries processing slower, although your tables don't look like they'll have a huge number of rows for this to make a difference. Anyway, it's just an observation. I would consider have artificial int primary keys in my tables.
EDIT
I had misread table Pet's values. The other answers here are right. You need to update those 0 values in CustomerID column to match existing CustomerIDs in Customer table or delete them, otherwise you'll get an error when trying to create the FK.

try this
CREATE TABLE IF NOT EXISTS `Pet` (
`ID` varchar(50) NOT NULL,
FOREIGN KEY (`CustomerID`) REFERENCES Customers(CustomerID) varchar(50) NOT NULL,
`Gender` varchar(20) DEFAULT NULL,
`Race` varchar(20) DEFAULT NULL,
`Name` varchar(20) DEFAULT NULL,
`Kind` varchar(20) DEFAULT NULL,
`Birthday` varchar(20) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

MySQL joining 2 tables in to one main table

I have 2 tables 'cat' and 'sub_cat' and these two tables should join or something with main table 'product'
I tried all joining methods and non of them gave me right result I want.
I'm sure there is a method. I don't know what to call.
Sample SQL
This is how the last query should be
forget about the normalization theories and every thing and I just want the last query to be like this or mysql method that I can use on this.
cat_id cannot be duplicate
s_id should also cannot be duplicate
like in third row there can be: cat_id but no s_id the s_id should be null
if there is no cat_id and no s_id both should be null like fourth row
p_id can be duplicate
can't use group by or distinct cause it doesn't give null values then as i know
only method i got closer is using left joining both cat and sub_cat to prod table but it gives me duplicate cat_id and s_id and can't use group by or distinct on this cause there should be null values.
here is the test data
CREATE TABLE IF NOT EXISTS `cat` (
`product_id` int(11) DEFAULT NULL,
`cat_id` int(11) DEFAULT NULL,
`cat_name` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `cat` (`product_id`, `cat_id`, `cat_name`) VALUES
(1, 1, 'cat1'),
(2, 2, 'cat2'),
(3, 3, 'cat3'),
(1, 4, 'ca4');
CREATE TABLE IF NOT EXISTS `prod` (
`product_id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `prod` (`product_id`, `name`) VALUES
(1, 'prod1'),
(2, 'prod2'),
(3, 'pro3'),
(4, 'prod4');
CREATE TABLE IF NOT EXISTS `sub_cat` (
`product_id` int(11) DEFAULT NULL,
`sub_cat_id` int(11) DEFAULT NULL,
`sub_cat_name` varchar(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `sub_cat` (`product_id`, `sub_cat_id`, `sub_cat_name`) VALUES
(1, 1, 'sub cat 1'),
(2, 2, 'sub cat 2'),
(1, 3, 'sub3');
I have done a similar thing in this one.prop_cat acts as you Category table,prop_subcat as your subcategory table and property as you product.
CREATE TABLE `prop_cat` (
`pcat_id` int(11) NOT NULL AUTO_INCREMENT,
`pcat_name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`pcat_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
CREATE TABLE `prop_subcat` (
`psubcat_id` int(11) NOT NULL,
`pcat_id` int(11) NOT NULL,
`psubcat_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`psubcat_id`,`pcat_id`),
KEY `pspc_idx` (`pcat_id`),
CONSTRAINT `catsub` FOREIGN KEY (`pcat_id`) REFERENCES `prop_cat` (`pcat_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `property` (
`prop_id` int(11) NOT NULL AUTO_INCREMENT,
`prop_name` varchar(25) DEFAULT NULL,
`price` double DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
`image` varchar(60) DEFAULT NULL,
`area` double DEFAULT NULL,
`psubcat_id` int(11) DEFAULT NULL,
`description` text,
PRIMARY KEY (`prop_id`),
KEY `psub_idx` (`psubcat_id`),
CONSTRAINT `psub` FOREIGN KEY (`psubcat_id`) REFERENCES `prop_subcat` (`pcat_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
SELECT
prop_cat.`pcat_id` AS prop_cat_pcat_id,
prop_cat.`pcat_name` AS prop_cat_pcat_name,
prop_subcat.`psubcat_id` AS prop_subcat_psubcat_id,
prop_subcat.`pcat_id` AS prop_subcat_pcat_id,
prop_subcat.`psubcat_name` AS prop_subcat_psubcat_name,
property.`prop_id` AS property_prop_id,
property.`prop_name` AS property_prop_name,
property.`price` AS property_price,
property.`location` AS property_location,
property.`image` AS property_image,
property.`area` AS property_area,
property.`psubcat_id` AS property_psubcat_id,
property.`description` AS property_description
FROM
`prop_cat` prop_cat INNER JOIN `prop_subcat` prop_subcat ON prop_cat.`pcat_id` = prop_subcat.`pcat_id`
INNER JOIN `property` property ON prop_subcat.`pcat_id` = property.`psubcat_id`

Foreign Key Constraint error for delete data form table

i am doing a project on ONLINE EXAMINATION which is copied from my senior but while execute i got some error. attach my code here and the error msg.
table #:
CREATE TABLE IF NOT EXISTS `question` (
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`question` varchar(500) DEFAULT NULL,
`optiona` varchar(100) DEFAULT NULL,
`optionb` varchar(100) DEFAULT NULL,
`optionc` varchar(100) DEFAULT NULL,
`optiond` varchar(100) DEFAULT NULL,
`correctanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
`marks` int(11) DEFAULT NULL,
PRIMARY KEY (`testid`,`qnid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table question
INSERT INTO `question` (`testid`, `qnid`, `question`, `optiona`, `optionb`, `optionc`, `optiond`, `correctanswer`, `marks`) VALUES
(1, 1, 'why use photoshop', 'image retouching', 'image making', 'image destroying', 'color coreection', 'optiona', 1),
(2, 1, 'java is', 'fish fry', 'software language', 'programing language', 'web maker', 'optionc', 1),
(2, 2, 'what is vaja', 'bengali', 'kokl', 'khsd', 'kojsgf', 'optiona', 1);
Table ###
--
-- Table structure for table student
CREATE TABLE IF NOT EXISTS `student` (
`stdid` bigint(20) NOT NULL,
`stdname` varchar(40) DEFAULT NULL,
`stdpassword` varchar(40) DEFAULT NULL,
`emailid` varchar(40) DEFAULT NULL,
`contactno` varchar(20) DEFAULT NULL,
`address` varchar(40) DEFAULT NULL,
`city` varchar(40) DEFAULT NULL,
`pincode` varchar(20) DEFAULT NULL,
PRIMARY KEY (`stdid`),
UNIQUE KEY `stdname` (`stdname`),
UNIQUE KEY `emailid` (`emailid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table studentquestion
CREATE TABLE IF NOT EXISTS `studentquestion` (
`stdid` bigint(20) NOT NULL DEFAULT '0',
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`answered` enum('answered','unanswered','review') DEFAULT NULL,
`stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
PRIMARY KEY (`stdid`,`testid`,`qnid`),
KEY `testid` (`testid`,`qnid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Constraints for table `question`
--
ALTER TABLE `question`
ADD CONSTRAINT `question_ibfk_1` FOREIGN KEY (`testid`) REFERENCES `test` (`testid`);
--
-- Constraints for table `studentquestion`
--
ALTER TABLE `studentquestion`
ADD CONSTRAINT `studentquestion_ibfk_1` FOREIGN KEY (`stdid`) REFERENCES `student` (`stdid`),
ADD CONSTRAINT `studentquestion_ibfk_2` FOREIGN KEY (`testid`, `qnid`) REFERENCES `question` (`testid`, `qnid`);
ERROR MESSAGE
Cannot delete or update a parent row: a foreign key constraint fails (oes.studentquestion, CONSTRAINT studentquestion_ibfk_2 FOREIGN KEY (testid, qnid) REFERENCES question (testid, qnid))
Perhaps it is not the full script you have.
Most probably the data is inserted in wrong order. The error occurs because the table studentquestion already has the data for the testid and qnid fields which is not present is the question master table. You have to reorder your insert-statements so that master tables are populated first and the detail tables after them.

Mysql how to add more than one value (in one box)

I am trying to add more than one value
INSERT INTO Requierments (item_ID, SName) VALUES (
05, 'Exotic Weapons''basic weapon');
is that possible? i have all ready tried | and & but i am not allowed to do that.
the idea is to demonstrate that item_ID requires more than one Sname.
and if no SName are needed, how do i code that, in the alter table it shows me that they are by default '0' for item_ID and '' for Sname, but when i try:
INSERT INTO Requierments (item_ID, SName) VALUES (
02, '');
or
INSERT INTO Requierments (item_ID, SName) VALUES (
02, );
error occurs. item_ID and SName are the primary key and foreign key to two different tables
CREATE TABLE `requierments` (
`item_ID` int(11) NOT NULL DEFAULT '0',
`SName` varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`item_ID`,`SName`),
KEY `SName` (`SName`),
CONSTRAINT `requierments_ibfk_1` FOREIGN KEY (`item_ID`) REFERENCES `item` (`ID`),
CONSTRAINT `requierments_ibfk_2` FOREIGN KEY (`SName`) REFERENCES `talents` (`SkillName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `talents` (
`SkillName` varchar(40) NOT NULL DEFAULT '',
`Bonus` varchar(30) DEFAULT NULL,
`Description` varchar(90) DEFAULT NULL,
`R_Str` int(11) DEFAULT NULL,
`R_WS` int(11) DEFAULT NULL,
`R_BS` int(11) DEFAULT NULL,
`R_Fel` int(11) DEFAULT NULL,
`R_Per` int(11) DEFAULT NULL,
`R_Int` int(11) DEFAULT NULL,
`R_Agi` int(11) DEFAULT NULL,
`R_WP` int(11) DEFAULT NULL,
`Talent_requiret` varchar(40) DEFAULT NULL,
PRIMARY KEY (`SkillName`),
KEY `Talent_requiret` (`Talent_requiret`),
CONSTRAINT `talents_ibfk_1` FOREIGN KEY (`Talent_requiret`) REFERENCES `talents` (`SkillName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `item` (
`ID` int(11) NOT NULL DEFAULT '0',
`Name_` varchar(30) DEFAULT NULL,
`Weight` int(11) DEFAULT NULL,
`Value_` int(11) DEFAULT NULL,
`Availability` varchar(30) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here are the 3 tables where i want to store in the requirement table what item_ID requires to be used under SName, since one item can be required to have more than one SName or none, it get confused on what to do.
The 'Exotic Weapons''basic weapon' syntax is interpreted as 'Exotic Weapons'basic weapon' (see the manual page on String Literals). You can store both, if you separate them with a special character like a comma or something ('Exotic Weapons,basic weapon'), but this is a bad idea, for the reasons that follow.
Anytime you are tempted to store multiple values in one column, you should have a new table to store each value and link it to the item_ID in question. This is called "normalization" - it makes your database more stable, your application code simpler, and everything just more likely to work as you move forward and modify things. Search for "database normalization" and read about how to do the kind of thing you are trying to do.
EDIT: It's still a little hard to tell what you're trying to do, but it looks like you just want to store multiple tuples in items. Example:
INSERT INTO Requierments (item_ID, SName) VALUES
(5, 'Exotic Weapons'),
(5, 'basic weapon');
Note that it should be 5, not 05. That's why INSERT INTO Requierments (item_ID, SName) VALUES ( 02, ''); fails. Also, you can't just omit a value and have blank space, like in your other example: INSERT INTO Requierments (item_ID, SName) VALUES ( 02, );
Honestly, it's very hard to tell what you're even trying to do here, and there seems to be some confusion about the basics of table structure and SQL syntax. For example, this statement is very confusing and unclear: "since one item can be required to have more than one SName or none, it get confused on what to do." If you can provide some more clarity about how you want the tables to relate to each other and where you are stuck, you will get more help from the SO community.

The way foreign keys work when there are 3 tables dependent on each other

I have tables
items
products
brands
their contents:
products:
- samsung galaxy s2
- iphone 5
brands
- samsung
- apple
the difference between an item and a product is as follows:
product is lets say iPhone.
item is a certain user's certain iPhone with its own properties like color and purchase price.
the product iPhone has a brand/manufacturer of Apple.
when inserting a new item, I want the database to get the brand from the product the item belongs to, so my foreign key is set up like this:
'db_name`.'products'.`productBrand`
I have two brands ATM - Samsung and Apple.
When I try to insert a new item via phpMyAdmin's interface and I get to the itemBrand column, the dropdown field only allows me one option - 1 (Samsung), no matter if I've chosen product 1 or 2 (Samsung Galaxy or iPhone5) at the itemGenericProduct column.
What am I doing wrong?
Here is some more detailed info:
CREATE TABLE IF NOT EXISTS `brands` (
`brandId` int(11) NOT NULL AUTO_INCREMENT,
`brandName` varchar(30) NOT NULL,
`brandLogo` varchar(100) NOT NULL,
`brandDescription` text NOT NULL,
PRIMARY KEY (`brandId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `brands`
--
INSERT INTO `brands` (`brandId`, `brandName`, `brandLogo`, `brandDescription`) VALUES
(1, 'Samsung', '', 'Manufacturer of ...'),
(2, 'Apple', '', 'American high-tech company ...');
--
-- Table structure for table `items`
--
CREATE TABLE IF NOT EXISTS `items` (
`itemId` int(11) NOT NULL AUTO_INCREMENT,
`generalProductId` int(11) NOT NULL,
`itemPurchasedPrice` double NOT NULL,
`itemDateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`itemDescription` text,
`itemBrand` int(11) NOT NULL,
`itemBoughtFromPlace` int(11) NOT NULL,
`itemBoughtFromUser` int(11) NOT NULL,
`itemConditionNew` tinyint(1) NOT NULL DEFAULT '1',
`itemBeingSold` tinyint(1) NOT NULL DEFAULT '1',
`itemSellPrice` double DEFAULT NULL,
PRIMARY KEY (`itemId`),
KEY `generalProductId` (`generalProductId`),
KEY `itemBrand` (`itemBrand`),
KEY `itemBoughtFromPlace` (`itemBoughtFromPlace`),
KEY `itemBoughtFromUser` (`itemBoughtFromUser`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- Table structure for table `products`
--
CREATE TABLE IF NOT EXISTS `products` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`productName` varchar(200) NOT NULL,
`productTimeAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`productDescription` text,
`productBrand` int(11) NOT NULL,
`productFirstAddedFrom` int(11) NOT NULL,
`productAvatar` varchar(100) DEFAULT NULL,
PRIMARY KEY (`productId`),
KEY `productBrand` (`productBrand`,`productFirstAddedFrom`),
KEY `productFirstAddedFrom` (`productFirstAddedFrom`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `products`
--
INSERT INTO `products` (`productId`, `productName`, `productTimeAdded`, `productDescription`, `productBrand`, `productFirstAddedFrom`, `productAvatar`) VALUES
(3, 'Samsung Galaxy SII', '2013-10-26 07:46:08', 'The Samsung Galaxy S II is a .....', 1, 1, NULL),
(4, 'iPhone 5', '2013-10-26 07:46:08', 'The iPhone 5 is a ....', 1, 2, NULL);
EDIT:
the following lines in PRODUCTS table seem strange
KEY `productBrand` (`productBrand`,`productFirstAddedFrom`),
KEY `productFirstAddedFrom` (`productFirstAddedFrom`)
because in the visual layout they look this way:
Foreign keys must point at a column of an other table (which has to be the same (ex.: INT(11) - INT(11)).
When your tables are created, you can add a foreign key by using
ALTER TABLE mytable ADD FOREIGN KEY (myfkey) REFERENCES myothertable(parentkey)
Now if we apply it to your structure:
DROP TABLE items; DROP TABLE brands; DROP TABLE products;
CREATE TABLE IF NOT EXISTS `brands` (
`brandId` int(11) NOT NULL AUTO_INCREMENT,
`brandName` varchar(30) NOT NULL,
`brandLogo` varchar(100) NOT NULL,
`brandDescription` text NOT NULL,
PRIMARY KEY (`brandId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `brands` (`brandId`, `brandName`, `brandLogo`, `brandDescription`) VALUES
(1, 'Samsung', '', 'Manufacturer of ...'),
(2, 'Apple', '', 'American high-tech company ...');
CREATE TABLE IF NOT EXISTS `items` (
`itemId` int(11) NOT NULL AUTO_INCREMENT,
`generalProductId` int(11) NOT NULL,
`itemPurchasedPrice` double NOT NULL,
`itemDateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`itemDescription` text,
`itemBrand` int(11) NOT NULL,
`itemBoughtFromPlace` int(11) NOT NULL,
`itemBoughtFromUser` int(11) NOT NULL,
`itemConditionNew` tinyint(1) NOT NULL DEFAULT '1',
`itemBeingSold` tinyint(1) NOT NULL DEFAULT '1',
`itemSellPrice` double DEFAULT NULL,
PRIMARY KEY (`itemId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `products` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`productName` varchar(200) NOT NULL,
`productTimeAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`productDescription` text,
`productBrand` int(11) NOT NULL,
`productFirstAddedFrom` int(11) NOT NULL,
`productAvatar` varchar(100) DEFAULT NULL,
PRIMARY KEY (`productId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `products` (`productId`, `productName`, `productTimeAdded`, `productDescription`, `productBrand`, `productFirstAddedFrom`, `productAvatar`) VALUES
(3, 'Samsung Galaxy SII', '2013-10-26 07:46:08', 'The Samsung Galaxy S II is a .....', 1, 1, NULL),
(4, 'iPhone 5', '2013-10-26 07:46:08', 'The iPhone 5 is a ....', 1, 2, NULL);
ALTER TABLE items ADD FOREIGN KEY(generalProductId) REFERENCES products(productId) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE products ADD FOREIGN KEY(productBrand) REFERENCES brands(brandId) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE items ADD FOREIGN KEY(itemBrand) REFERENCES product(productBrand) ON DELETE CASCADE ON UPDATE CASCADE;
If you want to keep the brandID stored in the items table, the foreign key constarint has to be a composite one (and a Unique index added to products in order for that to work):
Table brands:
CREATE TABLE IF NOT EXISTS `brands` (
`brandId` int(11) NOT NULL AUTO_INCREMENT,
`brandName` varchar(30) NOT NULL,
`brandLogo` varchar(100) NOT NULL,
`brandDescription` text NOT NULL,
PRIMARY KEY (`brandId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
INSERT INTO `brands` (`brandId`, `brandName`, `brandLogo`, `brandDescription`)
VALUES
(1, 'Samsung', '', 'Manufacturer of ...'),
(2, 'Apple', '', 'American high-tech company ...');
Table products:
CREATE TABLE IF NOT EXISTS `products` (
`productId` int(11) NOT NULL AUTO_INCREMENT,
`productName` varchar(200) NOT NULL,
`productTimeAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`productDescription` text,
`productBrand` int(11) NOT NULL,
`productFirstAddedFrom` int(11) NOT NULL,
`productAvatar` varchar(100) DEFAULT NULL,
PRIMARY KEY (`productId`),
KEY `productBrand` (`productBrand`,`productFirstAddedFrom`),
KEY `productFirstAddedFrom` (`productFirstAddedFrom`),
FOREIGN KEY (productBrand) -- FK added
REFERENCES brands (brandId),
UNIQUE (productBrand, productId) -- Unique index added
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
INSERT INTO `products`
(`productId`, `productName`, `productTimeAdded`, `productDescription`,
`productBrand`, `productFirstAddedFrom`, `productAvatar`)
VALUES
(3, 'Samsung Galaxy SII', '2013-10-26 07:46:08', 'The Samsung Galaxy S II is a .....', 1, 1, NULL),
(4, 'iPhone 5', '2013-10-26 07:46:08', 'The iPhone 5 is a ....', 1, 2, NULL);
Table items:
CREATE TABLE IF NOT EXISTS `items` (
`itemId` int(11) NOT NULL AUTO_INCREMENT,
`generalProductId` int(11) NOT NULL,
`itemPurchasedPrice` double NOT NULL,
`itemDateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`itemDescription` text,
`itemBrand` int(11) NOT NULL,
`itemBoughtFromPlace` int(11) NOT NULL,
`itemBoughtFromUser` int(11) NOT NULL,
`itemConditionNew` tinyint(1) NOT NULL DEFAULT '1',
`itemBeingSold` tinyint(1) NOT NULL DEFAULT '1',
`itemSellPrice` double DEFAULT NULL,
PRIMARY KEY (`itemId`),
KEY `generalProductId` (`generalProductId`),
KEY `itemBrand` (`itemBrand`),
KEY `itemBoughtFromPlace` (`itemBoughtFromPlace`),
KEY `itemBoughtFromUser` (`itemBoughtFromUser`),
FOREIGN KEY (itemBrand, generalProductId) -- composite FK
REFERENCES products (productBrand, productId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

Insert error 1241

I have two MySQL INNODB tables 'student' and 'instructor'. They are analogous in almost every single way. I can add arbitrary records into the 'student' table, but not the 'instructor' table. Both tables have the same structure with ID as the primary key.
(ID varchar, name varchar, dept_name, varchar, field4 int)
I have tried queries such as
INSERT INTO instructor VALUES ('12345', 'name', 'computer science', 50000);
INSERT INTO instructor SET ID = 67890, name = 'Polson', dept_name = 'Bioinformatics', salary = 100 ON DUPLICATE KEY SET name = 'Polson', dept_name = 'Bioinformatics', salary = 100;
INSERT INTO instructor (ID) VALUES ('12345');
INSERT INTO instructor (ID) VALUE ('12345');
while all of these queries work with the 'student' table. What is going on?
Here are my create table statements. My user has full permissions. Is there any status query that I should use to look for issues? Thank you!
CREATE TABLE `instructor` (
`ID` varchar(5) NOT NULL DEFAULT '',
`name` varchar(20) DEFAULT NULL,
`dept_name` varchar(25) DEFAULT NULL,
`salary` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `dept_name` (`dept_name`),
CONSTRAINT `instructor_ibfk_1` FOREIGN KEY (`dept_name`) REFERENCES `department` (`dept_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `student` (
`ID` varchar(10) NOT NULL DEFAULT '',
`name` varchar(20) DEFAULT NULL,
`dept_name` varchar(25) DEFAULT NULL,
`total_credits` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `dept_name` (`dept_name`),
CONSTRAINT `student_ibfk_1` FOREIGN KEY (`dept_name`) REFERENCES `department` (`dept_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE department (
dept_name varchar(25) NOT NULL DEFAULT '',
building varchar(15) DEFAULT NULL,
budget float(12,2) DEFAULT NULL,
PRIMARY KEY (dept_name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
which contains a record Bioinformatics | DBI | 1000000.00