Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to prevent adding a duplicate value into a table

How to prevent adding a duplicate value into a table

Scheduled Pinned Locked Moved Database
databasevisual-studiohelptutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    makumazan84
    wrote on last edited by
    #1

    OK, here is the deal. I've got a table with 2 columns: "ID"(int), "Name"(string). "ID" - is a unique key field used in relations with other tables, but the values of the "Name" should be unique as well. I need to add more data into this table, but not to create some duplicate information. I know about INSERT DISTINCT, but I am not using an SQL command, instead I use a VS generated TableAdapter (in my case it is named "companiesTableAdapter"). Here is a small code snippet: this.companiesTableAdapter.Insert("XYZ Company"); this code does insert the string into a table, but it doesn't care if "XYZ Company" is already there. (the DB itself is created in Access2003, if this may be helpful) If someone has done something similar, I would really appreciate their help.

    B T 2 Replies Last reply
    0
    • M makumazan84

      OK, here is the deal. I've got a table with 2 columns: "ID"(int), "Name"(string). "ID" - is a unique key field used in relations with other tables, but the values of the "Name" should be unique as well. I need to add more data into this table, but not to create some duplicate information. I know about INSERT DISTINCT, but I am not using an SQL command, instead I use a VS generated TableAdapter (in my case it is named "companiesTableAdapter"). Here is a small code snippet: this.companiesTableAdapter.Insert("XYZ Company"); this code does insert the string into a table, but it doesn't care if "XYZ Company" is already there. (the DB itself is created in Access2003, if this may be helpful) If someone has done something similar, I would really appreciate their help.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      First check if that name exists, in case if exists then don't insert otherwise do insert. example: if(select count(name) from tablename where name='value')=0 begin insert into tablename values ('idvalue','namevalue') end


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you then save my time by voting my post.

      M 1 Reply Last reply
      0
      • B Blue_Boy

        First check if that name exists, in case if exists then don't insert otherwise do insert. example: if(select count(name) from tablename where name='value')=0 begin insert into tablename values ('idvalue','namevalue') end


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you then save my time by voting my post.

        M Offline
        M Offline
        makumazan84
        wrote on last edited by
        #3

        Worked great! Thanks a lot

        B 1 Reply Last reply
        0
        • M makumazan84

          Worked great! Thanks a lot

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          No problem.


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          1 Reply Last reply
          0
          • M makumazan84

            OK, here is the deal. I've got a table with 2 columns: "ID"(int), "Name"(string). "ID" - is a unique key field used in relations with other tables, but the values of the "Name" should be unique as well. I need to add more data into this table, but not to create some duplicate information. I know about INSERT DISTINCT, but I am not using an SQL command, instead I use a VS generated TableAdapter (in my case it is named "companiesTableAdapter"). Here is a small code snippet: this.companiesTableAdapter.Insert("XYZ Company"); this code does insert the string into a table, but it doesn't care if "XYZ Company" is already there. (the DB itself is created in Access2003, if this may be helpful) If someone has done something similar, I would really appreciate their help.

            T Offline
            T Offline
            TheFM234
            wrote on last edited by
            #5

            You can add an unique index on the name column.

            M 1 Reply Last reply
            0
            • T TheFM234

              You can add an unique index on the name column.

              M Offline
              M Offline
              makumazan84
              wrote on last edited by
              #6

              How can I do that? (Add a unique numerical index to a column)

              A 1 Reply Last reply
              0
              • M makumazan84

                How can I do that? (Add a unique numerical index to a column)

                A Offline
                A Offline
                Ashfield
                wrote on last edited by
                #7

                Simply really

                Create unique index fred on myTable(myColumn)

                Bob Ashfield Consultants Ltd

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups