Insert error 1241 - mysql

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

Related

Cannot add or update a child row: a foreign key constraint fails even if I already added row on parent table

I'm trying to add a record to the child table (transaction_tb) and I used a value for the foreign key that DOES exist in the parent table's primary key. But why does it still returns me this error mentioned above?
CREATE TABLE custinfo_tb(
transactionID bigint NOT NULL,
Col_ID int(8) NOT NULL,
email varchar(50) NOT NULL,
contact int(11) NOT NULL,
address varchar(100) NOT NULL,
tdcp varchar(100) NOT NULL,
pc varchar(4) NOT NULL,
PRIMARY KEY (Col_ID,transactionID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE brokerinfo_tb
(
transactionID bigint NOT NULL,
Brokerid varchar(30) NOT NULL,
Broker_contact int(15) NOT NULL,
Account_wBroker varchar(100) NOT NULL,
PRIMARY KEY (Brokerid,transactionID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE stockinfo_tb
(
transactionID bigint NOT NULL,
Stock_Code varchar(6) NOT NULL,
Stock_Name varchar(50) NOT NULL,
Stock_Amount int(9) NOT NULL,
Avecost float NOT NULL,
Cert_amount float NOT NULL,
PRIMARY KEY (stock_code,transactionID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE transactiontype_tb
(
transactionID bigint NOT NULL,
Transfertype varchar(20) NOT NULL,
transferdesc varchar(20) NOT NULL,
transperiod varchar(10) NOT NULL,
transfee varchar(30) NOT NULL,
PRIMARY KEY (transfertype,transactionID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE transaction_tb(
transactionID bigint NOT NULL AUTO_INCREMENT,
COL_ID int NOT NULL,
sysdate DATE,
Stock_Code varchar(6) NOT NULL,
Brokerid varchar(30) NOT NULL,
Transfertype varchar(20) NOT NULL,
PRIMARY KEY (transactionID),
FOREIGN KEY (Col_ID,transactionID) REFERENCES custinfo_tb(Col_ID,transactionID) ON DELETE CASCADE ON UPDATE CASCADE ,
FOREIGN KEY (Brokerid,transactionID) REFERENCES brokerinfo_tb(Brokerid,transactionID) ON DELETE CASCADE ON UPDATE CASCADE ,
FOREIGN KEY (Stock_Code,transactionID) REFERENCES stockinfo_tb(Stock_Code,transactionID) ON DELETE CASCADE ON UPDATE CASCADE ,
FOREIGN KEY (Transfertype,transactionID) REFERENCES transactiontype_tb(Transfertype,transactionID) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
The data I entered are:
INSERT INTO brokerinfo_tb (transactionID, Brokerid)
VALUES (1,1);
INSERT INTO custinfo_tb (transactionID, Col_ID)
VALUES (1,20180111);
INSERT INTO stockinfo_tb (transactionID, Stock_Code)
VALUES (1,'AR');
INSERT INTO transactiontype_tb (transactionID, Transfertype)
VALUES (1,'TRANSFER_IN');
-- THis is where it failed
INSERT INTO transaction_tb (transactionID, Brokerid, COL_ID, Stock_Code, Transfertype)
VALUES (1,1,201808111,'AR','TRANSFER_IN');
What went wrong? I tried adding a row to my transaction_tb table for which there are already present matching row coming from the four tables ( custinfo_tb, brokerinfo_tb, stockinfo_tb and transactiontype_tb)
Your custinfo_tb.Col_ID contains 20180111.
You tried to insert transaction_tb.COL_ID value 201808111.
Compare these two values:
20180111
201808111

Only allow one unique foreign key for primary

I have 3 tables in mysql, One is a Company table, the other is a license table and the last is a joining table between both primary keys, When a person adds a company id to the license id in the joining table, it allows multiple companies to exist for one license, this cannot happen, so I need to do something that will only allow one company id for one license id
heres the tables
Table license
CREATE TABLE `License` (
`license_id` int(11) NOT NULL AUTO_INCREMENT,
`license_number` varchar(45) NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`duration` int(11) NOT NULL,
`expiry_date` date NOT NULL,
`product_id` int(11) DEFAULT NULL,
PRIMARY KEY (`license_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
;
Company Table
CREATE TABLE `Company` (
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`physical_address` varchar(255) DEFAULT NULL,
`postal_address` varchar(255) DEFAULT NULL,
`reseller_id` int(11) DEFAULT NULL,
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;
and Joining table
CREATE TABLE `CompanyLicense` (
`license_id` int(11) NOT NULL,
`company_id` int(11) NOT NULL,
PRIMARY KEY (`license_id`,`company_id`),
KEY `companlicence_company_fk_idx` (`company_id`),
CONSTRAINT `companylicense_company_fk` FOREIGN KEY (`company_id`) REFERENCES `Company` (`company_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `companylicense_license_fk` FOREIGN KEY (`license_id`) REFERENCES `License` (`license_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
So far i have this
INSERT INTO CompanyLicense (license_id, company_id) VALUES
('2','6') on duplicate key update license_id = '2';
doesnt seem to do the job
You need to make company unique in companylicense:
ALTER TABLE companylicense ADD UNIQUE KEY (company)
or better yet, make company a field in license instead of having a link table.

Adding Foreign key but getting constraint violation error

I have four tables:
1)Country:Two columns
a) country_id
b) country_name
2)State: Three columns
a) state_id
b) state_name
c) country_id(foreign key)
3)City: Three Columns
a) city_id
b) city_name
c) state_id(foreign key)
4) Doctor: 20 Columns
a) doc_id (primary key)
b) doc_name
c) email,gender age and so on
I have populated these tables with dara.
I am adding three new columns: country_id, state_id and city_id in doctor table as a foreign key.
When I insert the country_id, state_id and city_id as a foreign key in that table I got this error
1452 - Cannot add or update a child row: a foreign key constraint fails (`medical_network`.`#sql-4174_45`, CONSTRAINT `#sql-4174_45_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I know this is a basic error, but can't find the mistake.
Doctor Table:
CREATE TABLE `doctor` (
`doc_id` int(9) NOT NULL AUTO_INCREMENT,
`doc_fname` varchar(15) NOT NULL,
`doc_lname` varchar(15) NOT NULL,
`doc_province` varchar(20) NOT NULL,
`doc_city` varchar(25) NOT NULL,
`doc_gender` varchar(10) NOT NULL,
`doc_fathername` varchar(32) NOT NULL,
`doc_age` varchar(20) NOT NULL,
`doc_cnic` varchar(16) NOT NULL,
`doc_medclgname` varchar(100) NOT NULL,
`doc_specilization` varchar(100) NOT NULL,
`doc_phoneno` varchar(20) NOT NULL,
`doc_officeno` varchar(20) NOT NULL,
`doc_email` varchar(25) NOT NULL,
`doc_gradyear` int(10) NOT NULL,
`doc_licenseno` varchar(30) NOT NULL,
`doc_hosp` varchar(200) NOT NULL,
`doc_pass` varchar(35) NOT NULL,
`doc_address` varchar(100) NOT NULL,
`doc_country` varchar(100) NOT NULL,
`doc_img` varchar(200) NOT NULL,
`totime` time NOT NULL,
`fromtime` time NOT NULL,
`todate` date NOT NULL,
`fromdate` date NOT NULL,
`country_id` int(20) NOT NULL,
PRIMARY KEY (`doc_id`),
KEY `country_id` (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin
Country Table:
CREATE TABLE `country` (
`country_id` int(20) NOT NULL AUTO_INCREMENT,
`country_name` varchar(200) NOT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=252 DEFAULT CHARSET=latin1
State Table Query:
CREATE TABLE `state` (
`state_id` int(20) NOT NULL AUTO_INCREMENT,
`state_name` varchar(200) NOT NULL,
`country_id` int(20) NOT NULL,
PRIMARY KEY (`state_id`),
KEY `country_id` (`country_id`),
CONSTRAINT `state_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
City Table:
CREATE TABLE `city` (
`city_id` int(20) NOT NULL AUTO_INCREMENT,
`city_name` varchar(200) NOT NULL,
`state_id` int(20) NOT NULL,
PRIMARY KEY (`city_id`),
KEY `state_id` (`state_id`),
CONSTRAINT `city_ibfk_1` FOREIGN KEY (`state_id`) REFERENCES `state` (`state_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
First Add All the columns you want to make foreign keys in the Table.
Add some relevant data into it.
Now Alter the table and make those new added column a foreign key.
ie
ALTER TABLE TblName
AD FOREIGN KEY (P_Id)
REFERENCES ParentTable(P_Id)
In English:
You have some doctors who are in unknown countries.
In DB-speak:
One or more rows of doctor have a value for country_id that in not found in the country_id column of country.
Check that the values of country_id for new doctor rows exist in the country table.
To find them if doctor rows already exist:
select d.*
from doctor d
left join country c on c.country_id = doctor.country_id
where c.country_id is null
Change the column country_id int(20) NULL in 'doctor' table

Designing E-commerce database and mysql error

I am trying to make an e-commerce site from scratch. Currently I am trying to make the database.
These are the core tables that the database will have:
Customer: which will have email, username, password....
Customers_Session: Stores information about customer session in hash
Group: basically tells what permissions a customer will have
Category: the category type of a product
Product: information about a product such as name, and description...
Product_Price: Price info on products. This will store the different prices put for each product at various times.
Product_Variation: information about product images, and various colors or styles of a product.
Customer_Orders: What products a customer has ordered.
Customer_Reviews: Reviews made by customers on a product.
Reviewed Products: This table is created based on many to many relationship between the Product table and the Review Table.
Ordered products: This table is created based on the many to many relationship between the Product and the Orders Table.
Based on the above, I have come up with the sql code below:
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`username` varchar(30) NOT NULL,
`password` varchar(64) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(320) NOT NULL,
`zip` mediumint(5) NOT NULL,
`salt` varchar(40) NOT NULL,
`joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`group` tinyint(1) NOT NULL,
`dob` date NOT NULL,
`pic_url` varchar(225) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `groups`
--
CREATE TABLE IF NOT EXISTS `groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`permissions` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- This is for category table
--
CREATE TABLE IF NOT EXISTS `category`(
`category_id` tinyint(3) NOT NULL AUTO_INCREMENT,
`category_type` varchar(100) NOT NULL,
`category_description` varchar(160) NOT NULL,
PRIMARY KEY (`category_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
-- This is for product table
--
CREATE TABLE IF NOT EXISTS `products`(
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(100) NOT NULL UNIQUE,
`product_description` varchar(160) NOT NULL,
`quantity` int(5) NOT NULL,
`product_code` varchar(4) NOT NULL,
`keywords` varchar(70) NOT NULL,
`category_id` tinyint(3),
PRIMARY KEY (`product_id`),
INDEX (`category_id`),
CONSTRAINT FOREIGN KEY (`category_id`) REFERENCES category(`category_id`) ON DELETE SET NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
--
-- Table structure for table `bookings`
--
CREATE TABLE IF NOT EXISTS `bookings` (
`bookings_id` int(11) NOT NULL AUTO_INCREMENT,
`party_type` varchar(50) NOT NULL,
`location` varchar(100) NOT NULL,
`day` varchar(10) NOT NULL,
`time` smallint(6) NOT NULL,
`people_count` smallint(6) NOT NULL,
`booking_name` varchar(50) NOT NULL,
`booking_email` varchar(50) NOT NULL,
PRIMARY KEY (`bookings_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This is for the bookings' AUTO_INCREMENT=1 ;
--
-- Dumping data for table `groups`
--
INSERT INTO `groups` (`id`, `name`, `permissions`) VALUES
(1, 'Administrator', '{"admin":1}'),
(2, 'Users', '{"users":2}');
--
-- Table Structure for Reviews
--
CREATE TABLE IF NOT EXISTS `customer_reviews`(
`reviews_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11),
`rating` int(5) NOT NULL,
`comment` varchar(160) NOT NULL,
PRIMARY KEY(`reviews_id`),
INDEX (`user_id`),
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`) ON DELETE SET NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
--
-- Table structure for reviewed products This is a many relationshi btw reviews table and product
--
CREATE TABLE IF NOT EXISTS `reviewed_products`(
`product_id` int(11),
`reviews_id`int(11),
PRIMARY KEY(`product_id`,`reviews_id`),
CONSTRAINT FOREIGN KEY(`product_id`) REFERENCES products(`product_id`) ON DELETE SET NULL,
CONSTRAINT FOREIGN KEY(`reviews_id`) REFERENCES customer_reviews(`reviews_id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This is for many to many cardinality btw reviews and products' AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `orders`
--
CREATE TABLE IF NOT EXISTS `customer_orders` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`order_time` int(11) NOT NULL,
`amount` int(5),
`confirmation_number` int(,
`user_id` int(11),
`product_id` int(11),
PRIMARY KEY (`order_id`),
INDEX (`user_id`),
CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES users(`user_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
/* We need a new table since the customer_order and product is in a many to many relationship */
--
--
-- Table structure for table `curtomer_order_product`
--
CREATE TABLE IF NOT EXISTS `ordered_product`(
`product_id` int(11),
`order_id` int(11),
`quantity` smallint(5),
PRIMARY KEY(`product_id`,`order_id`),
INDEX (`product_id`,`order_id`),
CONSTRAINT FOREIGN KEY (`order_id`) REFERENCES customer_orders(`order_id`) ON DELETE RESTRICT,
CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- This is for price table
--
CREATE TABLE IF NOT EXISTS `product_price`(
`price_id` int(11) NOT NULL AUTO_INCREMENT,
`price` decimal(6,2) NOT NULL,
`product_id` int(11) NOT NULL,
PRIMARY KEY (`price_id`),
INDEX (`product_id`),
CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `product_variations`
--
CREATE TABLE IF NOT EXISTS `product_variations`(
`variations_id` int(11) NOT NULL AUTO_INCREMENT,
`color_name` varchar(10) NOT NULL,
`color_value` char(6) NOT NULL,
`product_id` int(11),
`picture1` varchar(100) NOT NULL,
`picture2` varchar(100) NOT NULL,
`picture3` varchar(100) NOT NULL,
PRIMARY KEY (`variations_id`),
INDEX (`product_id`),
CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `users_session`
--
CREATE TABLE IF NOT EXISTS `users_session` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`hash` varchar(64) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`user_id`),
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The problem is that when I try to run the above code in PHPmyadmin, I get an error stating "Cannot add foreign key constraint". This starts to happen when creating the customer_Reviews table and any other subsequent tables that require foreign keys.
My questions are:
1. Would you recommend designing the database this way.
2. Why am I getting the "Cannot add foreign key constraint" error?
Thanks.
For a full quality review of your database design, you might try the Code Review sister site. At a glance, I don't see any glaring issues with your database design or your SQL.
The foreign key constraint is failing because you have misnamed the column in question.
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`)
should be:
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`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.