You must Sign In to post a response.
  • Category: .NET

    Violation of Unique key constrant, cannot insert duplicate key,

    I got the above exception while inserting a record in table , I know this throws because of repeating same values again nd again. But in my case the column has no primary key , my table structure is as follows,

    patient_id , int (primary-key)
    patient_name varchar(50),
    address varchar (50),
    city varchar (20),
    cart_no varchar (20),
    Male_Female varchar(20) .

    Only patient_id has primary key and when new record inserted into table patient_id is
    auto-generate but i want to insert new record of patient (example : patient_name- sachin) but this name is already have record in table then it gives violation exception but patient_name column does not have any key constraints so why this happens?

    Give me a way, thanks!
  • #761898
    Hi
    priynaka

    Working in your query. Check your table design and check in your relation ship designs . Can you Share your snapshots in your table design from sql

    try this Query


    Create table onee
    (
    patient_id int primary key identity(1,1),
    patient_name varchar(50),
    address varchar (50),
    city varchar (20),
    cart_no varchar (20),
    Male_Female varchar(20)
    )


    Insert into onee values('sachin','123address','Chennai','123','Male')
    Select * from onee



    I have attached Image given below.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #761899
    This is ready made database of medical i just have to work on this , but why this happend i cant get it.
    I just attached snapshot of design of table look at it.

    image.docx

    Delete Attachment

  • #761901
    Hi

    In your documents says nothing primary key in the patient_name so you did not meet this issue because your field not set.

    ok do one thing send your table script for this table i will check and let me know.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761905
    Add on column with identity value. It will handle you problem
    By Nathan
    Direction is important than speed

  • #761906
    Hi
    Priyanka

    Check your table for this field set identity for your table.

    patient_id int primary key identity(1,1),

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761912
    Sorry , patient_id is foreign key here but patient_name doesnt have any key

  • #761914
    Hi,

    It should work 100%, If the patient_name is not in relation ship. As per your thread Patient_Id only PK, that to AutoGenerated number, my suggestion is remove total data in that table and insert one dummy record and see the Patient_Id and again insert same data and see the difference between 1st record and 2nd record.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #761915
    Sorry , patient_id is foreign key here but patient_name doesnt have any key

  • #761916
    Hi
    Priyanka

    First check your table design and Question before then post
    for any mistake in your side then after post your question. if you easily fixed in ur side.

    Can you refer this snapshot also

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #761966
    Hi Priyanka.

    If you use identity you can handle the issue when coming to patient_id.

    You said patient_id is foreign key, that means patient_id is primary key of your second table. So you might have got primary key violation.

    Use primary key for patient_id with identity in first table.

    According to me you should not use primary key constraint for name column.

    Regards

    Sridhar Thota.
    If you learn't from defeat..
    You haven't really lost..

    Sridhar Thota.
    Editor: DNS Forum.


Sign In to post your comments