Domanda

I have a table in SQL Server called Test_Table with two columns: ID and Name

The table looks something like this:

ID NAME
--------   
1  John
2  Jane

Now I have a stored procedure which inserts records into this.

INSERT INTO Test_Table
VALUES (@Id,@Name) 

And I'm passing this values from my c# code. Now I want to modify this so that the table does not have duplicates. Where should I check this, In the code or the DB? I'm very weak in DB side stuff. So how can I handle duplicates before inserting values in my table

È stato utile?

Soluzione

The "right" way to do that is in DB because:

  • Don't need to read all DB
  • Need to pass all data to C# which increase the IO
  • Concurrency - if you have more than 1 C# application you will need to sync them vs in DB it would be simpler
  • You can define the column as unique or key, which will prevent duplicate values ,DB will take care of it

If you use MSSQL use UNIQUE Constraints

Read this good answer about avoid duplicates

Altri suggerimenti

You should do this check in the database. Always, if you want it to be true of the data.

I'm not sure what you consider a duplicate. Normally, an id column would be an identity column that is automatically incremented for each value. This would prevent duplicates. You would define it as:

create table test_table (
    id int not null identity(1, 1),
    . . . 

Then, you would insert into it using:

insert into test_table(name)
    values (@Name);

The id would be assigned automatically.

If you want no duplicates just for name, then create a unique index or unique constraint (really the same thing). You can do this in the table definition just by adding unique to the column:

create table test_table (
    id int not null identity(1, 1),
    name varchar(255) unique
    . . .

Or by creating a unique index after you have created the table:

create index test_table_name on test_table(name)

(Or by explicitly creating a constraint, which is another method.)

In either case ,you will have to access to database to check wheteher values exist already.

IF NOT EXISTS (SELECT * FROM Test_Table WHERE ID= @ID AND Name=@Name)
    BEGIN
        INSERT INTO Test_Table
        VALUES (@Id,@Name) 
    END

If it is possible to make ID column as unique you can avoid checking as insertion would.t be allowed for repeating ID values , in that case you will have to handle error.

See this thread how to handle violation of Unique key constraint.

If you don't want repeating IDs you'll have to set the ID as the Primary Key, which is pretty much obligatory.

If you don't want the Name to repeat, you could populate a list with the Names the table contains, and then you would only insert whatever name is not in that List.

Here is an example, instead of using a list I used a dictionary:

Dictionary<int, string> Names = new Dictionary<int, string> ();
using (SqlCommand command = new SqlCommand ("SELECT * FROM TestTable", con))
using (SqlDataReader reader = command.ExecuteReader ()) {
   while (reader.Read ()) {
      Names.Add (reader["ID"], reader["NAME"]);
   }
}

if (!Names.ContainsValue ("ValueYouWantToInsert")) {
   //do stuff
}

You should check it in DB, Also you can make ID as Primary Key

Which is mostly used, because people can have duplicate name.

You can modify your Id with the Unique key constraint or you can also make it Primary key.

Try like this:

alter table Test_Table add primary key (ID)

and

alter table Test_Table add unique key (Name)
IF NOT EXISTS (SELECT * FROM Test_Table WHERE ID= @ID AND Name=@Name)
    BEGIN
        INSERT INTO Test_Table
        VALUES (@Id,@Name) 
    END
    ELSE
    BEGIN
        UPDATE Test_Table
        SET ID= @ID,NAME = @Name
        WHERE ID= @ID AND Name=@Name
    END
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top