SQL Server - Inserting Multiple Values Into Column












2















I'm trying to update my pizza so it will allow more than one topping. I am receiving the error Cannot insert duplicate key row in object 'dbo.PizzaToppings' with unique index 'FK_Pizza'. The duplicate key value is (3). when trying to add more than one toppingID to the column. How can I add multiple ToppingID's?



INSERT INTO dbo.PizzaToppings
(PizzaToppingsID, PizzaID, ToppingsID)
VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 4, 7),
(5, 5, 6),
(6, 6, 4),
(7, 3, 7);


The FK_Pizza that was created



CREATE UNIQUE NONCLUSTERED INDEX
FK_Pizza
ON PizzaToppings
(
PizzaID ASC
);









share|improve this question

























  • Do you mean to say you are interested in storing the pizzatoppings as a comma_seperated string in the field ToppingsID?. It would be a bad design to have it like that. Instead store the PizzaToppingsID and ToppingsId is a seperate table

    – George Joseph
    Nov 20 '18 at 15:47











  • What is the syntax error you get ?

    – GGadde
    Nov 20 '18 at 15:49













  • I would like to see that index FK_Pizza. Basically it says u are trying to insert a record that is not distuingishable from other records mentioned in the index FK_Pizza. Can you supply us with a create-statement for that table?

    – Henrov
    Nov 20 '18 at 16:02













  • @Henrov Updated.

    – Cameron Cole
    Nov 20 '18 at 16:06
















2















I'm trying to update my pizza so it will allow more than one topping. I am receiving the error Cannot insert duplicate key row in object 'dbo.PizzaToppings' with unique index 'FK_Pizza'. The duplicate key value is (3). when trying to add more than one toppingID to the column. How can I add multiple ToppingID's?



INSERT INTO dbo.PizzaToppings
(PizzaToppingsID, PizzaID, ToppingsID)
VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 4, 7),
(5, 5, 6),
(6, 6, 4),
(7, 3, 7);


The FK_Pizza that was created



CREATE UNIQUE NONCLUSTERED INDEX
FK_Pizza
ON PizzaToppings
(
PizzaID ASC
);









share|improve this question

























  • Do you mean to say you are interested in storing the pizzatoppings as a comma_seperated string in the field ToppingsID?. It would be a bad design to have it like that. Instead store the PizzaToppingsID and ToppingsId is a seperate table

    – George Joseph
    Nov 20 '18 at 15:47











  • What is the syntax error you get ?

    – GGadde
    Nov 20 '18 at 15:49













  • I would like to see that index FK_Pizza. Basically it says u are trying to insert a record that is not distuingishable from other records mentioned in the index FK_Pizza. Can you supply us with a create-statement for that table?

    – Henrov
    Nov 20 '18 at 16:02













  • @Henrov Updated.

    – Cameron Cole
    Nov 20 '18 at 16:06














2












2








2








I'm trying to update my pizza so it will allow more than one topping. I am receiving the error Cannot insert duplicate key row in object 'dbo.PizzaToppings' with unique index 'FK_Pizza'. The duplicate key value is (3). when trying to add more than one toppingID to the column. How can I add multiple ToppingID's?



INSERT INTO dbo.PizzaToppings
(PizzaToppingsID, PizzaID, ToppingsID)
VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 4, 7),
(5, 5, 6),
(6, 6, 4),
(7, 3, 7);


The FK_Pizza that was created



CREATE UNIQUE NONCLUSTERED INDEX
FK_Pizza
ON PizzaToppings
(
PizzaID ASC
);









share|improve this question
















I'm trying to update my pizza so it will allow more than one topping. I am receiving the error Cannot insert duplicate key row in object 'dbo.PizzaToppings' with unique index 'FK_Pizza'. The duplicate key value is (3). when trying to add more than one toppingID to the column. How can I add multiple ToppingID's?



INSERT INTO dbo.PizzaToppings
(PizzaToppingsID, PizzaID, ToppingsID)
VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 4, 7),
(5, 5, 6),
(6, 6, 4),
(7, 3, 7);


The FK_Pizza that was created



CREATE UNIQUE NONCLUSTERED INDEX
FK_Pizza
ON PizzaToppings
(
PizzaID ASC
);






sql tsql sql-update






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 16:05







Cameron Cole

















asked Nov 20 '18 at 15:41









Cameron ColeCameron Cole

1798




1798













  • Do you mean to say you are interested in storing the pizzatoppings as a comma_seperated string in the field ToppingsID?. It would be a bad design to have it like that. Instead store the PizzaToppingsID and ToppingsId is a seperate table

    – George Joseph
    Nov 20 '18 at 15:47











  • What is the syntax error you get ?

    – GGadde
    Nov 20 '18 at 15:49













  • I would like to see that index FK_Pizza. Basically it says u are trying to insert a record that is not distuingishable from other records mentioned in the index FK_Pizza. Can you supply us with a create-statement for that table?

    – Henrov
    Nov 20 '18 at 16:02













  • @Henrov Updated.

    – Cameron Cole
    Nov 20 '18 at 16:06



















  • Do you mean to say you are interested in storing the pizzatoppings as a comma_seperated string in the field ToppingsID?. It would be a bad design to have it like that. Instead store the PizzaToppingsID and ToppingsId is a seperate table

    – George Joseph
    Nov 20 '18 at 15:47











  • What is the syntax error you get ?

    – GGadde
    Nov 20 '18 at 15:49













  • I would like to see that index FK_Pizza. Basically it says u are trying to insert a record that is not distuingishable from other records mentioned in the index FK_Pizza. Can you supply us with a create-statement for that table?

    – Henrov
    Nov 20 '18 at 16:02













  • @Henrov Updated.

    – Cameron Cole
    Nov 20 '18 at 16:06

















Do you mean to say you are interested in storing the pizzatoppings as a comma_seperated string in the field ToppingsID?. It would be a bad design to have it like that. Instead store the PizzaToppingsID and ToppingsId is a seperate table

– George Joseph
Nov 20 '18 at 15:47





Do you mean to say you are interested in storing the pizzatoppings as a comma_seperated string in the field ToppingsID?. It would be a bad design to have it like that. Instead store the PizzaToppingsID and ToppingsId is a seperate table

– George Joseph
Nov 20 '18 at 15:47













What is the syntax error you get ?

– GGadde
Nov 20 '18 at 15:49







What is the syntax error you get ?

– GGadde
Nov 20 '18 at 15:49















I would like to see that index FK_Pizza. Basically it says u are trying to insert a record that is not distuingishable from other records mentioned in the index FK_Pizza. Can you supply us with a create-statement for that table?

– Henrov
Nov 20 '18 at 16:02







I would like to see that index FK_Pizza. Basically it says u are trying to insert a record that is not distuingishable from other records mentioned in the index FK_Pizza. Can you supply us with a create-statement for that table?

– Henrov
Nov 20 '18 at 16:02















@Henrov Updated.

– Cameron Cole
Nov 20 '18 at 16:06





@Henrov Updated.

– Cameron Cole
Nov 20 '18 at 16:06












3 Answers
3






active

oldest

votes


















1














Each Pizza has to be Unique.....
Try this:



CREATE UNIQUE NONCLUSTERED INDEX
FK_Pizza
ON PizzaToppings
(
PizzaID ASC, ToppingsID ASC
);


Don't forget to drop the constraint first






share|improve this answer



















  • 1





    I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

    – Cameron Cole
    Nov 20 '18 at 16:13



















2














You're trying to store an array into a what looks like a number column.



Usually you would have a row for each individual topping:



Pizzas:
PizzaID integer
PizzaName varchar(32)

Toppings:
ToppingID integer
ToppingName varchar(32)

PizzaTopping:
PizzaID integer (fk to Pizzas.PizzaID)
ToppingID integer (fk to Toppings.ToppingID)


PizzaTopping would contain a row for each topping per pizza.



Edit for updated question:



Your error is down to a duplicate key violation. Check the constraints on the table, it looks like there is a constraint that prevents you adding more than one row per pizzaID






share|improve this answer


























  • Ahh yes, my question has been updated. I am receiving this error when trying to do so.

    – Cameron Cole
    Nov 20 '18 at 15:55











  • @CameronCole See edit

    – JohnHC
    Nov 20 '18 at 16:01











  • Mmm okay, I added my FK_Pizza to the question.

    – Cameron Cole
    Nov 20 '18 at 16:06











  • Thank you for the help :)

    – Cameron Cole
    Nov 20 '18 at 16:13



















0














Why is this clustered index named like a foreign key constraint?



CREATE UNIQUE NONCLUSTERED INDEX
FK_Pizza
ON PizzaToppings
(
PizzaID ASC
);


Does it have to be unique? Don't you mean this to just be an FK and not an index?



Shouldn't your unique key be PizzaToppingID?



Are you allowing identity insert? Or shouldn't you let it be an autoincremented ID and just insert PizzaID and ToppingID?



Also is there already a record with PizzaID = 3 already in this table? If you are looking to just maintain or UPSERT, then shouldn't a MERGE be better?



If this is a junction table between Pizza and topping, to show which toppings are allowed on which pizza, shouldn't it have 2 FKs and 1 unique index?






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396557%2fsql-server-inserting-multiple-values-into-column%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Each Pizza has to be Unique.....
    Try this:



    CREATE UNIQUE NONCLUSTERED INDEX
    FK_Pizza
    ON PizzaToppings
    (
    PizzaID ASC, ToppingsID ASC
    );


    Don't forget to drop the constraint first






    share|improve this answer



















    • 1





      I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13
















    1














    Each Pizza has to be Unique.....
    Try this:



    CREATE UNIQUE NONCLUSTERED INDEX
    FK_Pizza
    ON PizzaToppings
    (
    PizzaID ASC, ToppingsID ASC
    );


    Don't forget to drop the constraint first






    share|improve this answer



















    • 1





      I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13














    1












    1








    1







    Each Pizza has to be Unique.....
    Try this:



    CREATE UNIQUE NONCLUSTERED INDEX
    FK_Pizza
    ON PizzaToppings
    (
    PizzaID ASC, ToppingsID ASC
    );


    Don't forget to drop the constraint first






    share|improve this answer













    Each Pizza has to be Unique.....
    Try this:



    CREATE UNIQUE NONCLUSTERED INDEX
    FK_Pizza
    ON PizzaToppings
    (
    PizzaID ASC, ToppingsID ASC
    );


    Don't forget to drop the constraint first







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 16:08









    HenrovHenrov

    7111932




    7111932








    • 1





      I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13














    • 1





      I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13








    1




    1





    I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

    – Cameron Cole
    Nov 20 '18 at 16:13





    I had to do the same thing for my FK_Toppings but yes that worked. Thank you for the help :)

    – Cameron Cole
    Nov 20 '18 at 16:13













    2














    You're trying to store an array into a what looks like a number column.



    Usually you would have a row for each individual topping:



    Pizzas:
    PizzaID integer
    PizzaName varchar(32)

    Toppings:
    ToppingID integer
    ToppingName varchar(32)

    PizzaTopping:
    PizzaID integer (fk to Pizzas.PizzaID)
    ToppingID integer (fk to Toppings.ToppingID)


    PizzaTopping would contain a row for each topping per pizza.



    Edit for updated question:



    Your error is down to a duplicate key violation. Check the constraints on the table, it looks like there is a constraint that prevents you adding more than one row per pizzaID






    share|improve this answer


























    • Ahh yes, my question has been updated. I am receiving this error when trying to do so.

      – Cameron Cole
      Nov 20 '18 at 15:55











    • @CameronCole See edit

      – JohnHC
      Nov 20 '18 at 16:01











    • Mmm okay, I added my FK_Pizza to the question.

      – Cameron Cole
      Nov 20 '18 at 16:06











    • Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13
















    2














    You're trying to store an array into a what looks like a number column.



    Usually you would have a row for each individual topping:



    Pizzas:
    PizzaID integer
    PizzaName varchar(32)

    Toppings:
    ToppingID integer
    ToppingName varchar(32)

    PizzaTopping:
    PizzaID integer (fk to Pizzas.PizzaID)
    ToppingID integer (fk to Toppings.ToppingID)


    PizzaTopping would contain a row for each topping per pizza.



    Edit for updated question:



    Your error is down to a duplicate key violation. Check the constraints on the table, it looks like there is a constraint that prevents you adding more than one row per pizzaID






    share|improve this answer


























    • Ahh yes, my question has been updated. I am receiving this error when trying to do so.

      – Cameron Cole
      Nov 20 '18 at 15:55











    • @CameronCole See edit

      – JohnHC
      Nov 20 '18 at 16:01











    • Mmm okay, I added my FK_Pizza to the question.

      – Cameron Cole
      Nov 20 '18 at 16:06











    • Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13














    2












    2








    2







    You're trying to store an array into a what looks like a number column.



    Usually you would have a row for each individual topping:



    Pizzas:
    PizzaID integer
    PizzaName varchar(32)

    Toppings:
    ToppingID integer
    ToppingName varchar(32)

    PizzaTopping:
    PizzaID integer (fk to Pizzas.PizzaID)
    ToppingID integer (fk to Toppings.ToppingID)


    PizzaTopping would contain a row for each topping per pizza.



    Edit for updated question:



    Your error is down to a duplicate key violation. Check the constraints on the table, it looks like there is a constraint that prevents you adding more than one row per pizzaID






    share|improve this answer















    You're trying to store an array into a what looks like a number column.



    Usually you would have a row for each individual topping:



    Pizzas:
    PizzaID integer
    PizzaName varchar(32)

    Toppings:
    ToppingID integer
    ToppingName varchar(32)

    PizzaTopping:
    PizzaID integer (fk to Pizzas.PizzaID)
    ToppingID integer (fk to Toppings.ToppingID)


    PizzaTopping would contain a row for each topping per pizza.



    Edit for updated question:



    Your error is down to a duplicate key violation. Check the constraints on the table, it looks like there is a constraint that prevents you adding more than one row per pizzaID







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 16:01

























    answered Nov 20 '18 at 15:48









    JohnHCJohnHC

    8,75311229




    8,75311229













    • Ahh yes, my question has been updated. I am receiving this error when trying to do so.

      – Cameron Cole
      Nov 20 '18 at 15:55











    • @CameronCole See edit

      – JohnHC
      Nov 20 '18 at 16:01











    • Mmm okay, I added my FK_Pizza to the question.

      – Cameron Cole
      Nov 20 '18 at 16:06











    • Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13



















    • Ahh yes, my question has been updated. I am receiving this error when trying to do so.

      – Cameron Cole
      Nov 20 '18 at 15:55











    • @CameronCole See edit

      – JohnHC
      Nov 20 '18 at 16:01











    • Mmm okay, I added my FK_Pizza to the question.

      – Cameron Cole
      Nov 20 '18 at 16:06











    • Thank you for the help :)

      – Cameron Cole
      Nov 20 '18 at 16:13

















    Ahh yes, my question has been updated. I am receiving this error when trying to do so.

    – Cameron Cole
    Nov 20 '18 at 15:55





    Ahh yes, my question has been updated. I am receiving this error when trying to do so.

    – Cameron Cole
    Nov 20 '18 at 15:55













    @CameronCole See edit

    – JohnHC
    Nov 20 '18 at 16:01





    @CameronCole See edit

    – JohnHC
    Nov 20 '18 at 16:01













    Mmm okay, I added my FK_Pizza to the question.

    – Cameron Cole
    Nov 20 '18 at 16:06





    Mmm okay, I added my FK_Pizza to the question.

    – Cameron Cole
    Nov 20 '18 at 16:06













    Thank you for the help :)

    – Cameron Cole
    Nov 20 '18 at 16:13





    Thank you for the help :)

    – Cameron Cole
    Nov 20 '18 at 16:13











    0














    Why is this clustered index named like a foreign key constraint?



    CREATE UNIQUE NONCLUSTERED INDEX
    FK_Pizza
    ON PizzaToppings
    (
    PizzaID ASC
    );


    Does it have to be unique? Don't you mean this to just be an FK and not an index?



    Shouldn't your unique key be PizzaToppingID?



    Are you allowing identity insert? Or shouldn't you let it be an autoincremented ID and just insert PizzaID and ToppingID?



    Also is there already a record with PizzaID = 3 already in this table? If you are looking to just maintain or UPSERT, then shouldn't a MERGE be better?



    If this is a junction table between Pizza and topping, to show which toppings are allowed on which pizza, shouldn't it have 2 FKs and 1 unique index?






    share|improve this answer




























      0














      Why is this clustered index named like a foreign key constraint?



      CREATE UNIQUE NONCLUSTERED INDEX
      FK_Pizza
      ON PizzaToppings
      (
      PizzaID ASC
      );


      Does it have to be unique? Don't you mean this to just be an FK and not an index?



      Shouldn't your unique key be PizzaToppingID?



      Are you allowing identity insert? Or shouldn't you let it be an autoincremented ID and just insert PizzaID and ToppingID?



      Also is there already a record with PizzaID = 3 already in this table? If you are looking to just maintain or UPSERT, then shouldn't a MERGE be better?



      If this is a junction table between Pizza and topping, to show which toppings are allowed on which pizza, shouldn't it have 2 FKs and 1 unique index?






      share|improve this answer


























        0












        0








        0







        Why is this clustered index named like a foreign key constraint?



        CREATE UNIQUE NONCLUSTERED INDEX
        FK_Pizza
        ON PizzaToppings
        (
        PizzaID ASC
        );


        Does it have to be unique? Don't you mean this to just be an FK and not an index?



        Shouldn't your unique key be PizzaToppingID?



        Are you allowing identity insert? Or shouldn't you let it be an autoincremented ID and just insert PizzaID and ToppingID?



        Also is there already a record with PizzaID = 3 already in this table? If you are looking to just maintain or UPSERT, then shouldn't a MERGE be better?



        If this is a junction table between Pizza and topping, to show which toppings are allowed on which pizza, shouldn't it have 2 FKs and 1 unique index?






        share|improve this answer













        Why is this clustered index named like a foreign key constraint?



        CREATE UNIQUE NONCLUSTERED INDEX
        FK_Pizza
        ON PizzaToppings
        (
        PizzaID ASC
        );


        Does it have to be unique? Don't you mean this to just be an FK and not an index?



        Shouldn't your unique key be PizzaToppingID?



        Are you allowing identity insert? Or shouldn't you let it be an autoincremented ID and just insert PizzaID and ToppingID?



        Also is there already a record with PizzaID = 3 already in this table? If you are looking to just maintain or UPSERT, then shouldn't a MERGE be better?



        If this is a junction table between Pizza and topping, to show which toppings are allowed on which pizza, shouldn't it have 2 FKs and 1 unique index?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 16:14









        NicVerAZNicVerAZ

        355310




        355310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396557%2fsql-server-inserting-multiple-values-into-column%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith