I have set a trigger up which I need to test. To do this I need to carry out an insert into the tblSOLine table however when I do this I am getting an error which I assume relates to the way the PK/FKs are set up. Is there any way I could do an insert into this table without messing with the table relationships?
Here is what I tried:
INSERT INTO tblSOLine (SOHeaderID, ProductID, Quantity, NetSale, VAT)
VALUES (5364, 6, 6, NULL, NULL)
Here is the error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblSOLine_tblSOHeader".
The conflict occured in database "VintageSounds", table "dbo.tblSOHeader", column 'SOHeaderID'.
The error is saying that SOHeaderID = 5364 does not exist in SOHeader. You need to be sure that this value is what you intend.
You should not be getting an error if you attempt to insert this a headerid with this number, if the foreign key does not work. However, such ids are usually IDENTITY columns, so they are automatically generated. If you don't have a typo in the number, then perhaps you are confusing the number with something else, such as the customer number.
Related
I have an INSERT statement that throws the Violation of Primary Key error but the rows is inserted into the database successfully.
Violation of PRIMARY KEY constraint 'PK_TableName_ID'. Cannot insert duplicate key in object 'dbo.TableName'. The duplicate key value is (ID).
Prior to the insert statement, I check if the ID exists in the table. If it doesn't, I proceed with the INSERT statement.
In the table, I have a DATETIME column, InsertedAt, which equals to the time when the rows was inserted.
I have no idea why the error is thrown and would love to hear other potential causes.
Thank you.
I'm having issue with an assignment I'm working on right now. The question is:
Write an INSERT statement that adds this row to the Products table:
ProductID: The next automatically generated ID
CategoryID: 4
ProductCode: dgx_640
ProductName: Yamaha DGX 640 88-Key Digital Piano
Description: Long description to come.
ListPrice: 799.99
DiscountPercent: 0
DateAdded: Today’s date/time.
Use a column list for this statement.
And the answer I came up with is:
INSERT INTO Products(CategoryID, ProductCode, ProductName, Description, ListPrice, DiscountPercent, DateAdded)
VALUES (4, 'dgx_640', 'Yamaha DGX 640 88-Key Digital Piano', 'Long description to come.', 799.99, 0, SYSDATETIME())
But when I try to execute it, an error comes up saying
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Products__Catego__3B75D760". The conflict occurred in database "MyGuitarShop", table "dbo.Categories", column 'CategoryID'.
The statement has been terminated.
Any help would be appreciated
The error is very clear - you're trying to insert a value into the CategoryID column of Products which causes a violation of the foreign key constraint to the category table.
This means: you're trying to insert a value (4) into Products.CategoryID which does not exist in the Category table. The foreign key constraint's job is to prevent exactly this case - it will not allow you to do this - for good reason.
So basically: you need to ensure that the values you're inserting into foreign key columns do exist in the referenced table (Category) before you do the INSERT.
The reference may not need to be enforced.
Make a new Database Diagram and add your two tables. Right click the connection line going between them, choose properties.
Where it says 'Enforce Foreign Key Constraint' change it to No.
This fixed the issue for me.
It is also possible that the number you're trying to insert and reference is an auto number row as a primary key in the connected table. I can only assume this can cause as issue, vs having the column be it's own editable column value.
you are trying to insert a value in the foreign key that doesn't exist in the table it reference, in you'r case it's the CategoryID.
to solve this you need to insert number 4 in the CategoryID column in the table catego first
I have one table [Software] and have 2 foreign key [Brand_ID,Category_ID]. I have bonded the 2 table [Brand and Category] with a drop down list.
When the insert statement is executing I am getting the INSERT statement conflicted with the FOREIGN KEY constraint error.
Here is the code:
Insert INTO tblSoftware(Description,Date_Of_Purchase,Price,Brand_ID,Category_ID) Values (#Description,#Date_Of_Purchase,#Price,#Brand_ID,#Category_ID)", con);
When I remove the Brand_ID and Category_ID from the sql statement, it is inserting fine.
How do i solve this?
Thanks
#nexusmusic00, make sure when you execute insert statement at that time Brand_ID and Category_ID values exists in Brand and Category table respectively.
What you are passing for #Brand_ID,#Category_ID when user did not select any thing from dropdown. I guess you are passing 0 for those two parameters that cause issue you have to set that value as NULL.
I'm trying to insert a record into a table with a Foreign Key Constraint. The constrained field is AccountID. I understand I can not insert records into the table if the value I'm inserting for AccountID does not exist in the table referenced by the constraint.
Below is an example of the insert statement I'm using:
INSERT INTO sometable (
AccountId
,TIdCode
)
VALUES (
'801143'
,2239754448
)
go
select *
from sometable
where TIdCode = 2239754448
My question is why am I getting a FK constraint error when the primary key value reference exists in the table referenced by the constraint.
I'm aware of how to disable then re-enable the constraint. However, I would like to understand why I'm getting this error.
Turns out the value was not in the table after all. I assumed the values from my production system were present in my test system. Thank you Damien_The _Unbeliever for asking a question that made me question my assumptions. Mr. Moose thank you as well for your insight.
I have a FK in my table that looks like this:
Clearly, I should be able to insert NULL into this column as well as any value that exists in the parent table.
But when I try to insert NULL, I get the following error:
The INSERT statement conflicted with the FOREIGN KEY constraint
"users_fk". The conflict occurred in database "mydatabase", table
"dbo.country", column 'country_id'.
You are absolutely right - you should be able to insert NULL into this column.
Are you 500% sure that you are? The error message says something else - it appears as if you're inserting an invalid value.
Could it be that you have
(a) another line in your script that inserts additional (invalid) values?
Or
(b) do you happen to have a trigger on this table that does something invalid?
Update: (based on your comment)
If you're not specifying the column in the INSERT statement - then maybe an invalid default value is defined on that column? Something like a default of 0 or something.
If you do have a default value - that default value will be inserted (and not NULL, as you seem to be expecting)