I need to be able to insert multiple rows in a table where one of the fields, a foreign key will be included in multiple rows.
Currently when I'm trying to do insert I'm getting this error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.dll but was not handled in user code
Additional information: Cannot insert duplicate key row in object
'dbo.userGroupMembership' with unique index 'IX_userId'. The duplicate
key value is (264673).
Query I'm using:
INSERT INTO userGroupMembership(userId, usergroupId, created, adultAdminAccessLevel)
SELECT [userId], 12, GETDATE(), 0
FROM [dbo].[userGroupMembership]
where usergroupId = #UserGroupId
UserId is the foreign key field.
Any idea if I need to do any configuration change in the table or how can I be able to insert multiple rows with same foreign key?
You have a unique index allowing one row per userID. If you truly want more than one row per userID just drop the unique index.
DROP INDEX dbo.userGroupMembership.IX_userID;
Related
I altered my postgresql database table to have a unique constraint like this:
ALTER TABLE my_table
ADD CONSTRAINT unique_values UNIQUE (value_one, value_two, value_three);
I want to not be able to post duplicate values for value_one, value_two, or value_three. So there should only ever be ONE unique value in these columns in the entire database.
But I'm still able to INSERT duplicate values in the value_one, value_two, and value_three columns.
What am I doing wrong?
Edit:
I have deleted all the duplicate data in the database, but get an error whenever I try to add a unique constraint to individual columns in the table.
Another thing, if I say:
ALTER TABLE my_table
ADD CONSTRAINT unique_values UNIQUE (value_one, value_two, value_three);
I do not get an error. But if I say:
ALTER TABLE my_table
ADD CONSTRAINT unique_values UNIQUE (value_one);
Like with just one value, I get this error:
could not create unique index "unique_values"
If you define a unique constraint over three columns, that means that you cannot insert the same three values a second time. But each of these columns can contain duplicate values. For example, these three rows would satisfy the uniqueness constraint:
(1, 2, 3)
(1, 2, 4)
(1, 5, 3)
but you would get an error if you inserted one of these triples a second time.
If you want each column to be unique, you have to define three constraints, one for each column.
I have a web application with multiple instance running all connected to a p-SQL with a table - myTable that have only two columns with one column is having unique key constraint.
This application will receive at least 15K request in a second from multiple instance and my requirement is to avoid the duplicate insertion in the table.
myTable:
CREATE TABLE myTable (
id SERIAL PRIMARY KEY,
myData VARCHAR (50) UNIQUE);
Insert statement:
INSERT INTO myTable (myData) VALUES ('data-1');
My question is if all the instances try to insert same data (assume the above insert statement from all the instances), all at a time to the table will I get only one record inserted and one instance will get inserted success message and all other instance will get a duplicate key error?
I am trying to insert a few values in an existing table, but first it makes nothing and 2nd time it shows me this error message.
My Unique KEY consists of a FK of a higher table and a counter.
If I check my tables there is no entry but it does not accept my UNIQUE Key.
Anybody with some ideas?
My test SQL Code for inserting into the view (same result with table)
INSERT INTO V_BRESZ(BRES_ID_LINKKEY, Maskenkey)
VALUES(29, 60);
For example my error message is:
violation of PRIMARY or UNIQUE KEY constraint "UK_BRESZ" on table
"BRESZ". Problematic key value is ("MASKENKEY" = ' 60',
"BRES_ID_LINKKEY" = 29).
But there is no such entry in my table / view.
Cannot insert duplicate key row in object 'dbo.OCRD' with unique index 'OCRD_ABS_ENTRY'. The duplicate key value is (1). The statement has been terminated.;
An error occurred while updating the entries. See the inner exception for details.; An error occurred while updating the entries. See the inner exception for details.
Inner Exception: Cannot insert duplicate key row in object 'dbo.OCRD' with unique index 'OCRD_ABS_ENTRY'. The duplicate key value is (1). The statement has
been terminated.
An error occurred while updating the entries. See the inner exception for details. An error occurred while updating the entries. See the inner exception for details.
It's an insert on DB
You have a column named "OCRD_ABS_ENTRY" which has an integrity
constraint (unique key or primary key).
You try to insert another entry with value "1" for OCRD_ABS_ENTRY
column, which should be unique.
Try to delete the constraint if it's not necessary, or to insert with
another value.
follow what #Florian already said.
Additional information
insert values into tables with out mentioning value to primary field
I am trying to insert a row into my database table, but I keep on getting a SQL error.
I have a table called tbl_template_log, it has 3 Forgain Keys, user_id, temp_id, savedtemp_id, at the moment I only want to Insert a row with user_id and set temp_id and savedtemp_id to 0.
Query:
INSERT INTO tbl_template_log (user_id, temp_id, savetemp_id, send_date, send_to, email_send) VALUES (user_id=77, temp_id=0, savetemp_id=0, send_date='2013-10-10', send_to='test#test.com', email_send='hello')
Error:
INSERT INTO tbl_template_log (user_id, temp_id, savetemp_id, send_date, send_to, email_send) VALUES (user_id=77, temp_id=0, savetemp_id=0, send_date='2013-10-10', send_to='test#test.com', email_send='hello')
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`admin_boltmail`.`tbl_template_log`, CONSTRAINT `tbl_template_log_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`user_id`))
From What I understand there is some sort of issue with user_id that the ID of the user has to be existing in tbl_user and ID 77 is an existing user id.....
Could some one point out a mistake I am doing. Thx
You can't add a row to this table unless the other tables specified in your foreign key constraints already have a record with the field value you're trying to insert. That's what a foreign key constraint means.
You're trying to break the rule you defined on your table. No, the database won't let you break the rule. You'll either have to add records to the foreign key tables or disable the constraints.