I Create two sample table and create both tables inserted procedure in MySql.I listed the both procedure and table script below. Now i try to add the procedures using DataAdapter. First Procedure add successfully.But the second procedure return the error.Cannot add or update a child row : a foreign key constraint fails.
CREATE TABLE `hd_test` (
`FLD_ID` int(11) NOT NULL AUTO_INCREMENT,
`FLD_NAME` varchar(20) DEFAULT NULL,
UNIQUE KEY `FLD_ID` (`FLD_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `hd_test1` (
`FLD_ID` int(11) NOT NULL DEFAULT '0',
`FLD_NAME` varchar(20) DEFAULT NULL,
`FLD_PARENT` int(11) DEFAULT NULL,
KEY `FLD_PARENT` (`FLD_PARENT`),
CONSTRAINT `hd_test1_ibfk_1` FOREIGN KEY (`FLD_PARENT`) REFERENCES `hd_test` (`FLD_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP PROCEDURE IF EXISTS db_helpdesk.SP_HD_TEST;
CREATE PROCEDURE db_helpdesk.`SP_HD_TEST`(
IN NAME VARCHAR(20))
BEGIN
INSERT INTO HD_TEST(FLD_NAME)VALUES(NAME);
END;
DROP PROCEDURE IF EXISTS db_helpdesk.SP_HD_TEST1;
CREATE PROCEDURE db_helpdesk.`SP_HD_TEST1`(
IN NAME VARCHAR(20),
IN PARENT INTEGER)
BEGIN
INSERT INTO HD_TEST1(FLD_NAME,FLD_PARENT)VALUES(NAME,PARENT);
END;
Error
Cannot add or update a child row : a foreign key constraint fails..
Please help me to fix this error.
Related
In ASP.NET MVC 5 project, the database and identity tables are created by code first, and i created other tables by SQL (not by code first) in this database, and i want to join User table with some table by user Id.
Say database called Qwerty and the identity tables are:
dbo.Users
dbo.Roles
dbo.UserClaims
... ect
I want to create tables by SQL like this:
Create Table Topic.Topic
(
TopicID int Primary key identity(1,1) not null,
TopicAddress nvarchar(255) not null
)
Create Table dbo.Bookmark
(
BookmarkID int Primary key identity(1,1) not null,
BookmarkDate datetime default getdate() not null,
UserID int constraint FK_Favorites_Users_UserID foreign key (UserID) references Users(UserID) not null
)
Topic table is created successfully, but when i run SQL code for Bookmark table, it give me error and mark Users (table name) word with red line
By default the primary key of Users is nvarchar, so, your foreign key should be defined with that type.
Create Table dbo.Bookmark
(
BookmarkID int Primary key identity(1,1) not null,
BookmarkDate datetime default getdate() not null,
UserID [nvarchar](128) constraint FK_Favorites_Users_UserID foreign key (UserID) references Users(UserID) not null
)
I have two tables , one to one relationship i makes DetailsID of projectDetails FK in ID of projects table:
projects:
ID, //has FK With DetailsID in Details table & auto identity (1,1)
ProjectName,
Areas,
PaymentSystem,
ReceivedDate,
PropertyClassification,
ProjectImage
ProjectDetails:
DetailsID ,auto identity ( 1,1)
ProjectDetailName,
ProjectDetailImage
I am trying to insert new record in projects table , gives me this error at this line of code :
con.Open();
comm.ExecuteNonQuery(); // when execute
System.Data.SqlClient.SqlException: 'The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Projects_ProjectDetails". The conflict occurred in database "AlamaarRealEstate", table "dbo.ProjectDetails", column 'DetailsID'.
and this is my stored to insert :
ALTER proc [Insert_Project]
#Projectname NVARCHAR(MAX) ,
#areas NVARCHAR(MAX) ,
#Paymentsystem NVARCHAR(MAX) ,
#Receiveddate date ,
#Classification NVARCHAR(MAX) ,
#Projectimage Nvarchar(MAX)
as
INSERT INTO dbo.Projects
(
ProjectName,
Areas,
PaymentSystem,
ReceivedDate,
PropertyClassification,
ProjectImage
)
VALUES
(
#Projectname ,
#areas,
#Paymentsystem ,
#Receiveddate ,
#Classification,
#Projectimage
)
The question explains the answer. Referential Integrity is not maintained properly, you are trying to insert into a child table for which the master value does not exist. Please insert values to Project_details first. This will resolve your issue. If you did not what this to throw an error, just check the existence of the DetailID in Projects table before inserting.
Without more detail your question is hard to answer. For instance in your procedure you do explicit column naming for inserts and LEAVE OUT the column you are having a problem with. So if the proc was wrong it would be bombing with your example foreign key constraint error. But you are not even listing the 'ID' field to insert into with the procedure. So this is common if you are using an 'Identity' field to self seed, but you are claiming it is a foreign key. So like others have commented, without more code to show the exact way your tables are made it's hard to guess. Here is a self extracting example you could run that shows if a column is nullable and I had a key constraint it would work. Without the exact code of the tables as well as the proc it is hard to tell. What you gave is pseudo code.
USE Tester --just a test database I have, you can use whatever database you want
GO
IF OBJECT_ID('Projects') IS NOT NULL
DROP TABLE Projects
IF OBJECT_ID('ProjectDetails') IS NOT NULL
DROP TABLE ProjectDetails
create TABLE ProjectDetails
(
DetailsID INT CONSTRAINT PK_DetailsId PRIMARY KEY,
ProjectDetailName VARCHAR(32)
)
CREATE TABLE Projects
(
Id INT CONSTRAINT FK_Projects_ProjectDetails FOREIGN KEY (Id) REFERENCES ProjectDetails(DetailsId),
ProjectName varchar(32)
)
GO
IF OBJECT_ID('Insert_Project') IS NOT NULL
DROP PROC Insert_Project
GO
Create proc Insert_Project
#Projectname NVARCHAR(MAX)
as
INSERT INTO dbo.Projects ( ProjectName )
VALUES ( #Projectname )
GO
Select *
From dbo.Projects
EXEC dbo.Insert_Project #Projectname = N'Test' -- nvarchar(max)
Select *
From dbo.Projects
I found in stackoverflow a link who explains this question. I wrote a procedure who does that but the retreived data is not as it's expected by mysql
Here is data that I receive using procedure (this gives an error when I use it for creating a table in this form, the error is syntax error near id int(11))
CREATE TABLE IF NOT EXISTS `atelier`(
`id` int(11) NO PRI auto_increment
`nom` varchar(30) NO UNI
`at_date` date NO
`pax` int(11) NO
`prix` double NO
`vente` double YES
`note` varchar(200) YES
`cr_user` varchar(15) NO
`cr_date` datetime NO
`up_user` varchar(15) YES
`up_date` datetime YES
And here is the data given by phpmyadmin
CREATE TABLE `Atelier` (
`id` int(11) NOT NULL auto_increment,
`nom` varchar(30) NOT NULL,
`date` date NOT NULL,
`pax` int(11) NOT NULL,
`prix` double NOT NULL,
`vente` double default NULL,
`note` varchar(200) default NULL,
`cr_user` varchar(15) NOT NULL,
`cr_date` datetime NOT NULL,
`up_user` varchar(15) default NULL,
`up_date` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=Innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
What I want to know should I do translation of YES AND NO within my program or there is a way to obtain it with a query, and with wich query I can obtain info of two last lines given by phpmyadmin.(I think they are usefull for the choose of ENGINE and define the unique and primary keys)
PS:Using c#, and names of the tables I get from another procedure that exist in my program. I don't get it with DESCRIBE. What I receive is between id and up_date.
Here is the link of my procedures MY PROCEDURES
You didn't show the procedure generating this output, but if you replace DESCRIBE in there with SHOW CREATE TABLE I suppose it will work.
I need to create a table in MySQL that will have indexes for each row , and when I insert records they will be inserted at the tail of the table.
And I will be able to make queries by row numbers.
Can you give me an example how to do so ?
Thanks for help.
This is how you create a table:
CREATE TABLE `symbols` (
`id` int(11) NOT NULL auto_increment,
`country` varchar(255) NOT NULL default '',
`animal` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
Read more on mysql here and here
I am using SQL Server 2008 Enterprise. I have tried that if I set SQL Server transaction log to ever increase for the related database (with no backup settings), then a single delete statement of this stored procedure will cause deadlock if executed by multiple threads at the same time? Any ideas why?
For the delete statement, Param1 is a column of table FooTable, Param1 is a foreign key of another table (refers to another primary key clustered index column of the other table). There is no index on Param1 itself for table FooTable. FooTable has another column which is used as clustered primary key, but not Param1 column.
create PROCEDURE [dbo].[FooProc]
(
#Param1 int
,#Param2 int
,#Param3 int
)
AS
DELETE FooTable WHERE Param1 = #Param1
INSERT INTO FooTable
(
Param1
,Param2
,Param3
)
VALUES
(
#Param1
,#Param2
,#Param3
)
DECLARE #ID bigint
SET #ID = ISNULL(##Identity,-1)
IF #ID > 0
BEGIN
SELECT IdentityStr FROM FooTable WHERE ID = #ID
END
As a rule of thumb, you should always create an index on the column that has FOREIGN KEY constraint. Otherwise, deadlock is very likely to happen (because the server has to lock the whole dependent table to ensure that constraint)