I created a table like this:
CREATE TABLE `group` (
`g_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`owner_id` int(10) unsigned NOT NULL,
`g_name` varchar(45) NOT NULL,
`refer_code` varchar(45) GENERATED ALWAYS AS (concat(`g_name`)) VIRTUAL,
`created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`row_hash` varchar(256) NOT NULL,
PRIMARY KEY (`g_id`),
UNIQUE KEY `g_hash_UNIQUE` (`row_hash`),
UNIQUE KEY `refer_UNIQUE` (`refer_code`),
KEY `owner_idx` (`owner_id`),
CONSTRAINT `owner_id` FOREIGN KEY (`owner_id`) REFERENCES `user` (`u_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The update statement gives me error:
INSERT INTO `server`.`group` (`owner_id`, `g_name`, `refer_code`, `created_on`, `row_hash`)
VALUES ('4', 'cool', null, '2018-02-13 10:34:11', '452345324')
Error Code: 3105. The value specified for generated column 'refer_code' in table 'group' is not allowed.
How to specify the refer_code while inserting?
Values of the generated column are computed from the expression in the column definition of your CREATE TABLE, so don't include it in INSERT or UPDATE statements:
INSERT INTO `server`.`group` (`owner_id`, `g_name`, `created_on`, `row_hash`)
VALUES ('4', 'cool', '2018-02-13 10:34:11', '452345324')
Related
This is my sql file:
DROP DATABASE IF EXISTS `dbstudents`;
CREATE DATABASE IF NOT EXISTS `dbstudents`;
USE `dbstudents`;
DROP TABLE IF EXISTS `student_detail`;
CREATE TABLE `student_detail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fav_programming_language` varchar(128) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`student_detail_id` int(11) DEFAULT NULL,
`password` varchar(50) NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_DETAIL_idx` (`student_detail_id`),
CONSTRAINT `FK_DETAIL` FOREIGN KEY (`student_detail_id`) REFERENCES `student_detail` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `authorities`;
CREATE TABLE `authorities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`authority` varchar(50) NOT NULL,
UNIQUE KEY `authorities_idx_1` (`id`,`authority`),
CONSTRAINT `authorities_ibfk_1` FOREIGN KEY (`id`) REFERENCES `student` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This is insert statement:
INSERT INTO `student`
VALUES
(1, 'Stefan', 'Stefanovic', 'stefan#gmail.com', 1, 'stefan123', 1),
(2, 'Marko', 'Markovic', 'marko#gmail.com', 1, 'marko123', 1),
(3, 'Jovan', 'Jovanovic', 'jovan#gmail.com', 1, 'jovan123', 1);
This is error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`dbstudents`.`student`, CONSTRAINT `FK_DETAIL` FOREIGN KEY (`student_detail_id`) REFERENCES `student_detail` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I found some solutions but all of them are on some way different then mine. How to solve this? An error occurs when I try to fill in every table except the autorities
As you haven't any student_details already in the system use NULL instead a number and add the number later
INSERT INTO `student`
VALUES
(1, 'Stefan', 'Stefanovic', 'stefan#gmail.com', NULL, 'stefan123', 1),
(2, 'Marko', 'Markovic', 'marko#gmail.com', NULL, 'marko123', 1),
(3, 'Jovan', 'Jovanovic', 'jovan#gmail.com', NULL, 'jovan123', 1);
see example https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c1bbdd8ee9b9a9af244da774cdb41175
I have two tables, EVENTS and EVENT_ATTENDEES.
I'd like to be able to attempt a row insert on event_attendees and if the call_sign and event_id already exist in the table, it should update the response for that row instead of inserting a new one.
Events Table
CREATE TABLE `events` (
`event_id` int(11) NOT NULL,
`created_by` varchar(1000) NOT NULL,
`event_type` varchar(1000) NOT NULL,
`event_name` varchar(1000) NOT NULL,
`event_start` datetime NOT NULL,
`event_end` datetime NOT NULL,
`max_attendees` varchar(10) NOT NULL,
`open_registration` varchar(10) NOT NULL
)
ALTER TABLE `events`
ADD PRIMARY KEY (`event_id`);
ALTER TABLE `events`
MODIFY `event_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11
Event Attendees
CREATE TABLE `event_attendees` (
`id` int(11) NOT NULL,
`event_id` int(11) NOT NULL,
`call_sign` varchar(10) NOT NULL,
`response` varchar(10) NOT NULL
)
ALTER TABLE `event_attendees`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `event_id_2` (`event_id`,`call_sign`),
ADD KEY `event_id` (`event_id`) USING BTREE;
ALTER TABLE `event_attendees`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;
ALTER TABLE `event_attendees`
ADD CONSTRAINT `event_id_fk` FOREIGN KEY (`event_id`) REFERENCES `events` (`event_id`) ON DELETE CASCADE ON UPDATE NO ACTION;
I have tried the below query, but constantly get an error (I have created an event that has event_id = 10):
INSERT INTO event_attendees
(event_id, call_sign, response)
VALUES
('10', '007', 'Declined')
ON DUPLICATE KEY UPDATE
response = VALUES('Declined')
" #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test')' at line 6"
There is no need of VALUES here. Modify your query as:
INSERT INTO event_attendees
(event_id, call_sign, response)
VALUES
('10', '007', 'Declined')
ON DUPLICATE KEY UPDATE
response = 'Declined'
From doc:
you can use the VALUES(col_name) function to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement
Here you can find more about VALUES function.
I am trying to add to my database table, but seem to get an error. I get a 1452 Error. Here is what I think is the problem: I think my parent_fk is referring to an id what does not yet exist. At least that is what I understand of the following error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (assetgallery.ag_asset, CONSTRAINT fk_ag_asset_2 FOREIGN KEY (parent_fk) REFERENCES ag_asset (id) ON DELETE CASCADE ON UPDATE CASCADE)
Say I have a tuple in my database with the id 2. The first tuple:
'2', '8', NULL, NULL, '2', '2015-09-24 09:42:31', 'sabernLogo_0.png', NULL, NULL, NULL, '180', '80', NULL, '1', NULL, NULL, '3', '1'
I am trying to add another with an INSERT statement. I am Inserting without an id, so it looks like this :
Insert into ag_asset
(album_fk, parent_fk, head_fk, status_fk, modified, filename, title, `desc`, `text`, width, height, owner_fk, locked, remarks, group_fk, user_fk, type_fk)
VALUES(6, 0, null, 1, '2015-10-15 15:47:13.0', 'index.png', null, null, null, 215, 234, null, null, null, null, 3, 1);
From what I understand because my id is Autoincrement'ed, not filling this in would result in a freshly made id, being 2.
Here is the create statement
CREATE TABLE `ag_asset` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`album_fk` int(10) DEFAULT NULL,
`parent_fk` int(10) DEFAULT NULL,
`head_fk` int(10) DEFAULT NULL,
`status_fk` int(10) DEFAULT '1',
`modified` datetime NOT NULL,
`filename` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`desc` text,
`text` longtext,
`width` int(10) DEFAULT NULL,
`height` int(10) DEFAULT NULL,
`owner_fk` int(10) DEFAULT NULL,
`locked` int(10) DEFAULT NULL,
`remarks` text,
`group_fk` int(10) DEFAULT NULL,
`user_fk` int(10) DEFAULT NULL,
`type_fk` int(10) DEFAULT '1',
PRIMARY KEY (`id`),
KEY `in_filename` (`filename`(12)),
KEY `in_title` (`title`(12)),
KEY `fk_ag_asset_1` (`album_fk`),
KEY `fk_ag_asset_2` (`parent_fk`),
KEY `fk_ag_asset_3` (`head_fk`),
KEY `fk_ag_asset_4` (`status_fk`),
KEY `fk_ag_asset_5` (`owner_fk`),
KEY `fk_ag_asset_6` (`type_fk`),
CONSTRAINT `fk_ag_asset_1` FOREIGN KEY (`album_fk`) REFERENCES `ag_album` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ag_asset_2` FOREIGN KEY (`parent_fk`) REFERENCES `ag_asset` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ag_asset_3` FOREIGN KEY (`head_fk`) REFERENCES `ag_asset` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ag_asset_4` FOREIGN KEY (`status_fk`) REFERENCES `ag_status` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_ag_asset_5` FOREIGN KEY (`owner_fk`) REFERENCES `ag_owner` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_ag_asset_6` FOREIGN KEY (`type_fk`) REFERENCES `ag_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;
Do I understand the error correctly or am I even looking in the wrong direction?
In foreign key concept whatever value you are trying to insert/update in child table, must exist in its parent/referenced table.
Here you are trying to insert values like 0, NULL etc in columns for those you have created referenced and your master table does not keep these values.
Solution1:
Either first add these values in master table then you can insert in child table.
Solution2:
If you want to add such data in child table forcefully as per your requirement which is not suggested then you can do as-
set foreign_key_checks=0;
Insert statement here...
set foreign_key_checks=1;
I am getting a 1452 error from MySQL. Here is the SQL I used to INSERT
INSERT INTO `Quote` (`QTE_id`, `USR_id`, `QTE_total`, `QTE_desc`, `QTE_dateCreated`, `QTE_dateModified`,`QTE_name`, `QTE_status`)
VALUES
(39, 2, NULL, 'desc', '2013-11-19 00:00:00', '2013-11-19 11:22:49', 'test', 'Active');
Cannot add or update a child row: a foreign key constraint fails (`dbNAME`.`Quote`, CONSTRAINT `USR_id1` FOREIGN KEY (`USR_id`) REFERENCES `users` (`USR_id`))
I am positive that the USR_id I am trying to put into the Quote table exists. Any ideas? A lot of the other questions on Stack Overflow did not answer my question.
Here is the Create syntax for the following tables I am trying to insert and relate (generated from Workbench):
CREATE TABLE `Quote` (
`QTE_id` int(11) NOT NULL AUTO_INCREMENT,
`USR_id` int(11) DEFAULT NULL,
`QTE_total` decimal(7,2) DEFAULT NULL,
`QTE_desc` text,
`QTE_dateCreated` timestamp NULL DEFAULT NULL,
`QTE_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`QTE_name` varchar(255) DEFAULT NULL,
`QTE_status` varchar(30) DEFAULT NULL,
PRIMARY KEY (`QTE_id`),
KEY `USR_id1` (`USR_id`),
CONSTRAINT `USR_id1` FOREIGN KEY (`USR_id`) REFERENCES `users` (`USR_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
And the User Table:
CREATE TABLE `Users` (
`USR_id` int(11) NOT NULL AUTO_INCREMENT,
`MGR_id` int(11) NOT NULL DEFAULT '0'
`REP_id` int(11) NOT NULL,
`USR_name` varchar(255) NOT NULL,
`USR_login` varchar(255) NOT NULL,
`USR_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`USR_dateCreated` datetime NOT NULL,
`USR_role` enum('Salesperson','Manager') DEFAULT NULL,
PRIMARY KEY (`USR_id`,`MGR_id`,`REP_id`),
KEY `MGR_id_idx` (`MGR_id`),
KEY `REP_id_idx` (`REP_id`),
KEY `USR_login` (`USR_login`),
CONSTRAINT `MGR_id` FOREIGN KEY (`MGR_id`) REFERENCES `users` (`USR_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `REP_id` FOREIGN KEY (`REP_id`) REFERENCES `representative` (`REP_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
I had to do a few small changes (change the users to Users, add a ',' at the end of line that defines MGR_id, remove the constraint for REP_id) to make the two CREATEs to work.
Here are the exact CREATEs:
CREATE TABLE `Users` (
`USR_id` int(11) NOT NULL AUTO_INCREMENT,
`MGR_id` int(11) NOT NULL DEFAULT '0',
`REP_id` int(11) NOT NULL,
`USR_name` varchar(255) NOT NULL,
`USR_login` varchar(255) NOT NULL,
`USR_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`USR_dateCreated` datetime NOT NULL,
`USR_role` enum('Salesperson','Manager') DEFAULT NULL,
PRIMARY KEY (`USR_id`,`MGR_id`,`REP_id`),
KEY `MGR_id_idx` (`MGR_id`),
KEY `REP_id_idx` (`REP_id`),
KEY `USR_login` (`USR_login`),
CONSTRAINT `MGR_id` FOREIGN KEY (`MGR_id`) REFERENCES `Users` (`USR_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `Quote` (
`QTE_id` int(11) NOT NULL AUTO_INCREMENT,
`USR_id` int(11) DEFAULT NULL,
`QTE_total` decimal(7,2) DEFAULT NULL,
`QTE_desc` text,
`QTE_dateCreated` timestamp NULL DEFAULT NULL,
`QTE_dateModified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`QTE_name` varchar(255) DEFAULT NULL,
`QTE_status` varchar(30) DEFAULT NULL,
PRIMARY KEY (`QTE_id`),
KEY `USR_id1` (`USR_id`),
CONSTRAINT `USR_id1` FOREIGN KEY (`USR_id`) REFERENCES `Users` (`USR_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
With that in place the following two INSERTs worked:
mysql> INSERT INTO `Users` (`USR_id`, `MGR_id`) VALUES(2, 2);
Query OK, 1 row affected, 4 warnings (0.22 sec)
mysql> INSERT INTO `Quote` (`QTE_id`, `USR_id`, `QTE_total`, `QTE_desc`, `QTE_dateCreated`, `QTE_dateModified`,`QTE_name`, `QTE_status`) VALUES (39, 2, NULL, 'desc', '2013-11-19 00:00:00', '2013-11-19 11:22:49', 'test', 'Active');
Query OK, 1 row affected (0.22 sec)
I hope this helps.
I can't seem to figure out why my foreign key constraints aren't working. Here's my table schema:
CREATE TABLE `templates_textboxes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`template_id` int(10) unsigned DEFAULT NULL,
`textbox_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `templates_textboxes_to_templates_idx` (`template_id`),
KEY `templates_textboxes_to_textboxes_idx` (`textbox_id`),
CONSTRAINT `templates_textboxes_to_templates` FOREIGN KEY (`template_id`) REFERENCES `templates` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1$$
CREATE TABLE `textboxes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
`columns` tinyint(1) unsigned NOT NULL DEFAULT '1',
`width` int(5) unsigned NOT NULL DEFAULT '0',
`height` int(5) unsigned NOT NULL DEFAULT '0',
`x` int(5) unsigned NOT NULL DEFAULT '0',
`y` int(5) unsigned NOT NULL DEFAULT '0',
`z` int(5) unsigned NOT NULL DEFAULT '500',
PRIMARY KEY (`id`),
CONSTRAINT `textboxes_to_templates_textboxes` FOREIGN KEY (`id`) REFERENCES `templates_textboxes` (`textbox_id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8$$
When I create a textbox in PHP, first I insert into textboxes to get the ID then insert a row in the match table templates_textboxes with the template id and new textbox id. I've read every post I can find on here about foreign keys allowing nulls and all it says to do is to set the foreign key column (templates_textboxes.textbox_id) to allow nulls. I tried that and when I try to insert into textboxes I get the following error:
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`pitchperfect`.`textboxes`, CONSTRAINT `textboxes_to_templates_textboxes` FOREIGN KEY (`id`) REFERENCES `templates_textboxes` (`textbox_id`) ON DELETE CASCADE ON UPDATE NO ACTION)INSERT INTO `textboxes` (`content`, `columns`, `width`, `height`, `x`, `y`, `z`) VALUES ('', '1', '400', '300', '133', '93', '500')
What I'm trying to accomplish is have cascade deletes chain from themes, to templates and their assets (templates_textboxes -> textboxes).
Thanks!
Since textboxes.id is not yet defined and is AUTO_INCREMENT, that value must first exist in templates_textboxes.textbox_id. This is why the constraint is failing.