I am working with SQL Server 2008. I want to add duplicate keys on database. Presently it shows error while inserting duplicate key.
How can I insert duplicate keys into database?
My insertion query is
string qry = "insert into EmpMaster values('"id+"','"+code+"','"+type+"','"+fname+"')";
SqlDataReader dr1 = conn.query(qry);.
Primary key is code.
You can't insert duplicate to primary key column. Primary key is for not null and non-duplicate values to do so remove primary key column name from code. or take another column which can insert duplicate
Suggestion :- Removing primary key is not a good practice. If you want to add duplicate keys then you can use another column
Thanks.
You can't add duplicate to a primary key column.
If you want to add duplicate data in COLUMN "CODE", just normalize your database and make foreign key references .
Related
I use the MS Sync Framework to sync my SQL Server instance with a local SQL CE file to make it possible working offline with my Windows app.
I use GUIDs as keys. On my table I have a unique index on 2 columns: user_id and setting_id:
usersettings table
------------------
id PK -> I also tried it without this column. Same result
user_id FK
setting_id FK
value
Now I do the following:
I create a new record in this table in both databases - SQL Server and SQL CE with the same user_id and setting_id.
This should work and merge the data together since this can happen in real life. But I get an error when syncing saying the unique key constraint led to an error. The key pair already exists in the table.
A duplicate value cannot be inserted into a unique index. [ Table name = user_settings,Constraint name = unique_userid_settingid ]
Why can't MS sync handle that? It should not try to insert the key pair again. It should update the value if needed.
The issue is if you add the same key pair to different copies of the table, they get different IDs (GUIDs) as primary keys in this usersettings table.
As this is simply a many-to-many table between Users and Settings, there is no need to have that ID as a PK (or even a column at all).
Instead, just use a concatenated key of the two FKs e.g.,
CREATE TABLE [dbo].[usersettings](
[user_id] [UNIQUEIDENTIFIER] NOT NULL,
[setting_id] [UNIQUEIDENTIFIER] NOT NULL,
[value] [varchar](50) NOT NULL,
CONSTRAINT [PK_usersettings] PRIMARY KEY CLUSTERED ([user_id] ASC, [setting_id] ASC) );
Of course, include appropriate field settings (e.g., if you use VARCHARs to store the IDs) and relevant FKs.
As the rows inserted should now be identical on the two copies, it should merge fine.
If you must have a single column as a unique identifier for the table, you could make it meaningful e.g.,
the PK (ID) becomes a varchar (72)
it gets filled with CONCAT(user_ID, setting_id)
As the User_ID and Setting_ID are FKs, you should already have them generated so concatenating them should be easy enough.
Do you get the error during sync, then it should appear as a conflict, that you must solve in code.
https://learn.microsoft.com/en-us/previous-versions/sql/synchronization/sync-framework-2.0/bb734542(v=sql.105)
I also see this in the manual: By default, the following objects are not copied to the client database: FOREIGN KEY constraints, UNIQUE constraints, DEFAULT constraints, and the SQL Server ROWGUIDCOL property. This indicates poor support for your scenario
I suggest you remove the unique constraint from the device table.
I am working on an existing table where as I can see there are 3(!) primary keys:
I want to copy the existing rows, alter the ctid column and then copy them again to the end of the table. I try that and I am getting the error:
Cannot add an entity with a key that is already in use.
Probably because I am copying the rows and adding them with the same primary keys. How I can solve this? Is it possible to solve it without modifying the db schema (I am thinking of adding ctid as primary key also)?
Code
var testsDefault = (from i in dc.TestUnits
where i.ctid == null
select i).ToList();
List<DAL.TestUnit> TestList = new List<DAL.TestUnit>();
foreach (var test in testsDefault)
{
DAL.TestUnit newTest = new DAL.TestUnit();
newTest.TestID = test.TestID;
newTest.PatientType = test.PatientType;
newTest.Unit = test.Unit;
newTest.ctid = "105";
TestList.Add(newTest);
}
dc.TestUnits.InsertAllOnSubmit(TestList);
dc.SubmitChanges();
You need to add ctid to your composite primary key.
ALTER TABLE TestUnits
DROP CONSTRAINT PK_WhateverYourCompositeIndexNameIs
ALTER TABLE TestUnits
ADD CONSTRAINT PK_WhateverYourCompositeIndexNameIs PRIMARY KEY (TestID, PatientType, Unit, ctid)
See: How can I alter a primary key constraint using SQL syntax?
No,it is not possible what you are trying to do without modifying the db schema. Since you are using three PK and Primary key can not be duplicate as you are trying to do. The solution to your problem is make all the row's columns unique and then add the another row but make sure all tuples must have unique entries.
The another solution to your problem is already given by Rafalon
How can I change primary key with linq in c#
I try
db.SubmitChange();
an error occur.
Then I realized linq does not allow to change primary key field in my c# program
How can I fix this problem.
You can't update a primary key using LINQ. You need to delete that row from the database and add a new one with the new primary key. You can do that in the database using SQL but it should be Unique and shouldn't violate any foreign key constraint.
I am working on windows application using c# and MySQL.
I inserted new record by reading text file into MySQL database, Database having no primary key but combination of four columns we can consider as component of primary key (but actually database having no primary key).
I know how to add and update record using DataAdapter and DataSet. But I can not update a record in the database having no primary key.
Can you please guide me, how can I update a record in the database with a database having no primary key.
Thanks.
Assuming you have next table:
column1,
column2,
..
columnN,
target -- column to update
then use:
UPDATE table
SET
target = #target
WHERE
column1 = #va1, column2 = #val, -- etc specify all columns in table
If you have duplicated (absolute equal) records, you will update all of them.
For this to work with a Dataset you need to define your update command "By-Hand".
You need to set the proper DataAdapter.UpdateCommand.
In my point of view it would be a better approach to just add a primary key column to the table.
I'm trying (SQL Server Compact) to add primary key constraint on existing table that has some rows in it. While adding primary key I'm getting the error:
"A duplicate key cannot be inserted into a unique index"
I don't what this is, can anyone help me with this?
Make sure the data in the table respects the contraint you're trying to set on the table. If the column you are making primary has duplicate entries, it won't be able to work as primary key, hence the error.
You could try and find the rows with duplicate entries, with something like this:
select Id, Count(*) from myTable
having Count(*) > 1
group by Id
Try this
select id_column, count(*) from your_table group by id_column having count(*) > 1
If there are any records returned from this above query you cannot add a primary key on id_column since duplicate IDs exist.
Of course you will need to replace id_column and your_table with the appropriate names.