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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Violation of PRIMARY KEY constraint????

Violation of PRIMARY KEY constraint????

Scheduled Pinned Locked Moved C#
helpdatabasequestionsql-serversysadmin
13 Posts 4 Posters 14 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.
  • T Offline
    T Offline
    Twyce
    wrote on last edited by
    #1

    I am using bulkcopy to import data into SQL server database.The data i'm trying to import contains student numbers and the modules that they are registerd for.A student can be registered for many modules(which means one student number can appear many times on the table).Problem is when i try to import my spradsheet my application throws a "Violation of PRIMARY KEY constraint" error. How do i fix this

    P W M 3 Replies Last reply
    0
    • T Twyce

      I am using bulkcopy to import data into SQL server database.The data i'm trying to import contains student numbers and the modules that they are registerd for.A student can be registered for many modules(which means one student number can appear many times on the table).Problem is when i try to import my spradsheet my application throws a "Violation of PRIMARY KEY constraint" error. How do i fix this

      P Offline
      P Offline
      perle1
      wrote on last edited by
      #2

      Hi Twyce, you have a duplicate value on the primary key. :confused: On which field / fields you have set the primary key ?? Greetings Ralph

      T 1 Reply Last reply
      0
      • T Twyce

        I am using bulkcopy to import data into SQL server database.The data i'm trying to import contains student numbers and the modules that they are registerd for.A student can be registered for many modules(which means one student number can appear many times on the table).Problem is when i try to import my spradsheet my application throws a "Violation of PRIMARY KEY constraint" error. How do i fix this

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        What is the primary key for registrations table? If it is only student then you'll have a problem since student can have only one module. If the table definition is correct, but you have duplicate data (for example because of denormalization), you can disable the primary key, load the data, clean the data and enable the primary key. Hope this helps, Mika

        T 1 Reply Last reply
        0
        • P perle1

          Hi Twyce, you have a duplicate value on the primary key. :confused: On which field / fields you have set the primary key ?? Greetings Ralph

          T Offline
          T Offline
          Twyce
          wrote on last edited by
          #4

          the StudentNumber is my primary key

          P 1 Reply Last reply
          0
          • W Wendelius

            What is the primary key for registrations table? If it is only student then you'll have a problem since student can have only one module. If the table definition is correct, but you have duplicate data (for example because of denormalization), you can disable the primary key, load the data, clean the data and enable the primary key. Hope this helps, Mika

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

            Ok here is what i am doing.The tble is an asociative entity between table student and table module.so it gets its data from both tables.what i did was jst make the StudentID(from table student) my primary key.A student can be registerd for more than 1 module .i want to have duplicate student numbers but with different module IDs. do you get me?

            W 1 Reply Last reply
            0
            • T Twyce

              Ok here is what i am doing.The tble is an asociative entity between table student and table module.so it gets its data from both tables.what i did was jst make the StudentID(from table student) my primary key.A student can be registerd for more than 1 module .i want to have duplicate student numbers but with different module IDs. do you get me?

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              Yep, define both StudentID and ModuleID for the same primary key (multicolumned key)

              T 1 Reply Last reply
              0
              • T Twyce

                the StudentNumber is my primary key

                P Offline
                P Offline
                perle1
                wrote on last edited by
                #7

                If you have more then one tables, please give us an illustration. Do you try to insert "Students" twice ?

                T 1 Reply Last reply
                0
                • W Wendelius

                  Yep, define both StudentID and ModuleID for the same primary key (multicolumned key)

                  T Offline
                  T Offline
                  Twyce
                  wrote on last edited by
                  #8

                  thnx ey i'll try that and get back to you

                  1 Reply Last reply
                  0
                  • P perle1

                    If you have more then one tables, please give us an illustration. Do you try to insert "Students" twice ?

                    T Offline
                    T Offline
                    Twyce
                    wrote on last edited by
                    #9

                    yes you can say that coz i insert a student number more than once but with a different module code example StudentID ModuleCode s20600752 WIC33222 s20502106 MKI4563 s20600752 LKH7566 s20600752 POU35568 s20502106 KLK67887

                    P 1 Reply Last reply
                    0
                    • T Twyce

                      yes you can say that coz i insert a student number more than once but with a different module code example StudentID ModuleCode s20600752 WIC33222 s20502106 MKI4563 s20600752 LKH7566 s20600752 POU35568 s20502106 KLK67887

                      P Offline
                      P Offline
                      perle1
                      wrote on last edited by
                      #10

                      :) it will crash on Student s20600752, Line 3 !! this key is already there. set your primary key on both fields "StudentID" and "ModuleCode" and make sure you don't insert a student with the "ModuleCode" twice.

                      T 1 Reply Last reply
                      0
                      • P perle1

                        :) it will crash on Student s20600752, Line 3 !! this key is already there. set your primary key on both fields "StudentID" and "ModuleCode" and make sure you don't insert a student with the "ModuleCode" twice.

                        T Offline
                        T Offline
                        Twyce
                        wrote on last edited by
                        #11

                        Thanx for replying so quick. i have jst set my primary key on both fields an ran it and it still gives me the same error.

                        P 1 Reply Last reply
                        0
                        • T Twyce

                          Thanx for replying so quick. i have jst set my primary key on both fields an ran it and it still gives me the same error.

                          P Offline
                          P Offline
                          perle1
                          wrote on last edited by
                          #12

                          That make no sense. :doh: You really sure you don't insert different module codes for the same student? Try your routine with fewer data. I hope that will help you to find the problem.

                          1 Reply Last reply
                          0
                          • T Twyce

                            I am using bulkcopy to import data into SQL server database.The data i'm trying to import contains student numbers and the modules that they are registerd for.A student can be registered for many modules(which means one student number can appear many times on the table).Problem is when i try to import my spradsheet my application throws a "Violation of PRIMARY KEY constraint" error. How do i fix this

                            M Offline
                            M Offline
                            Mycroft Holmes
                            wrote on last edited by
                            #13

                            Having used BCP and bulkcopy to move massive amounts of data I can recommend the following. Move your data into a staging table. Manage your transformations by stored procedure AFTER the bulk loading of the data is completed. You have a structural issue, a relational database is NOT a spreadsheet. This data should obviously be goung into 3 or more tables. Student, Course and a manytomany link table. Load the data and then insert/update the student table, update/insert the course table, update/insert the linking table (this has studentid and courseid and data pertaining to this link, scores etc) Twyce, this is database design 101, I recommend you do some study on the proper design of data structures.

                            Never underestimate the power of human stupidity RAH

                            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