Dim datetimepicker1 As String = Format(System.DateTime.Now, "yyyy-MM-dd HH:mm:ss")
Try
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = "INSERT INTO tbl_product (`prod_name`,`prod_desc`, `cat_id`, `uom_id`,`uom_num`, `dept_id`, `brand_id`, `size_id`, `type_id`, `remarks`, `date`) values (#prod_name,#prod_desc,#cat_id,#uom_id,#uom_num,#dept_id,#brand_id,#size_id,#type_id,#remarks,#date)"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("#prod_name", TextBox1.Text)
.Parameters.AddWithValue("#prod_desc", TextBox2.Text)
.Parameters.AddWithValue("#cat_id", ComboBox1.Text)
.Parameters.AddWithValue("#uom_id", ComboBox2.Text)
.Parameters.AddWithValue("#uom_num", TextBox3.Text)
.Parameters.AddWithValue("#dept_id", ComboBox3.Text)
.Parameters.AddWithValue("#brand_id", ComboBox4.Text)
.Parameters.AddWithValue("#size_id", ComboBox5.Text)
.Parameters.AddWithValue("#type_id", ComboBox6.Text)
.Parameters.AddWithValue("#remarks", RichTextBox1.Text)
.Parameters.AddWithValue("#date", datetimepicker1)
.ExecuteNonQuery()
End With
MsgBox(" SIze Successfully added")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
cannot add or update child row foreign key constraint fails this is my error
this is my table structure*************8
--
-- Table structure for table tbl_brand
CREATE TABLE IF NOT EXISTS tbl_brand (
brand_id int(11) NOT NULL AUTO_INCREMENT,
brand_name varchar(200) NOT NULL,
brand_desc varchar(200) DEFAULT NULL,
PRIMARY KEY (brand_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table tbl_brand
INSERT INTO tbl_brand (brand_id, brand_name, brand_desc) VALUES
(1, 'Nike ', 'Nike Air '),
(2, 'Crocs ', 'Class A '),
(3, 'SafeGuard ', 'SafeGuard ');
--
-- Table structure for table tbl_category
CREATE TABLE IF NOT EXISTS tbl_category (
cat_id int(11) NOT NULL AUTO_INCREMENT,
cat_name varchar(200) NOT NULL,
cat_desc varchar(200) NOT NULL,
PRIMARY KEY (cat_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table tbl_category
INSERT INTO tbl_category (cat_id, cat_name, cat_desc) VALUES
(1, 'Bath Soap ', 'Bath Soap '),
(2, 'Detergent ', 'Detergent ');
--
-- Table structure for table tbl_dept
CREATE TABLE IF NOT EXISTS tbl_dept (
dept_id int(11) NOT NULL AUTO_INCREMENT,
dept_name varchar(200) NOT NULL,
dept_desc varchar(200) NOT NULL,
PRIMARY KEY (dept_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table tbl_dept
INSERT INTO tbl_dept (dept_id, dept_name, dept_desc) VALUES
(1, 'Shoes ', 'Shoes '),
(2, 'Soap ', 'Soap ');
--
-- Table structure for table tbl_product
CREATE TABLE IF NOT EXISTS tbl_product (
prod_id int(11) NOT NULL AUTO_INCREMENT,
prod_name varchar(200) NOT NULL,
prod_desc varchar(200) DEFAULT NULL,
cat_id int(11) NOT NULL,
dept_id int(11) NOT NULL,
brand_id int(11) NOT NULL,
type_id int(11) NOT NULL,
uom_id int(11) NOT NULL,
size_id int(11) NOT NULL,
date datetime NOT NULL,
remarks varchar(200) DEFAULT NULL,
uom_num int(60) DEFAULT NULL,
PRIMARY KEY (prod_id),
KEY tbl_product_ibfk_9 (type_id),
KEY tbl_product_ibfk_10 (uom_id),
KEY tbl_product_ibfk_11 (size_id),
KEY tbl_product_ibfk_12 (dept_id),
KEY tbl_product_ibfk_13 (cat_id),
KEY tbl_product_ibfk_14 (brand_id)
); ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Table structure for table tbl_size
CREATE TABLE IF NOT EXISTS tbl_size (
size_id int(11) NOT NULL AUTO_INCREMENT,
size_name varchar(100) NOT NULL,
PRIMARY KEY (size_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table tbl_size
INSERT INTO tbl_size (size_id, size_name) VALUES
(1, 'Small(S) '),
(2, 'Medium(M) '),
(3, 'Large(L) ');
--
-- Table structure for table tbl_type
CREATE TABLE IF NOT EXISTS tbl_type (
type_id int(11) NOT NULL AUTO_INCREMENT,
type_name varchar(200) NOT NULL,
PRIMARY KEY (type_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
--
-- Dumping data for table tbl_type
INSERT INTO tbl_type (type_id, type_name) VALUES
(1, 'BaskertBall Shoes '),
(2, 'Jersey Shorts '),
(3, 'running shoes '),
(8, 'Bath Soap ');
--
-- Table structure for table tbl_uom
CREATE TABLE IF NOT EXISTS tbl_uom (
uom_id int(11) NOT NULL AUTO_INCREMENT,
uom_name varchar(200) NOT NULL,
PRIMARY KEY (uom_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
--
-- Dumping data for table tbl_uom
INSERT INTO tbl_uom (uom_id, uom_name) VALUES
(1, 'kilogram(kg) '),
(2, 'Gram(g) '),
(3, 'Milligram(Mg) '),
(4, 'Liter(L) '),
(5, 'Milliliters(ml) '),
(6, 'Pieces(pcs) '),
(7, 'foot(ft) ');
--
-- Table structure for table tbl_user
CREATE TABLE IF NOT EXISTS tbl_user (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_code varchar(200) DEFAULT NULL,
user_password varchar(200) DEFAULT NULL,
user_name varchar(200) DEFAULT NULL,
user_level int(1) DEFAULT NULL,
datetime datetime NOT NULL,
com_code varchar(11) NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY user_code (user_code)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dumping data for table tbl_user
INSERT INTO tbl_user (user_id, user_code, user_password, user_name, user_level, datetime, com_code) VALUES
(1, '1024', '1024', 'Vincent Dematera', 3, '2012-01-10 18:18:33', '001'),
(7, '14 ', '14', 'Megan Bueno', 1, '2012-10-30 21:56:14', '002'),
(8, '13', '13', 'Anonymous', 1, '2012-10-20 21:51:00', '002'),
(9, '9', '9', 'boom', 1, '0000-00-00 00:00:00', '003');
--
-- Constraints for dumped tables
--
-- Constraints for table tbl_product
ALTER TABLE tbl_product
ADD CONSTRAINT tbl_product_ibfk_9 FOREIGN KEY (type_id) REFERENCES tbl_type (type_id),
ADD CONSTRAINT tbl_product_ibfk_10 FOREIGN KEY (uom_id) REFERENCES tbl_uom (uom_id),
ADD CONSTRAINT tbl_product_ibfk_11 FOREIGN KEY (size_id) REFERENCES tbl_size (size_id),
ADD CONSTRAINT tbl_product_ibfk_12 FOREIGN KEY (dept_id) REFERENCES tbl_dept (dept_id),
ADD CONSTRAINT tbl_product_ibfk_13 FOREIGN KEY (cat_id) REFERENCES tbl_category (cat_id),
ADD CONSTRAINT tbl_product_ibfk_14 FOREIGN KEY (brand_id) REFERENCES tbl_brand (brand_id),
ADD CONSTRAINT tbl_product_ibfk_2 FOREIGN KEY (type_id) REFERENCES tbl_type (type_id),
ADD CONSTRAINT tbl_product_ibfk_3 FOREIGN KEY (uom_id) REFERENCES tbl_uom (uom_id),
ADD CONSTRAINT tbl_product_ibfk_4 FOREIGN KEY (size_id) REFERENCES tbl_size (size_id),
ADD CONSTRAINT tbl_product_ibfk_5 FOREIGN KEY (dept_id) REFERENCES tbl_dept (dept_id),
ADD CONSTRAINT tbl_product_ibfk_6 FOREIGN KEY (cat_id) REFERENCES tbl_category (cat_id),
ADD CONSTRAINT tbl_product_ibfk_7 FOREIGN KEY (brand_id) REFERENCES tbl_brand (brand_id);
SET FOREIGN_KEY_CHECKS=1;
You have a significant number of foreign key constraints for your table product. For example, any "size_id" you enter into table products HAS to correspond to a "size_id" in table size.
What your vb.net program is telling you is that one of the id values you are entering into table product does not exist in its corresponding table. My best guess would be that one of your id values (ie. size_id, uom_id, etc) is empty, blank, or zero.
You have a number of options - you can:
turn foreign key off for the table if you wish with SET foreign_key_checks = 0;
you can remove the foreign keys altogether
you can fix your code to ensure you are adhering to the foreign keys before you even attempt to insert a value
Related
I tried Inner join, however i m not able to figure out what is wrong i m doing.
How can I query data from all the 6 tables in the blue box.
The studentprofile table is what is the link between the other 5 tables in the blue region.
I ran this query and this showed me all the data.
select * from users, roles_assigned,studentprofile,schoolwithusers;
Problem I am facing is that I need only filted data for user who are students and school name and student profile
Please Help.
here is my sql code.
--
-- Database: `onlinemarksheets`
--
-- --------------------------------------------------------
--
-- Table structure for table `exams`
--
CREATE TABLE `exams` (
`id` int(11) NOT NULL,
`examtye_id` int(11) DEFAULT NULL,
`schooluser_id` int(11) DEFAULT NULL,
`duration_to` date DEFAULT NULL,
`duration_from` date DEFAULT NULL,
`year` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `examtype`
--
CREATE TABLE `examtype` (
`id` int(11) NOT NULL,
`type` enum('Mid-Term','Half-yearly','Yearly') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `examtype`
--
INSERT INTO `examtype` (`id`, `type`) VALUES
(1, 'Mid-Term'),
(2, 'Half-yearly'),
(3, 'Yearly');
-- --------------------------------------------------------
--
-- Table structure for table `marks`
--
CREATE TABLE `marks` (
`id` int(11) NOT NULL,
`exam_id` int(11) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`marks_obtained` int(11) DEFAULT NULL,
`marks_total` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `roles`
--
CREATE TABLE `roles` (
`id` int(11) NOT NULL,
`type` enum('Admin','Teacher','Student') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `roles`
--
INSERT INTO `roles` (`id`, `type`) VALUES
(1, 'Admin'),
(2, 'Teacher'),
(3, 'Student');
-- --------------------------------------------------------
--
-- Table structure for table `roles_assigned`
--
CREATE TABLE `roles_assigned` (
`id` int(11) NOT NULL,
`role_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `roles_assigned`
--
INSERT INTO `roles_assigned` (`id`, `role_id`, `user_id`) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 3, 4),
(5, 3, 5),
(6, 3, 6),
(7, 1, 1),
(8, 2, 2),
(9, 3, 3),
(10, 3, 4),
(11, 3, 5),
(12, 3, 6);
-- --------------------------------------------------------
--
-- Table structure for table `schools`
--
CREATE TABLE `schools` (
`id` int(11) NOT NULL,
`schoolname` varchar(45) DEFAULT NULL,
`school_email` varchar(45) DEFAULT NULL,
`school_phone` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schools`
--
INSERT INTO `schools` (`id`, `schoolname`, `school_email`, `school_phone`) VALUES
(1, 'D.A.V Public school', 'info#davschool.com', '789456123'),
(2, 'saraswati Public school', 'info#saraswatischool.com', '9998887774'),
(3, 'S.G.R.R Public School', 'Info#sgrr.com', '54245645125'),
(4, 'Sun Valley Public', 'info#sunvalley.com', '23423423424'),
(5, 'Marshal Public school', 'info#marshalschool.com', '23482728347');
-- --------------------------------------------------------
--
-- Table structure for table `schoolwithusers`
--
CREATE TABLE `schoolwithusers` (
`id` int(11) NOT NULL,
`school_id` int(11) DEFAULT NULL,
`studentprofile_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schoolwithusers`
--
INSERT INTO `schoolwithusers` (`id`, `school_id`, `studentprofile_id`) VALUES
(1, 1, 3),
(2, 3, 4);
-- --------------------------------------------------------
--
-- Table structure for table `studentprofile`
--
CREATE TABLE `studentprofile` (
`id` int(11) NOT NULL,
`user_id_fk` int(11) DEFAULT NULL,
`rollno` int(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`attendence` int(11) DEFAULT NULL,
`class` int(11) DEFAULT NULL,
`section` enum('A','B') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `studentprofile`
--
INSERT INTO `studentprofile` (`id`, `user_id_fk`, `rollno`, `dob`, `attendence`, `class`, `section`) VALUES
(1, 3, 22, '2022-01-21', 60, 1, 'A'),
(2, 4, 21, '2012-01-04', 45, 1, 'A'),
(3, 4, 1, '2012-01-04', 100, 3, 'B'),
(4, 5, 30, '2007-04-26', 45, 3, 'B'),
(5, 6, 2, '2022-01-19', 50, 6, 'B');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`fname` varchar(45) DEFAULT NULL,
`lname` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `fname`, `lname`, `email`, `password`) VALUES
(1, 'Shashank', 'Naithani', 'shashank8036#gmail.com', 'lol123'),
(2, 'Kuldeep', 'Negi', 'negikuldeep#gmail.com', 'lop123'),
(3, 'Arpit', 'Thakut', 'Aptha#gmail.com', 'arp123'),
(4, 'Ankit', 'Barthwal', 'ankitbarth#gmail.com', 'ankit123'),
(5, 'Mukesh', 'Thakur', 'sasdb#gmail.com', 'sha123'),
(6, 'Arjun', 'Negi', 'sasdb#gmail.com', 'sha123');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `exams`
--
ALTER TABLE `exams`
ADD PRIMARY KEY (`id`),
ADD KEY `examtye_id` (`examtye_id`) USING BTREE,
ADD KEY `schooluser_id` (`schooluser_id`);
--
-- Indexes for table `examtype`
--
ALTER TABLE `examtype`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `marks`
--
ALTER TABLE `marks`
ADD PRIMARY KEY (`id`),
ADD KEY `exam_id` (`exam_id`);
--
-- Indexes for table `roles`
--
ALTER TABLE `roles`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
ADD PRIMARY KEY (`id`),
ADD KEY `role_id` (`role_id`) USING BTREE,
ADD KEY `user_id` (`user_id`) USING BTREE;
--
-- Indexes for table `schools`
--
ALTER TABLE `schools`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
ADD PRIMARY KEY (`id`),
ADD KEY `school_id` (`school_id`) USING BTREE,
ADD KEY `studentprofile_id` (`studentprofile_id`);
--
-- Indexes for table `studentprofile`
--
ALTER TABLE `studentprofile`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id_idx` (`user_id_fk`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `exams`
--
ALTER TABLE `exams`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `examtype`
--
ALTER TABLE `examtype`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `marks`
--
ALTER TABLE `marks`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `roles`
--
ALTER TABLE `roles`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
--
-- AUTO_INCREMENT for table `schools`
--
ALTER TABLE `schools`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT for table `studentprofile`
--
ALTER TABLE `studentprofile`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `exams`
--
ALTER TABLE `exams`
ADD CONSTRAINT `examtye_id` FOREIGN KEY (`examtye_id`) REFERENCES `examtype` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `schooluser_id` FOREIGN KEY (`schooluser_id`) REFERENCES `schoolwithusers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `marks`
--
ALTER TABLE `marks`
ADD CONSTRAINT `exam_id` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
ADD CONSTRAINT `role_id` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
ADD CONSTRAINT `school_id` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `studentprofile_id` FOREIGN KEY (`studentprofile_id`) REFERENCES `studentprofile` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `studentprofile`
--
ALTER TABLE `studentprofile`
ADD CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id_fk`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
You have to join each table on the primary key/foreign key. Some of the sample data is not quite correct because you have duplicate combinations of student_id and roles_id values in the roles_assigned table. Be sure to list the column names you need instead of *.
select *
from users
INNER JOIN roles_assigned ON users.id = roles_assigned.user_id
INNER JOIN roles ON roles.id = roles_assigned.role_id
INNER JOIN studentprofile on studentprofile.id = users.id
INNER JOIN schoolwithusers on schoolwithusers.studentprofile_id =
studentprofile.id
INNER JOIN schools ON schools.id = schoolwithusers.school_id
WHERE roles.type = 'Student';
This question already has an answer here:
Multiple Column Foreign Key: Set single column to Null "ON DELETE" instead of all
(1 answer)
Closed 2 years ago.
Please refer to this SQLFiddle
CREATE TABLE `parent` (
`id` varchar(64) NOT NULL,
`master_id` varchar(64) NOT NULL,
`c1_id` varchar(64) DEFAULT NULL,
`c2_id` varchar(64) DEFAULT NULL,
PRIMARY KEY (`master_id`, `id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `child_1` (
`id` varchar(64) NOT NULL,
`master_id` varchar(64) NOT NULL,
PRIMARY KEY (`master_id`, `id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `child_2` (
`id` varchar(64) NOT NULL,
`master_id` varchar(64) NOT NULL,
PRIMARY KEY (`master_id`, `id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO child_1 (`id`, `master_id`)
VALUES
(1, 'm1');
INSERT INTO child_2 (`id`, `master_id`)
VALUES
(2, 'm2'),
(3, 'm1');
INSERT INTO parent (`id`, `master_id`, `c1_id`, `c2_id`)
VALUES
(4, 'm1', null, null),
(5, 'm1', 1, null),
(6, 'm2', null, 2);
I have multiple tables that have composite Primary Key.
All PK are PRIMARY KEY (master_id, id)
And now I try to add Foreign Keys to these tables.
ALTER TABLE `parent`
ADD CONSTRAINT parent_fk_1
FOREIGN KEY (`master_id`, `c1_id`)
REFERENCES child_1 (`master_id`, `id`)
ON UPDATE CASCADE
ON DELETE SET NULL;
But it throws error Cannot add foreign key constraint.
Here parent.master_id always must stay NOT NULL while c1_id and c2_id can be set to NULL.
Is it possible to achieve this kind of Foreign Key setup?
I found that there is MATCH SIMPLE option that allows composite key to be partial NULL, but how to apply it for ON DELETE SET NULL?
Both referring Column are NOT NULL, so you can't use ON DELETE SET NULL, this violates the NOT NULL
ALTER TABLE `parent`
ADD CONSTRAINT parent_fk_1
FOREIGN KEY (`master_id`, `c1_id`)
REFERENCES child_1 (`master_id`, `id`)
ON UPDATE CASCADE;
I have the following table in MySQL version 5.5.24
CREATE TABLE IF NOT EXISTS `bookings` (
`booking_id` int(11) NOT NULL ,
`class_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;
INSERT INTO `bookings` (`booking_id`, `class_id`, `user_id`) VALUES
(19, 3, 5),
(21, 6, 5);
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL,
`username` varchar(20) NOT NULL,
`firstname` varchar(20) NOT NULL,
`lastname` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
INSERT INTO `users` (`user_id`, `username`, `firstname`, `lastname`, `email`, `password`) VALUES
(4, 'another', 'Anne', 'Other', 'another#gmail.com', '1234'),
(5, 'rbirney', 'Rosanne', 'Birney', 'rosanne.birney#gmail.com', '1111');
ALTER TABLE `bookings`
ADD PRIMARY KEY (`booking_id`), ADD KEY `class_id` (`class_id`,`user_id`), ADD KEY `user_id` (`user_id`);
ALTER TABLE `classes`
ADD PRIMARY KEY (`class_id`);
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
ALTER TABLE `bookings`
MODIFY `booking_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=22;
ALTER TABLE `classes`
MODIFY `class_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11;
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;
ALTER TABLE `bookings`
ADD CONSTRAINT `fkBookingClass` FOREIGN KEY (`class_id`) REFERENCES `classes` (`class_id`),
ADD CONSTRAINT `fkBookingUser` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`);
I am getting the following message
#1062 - Duplicate entry '19' for key 'PRIMARY'
anyone have any ideas?
In SQL it is not allowed to use the same value for a primary key twice. (value = 19)
Primary keys are always unique.
My code SQL is :
enter code here
CREATE TABLE IF NOT EXISTS `participer` (
`statut_Invitation` tinyint(1) NOT NULL,
`NumVersion` int(11) NOT NULL,
`InviteCode` varchar(255) NOT NULL,
`IdQuest` int(11) NOT NULL,
PRIMARY KEY (`NumVersion`,`InviteCode`,`IdQuest`),
KEY `FK_Participer_NumVersion` (`NumVersion`),
KEY `FK_Participer_IdQuest` (`IdQuest`),
KEY `FK_Participer_InviteCode` (`InviteCode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `participer` (`statut_Invitation`, `NumVersion`, `InviteCode`, `IdQuest`) VALUES
(0, 2, '2548', 1),
(0, 2, '8742', 1);
CREATE TABLE IF NOT EXISTS `questionnaire` (
`IdQuest` int(11) NOT NULL AUTO_INCREMENT,
`Nom` varchar(256) DEFAULT NULL,
`DateCreation` date DEFAULT NULL
PRIMARY KEY (`IdQuest`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `questionnaire` (`IdQuest`, `Nom`, `DateCreation`, `ID`) VALUES
(1, 'Parions Sport', '2015-03-23'),
(2, 'GPS', '2015-03-23');
CREATE TABLE IF NOT EXISTS `utilisateurs` (
`InviteCode` varchar(255) NOT NULL,
`Email` varchar(25) NOT NULL,
`DateNaissance` date DEFAULT NULL,
`Ville` varchar(25) DEFAULT NULL,
PRIMARY KEY (`InviteCode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `utilisateurs` (`InviteCode`, `Email`, `DateNaissance`, `Ville`) VALUES
('1235314', 'bocchi#gmail.com', NULL, NULL),
('2548', 'bocchiTEST#gmail.com', NULL, NULL),
('337752493652424404824466004444846460026', 'dylan#gmail.com', NULL, NULL),
('53131172170670664482420846024208824402', 'dylan2#gmail.com', NULL, NULL),
('5446544', 'lool', NULL, NULL),
('8742', 'max#gmail.com', NULL, NULL);
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `versionquestionnaire` (
`NumVersion` int(11) NOT NULL,
`DateExpiration` date DEFAULT NULL,
`SommeNote` float DEFAULT NULL,
`NbReponses` int(11) DEFAULT NULL,
`IdQuest` int(11) NOT NULL,
PRIMARY KEY (`NumVersion`,`IdQuest`),
KEY `FK_VersionQuestionnaire_IdQuest` (`IdQuest`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `versionquestionnaire` (`NumVersion`, `DateExpiration`, `SommeNote`, `NbReponses`, `IdQuest`) VALUES
(1, '2015-03-26', 120, 2, 1),
(1, '2015-03-30', 100, 1, 2),
(2, '2015-03-26', 210, 3, 1),
(2, '2015-03-30', 300, 4, 2);
ALTER TABLE `participer`
ADD CONSTRAINT `FK_Participer_IdQuest` FOREIGN KEY (`IdQuest`) REFERENCES `versionquestionnaire` (`IdQuest`),
ADD CONSTRAINT `FK_Participer_InviteCode` FOREIGN KEY (`InviteCode`) REFERENCES `utilisateurs` (`InviteCode`),
ADD CONSTRAINT `FK_Participer_NumVersion` FOREIGN KEY (`NumVersion`) REFERENCES `versionquestionnaire` (`NumVersion`);
ALTER TABLE `versionquestionnaire`
ADD CONSTRAINT `FK_VersionQuestionnaire_IdQuest` FOREIGN KEY (`IdQuest`) REFERENCES `questionnaire` (`IdQuest`);
When i want to delete the version 1 of the questionnaire 1 :
DELETE FROM `versionquestionnaire` WHERE `versionquestionnaire`.`NumVersion` = 1 AND `versionquestionnaire`.`IdQuest` = 1;
I have a error :
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`testbug`.`participer`, CONSTRAINT `FK_Participer_IdQuest` FOREIGN KEY (`IdQuest`) REFERENCES `versionquestionnaire` (`IdQuest`)) ;
I don't understand because in the table participer, there isn't the couple with NumVersion = 1 and IdQuest = 1, there are 2 couples with NumVersion = 1 and IdQuest = 1. So my request may be run.
In short, you can't delete records from versionquestionnaire because some records in participer table reference records in participer.
Your request clearly violates IdQuest constraint: you're trying to delete a record with IdQuest = 1, while BOTH participants that still reference that ID.
i have the following database relations:
user 1:n survey 1:n statement 1:n user_response n:1 user
i need to query all statements which don't have a relation to a user_response or where the field user_response.closed = 1 of a given user-, and a given statement-id.
I don't really get it...
EDIT
Here's an export of my database. For example i have the survey-id 1 and user-id 1. I need all statements of the survey with the id 1, which don't have a response of the user with the id 1, or where the response of the user is not closed.
So my result should be 'Statement 1' (because user_response.closed = 0) and 'Statement 2' (because the user with id 1 didn't give a response).
CREATE TABLE IF NOT EXISTS `statement` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(64) NOT NULL,
`survey_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `2` (`survey_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `statement` (`id`, `title`, `survey_id`) VALUES
(1, 'Statement 1', 1),
(2, 'Statement 2', 1),
(3, 'Statement 1', 2),
(4, 'Statement 1', 3);
CREATE TABLE IF NOT EXISTS `survey` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(64) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `1` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `survey` (`id`, `title`, `user_id`) VALUES
(1, 'Survey 1', 1),
(2, 'Survey 2', 1),
(3, 'Survey 3', 2);
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `user` (`id`, `name`) VALUES
(1, 'Max'),
(2, 'Peter');
CREATE TABLE IF NOT EXISTS `user_response` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value` int(3) NOT NULL,
`closed` tinyint(1) NOT NULL,
`statement_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `3` (`statement_id`),
KEY `4` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `user_response` (`id`, `value`, `closed`, `statement_id`, `user_id`) VALUES
(1, 50, 0, 1, 1),
(2, 20, 0, 4, 2);
ALTER TABLE `statement`
ADD CONSTRAINT `2` FOREIGN KEY (`survey_id`) REFERENCES `survey` (`id`),
ADD CONSTRAINT `FK_BBA66ED9A94F7BC` FOREIGN KEY (`survey_id`) REFERENCES `survey` (`id`);
ALTER TABLE `survey`
ADD CONSTRAINT `1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
ADD CONSTRAINT `FK_AD5F9BFC9395C3F3` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);
ALTER TABLE `user_response`
ADD CONSTRAINT `3` FOREIGN KEY (`statement_id`) REFERENCES `statement` (`id`),
ADD CONSTRAINT `4` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);