How to restrict a specific row being deleted in MySQL












1















I'm using jComboBox to select a Supplier from table so I have created -1 id row in a table for "none" selected Supplier.



-1 row is also appearing to user where I've fetched all records in
jTable.



I don't want user delete this row may either accidentally or consciously.



So may I have something like this?

I'm using MySQL and Java.



String comName = (String) jSupplrsComboBox.getSelectedItem();

Suppliers suppliers = manager.searchSuppCompny(comName);
if (comName.equals("none")) {
receivings.getSuppliers().setId(-1);
receivings.setSuppliers(suppliers);
}
receivings.setSuppliers(suppliers);


Fetched Suppliers to jComboBox and just manually addd "none" text so it doesn't match in Suppliers table.










share|improve this question

























  • I don't think a "no supplier" thing should even be in the database.

    – f1sh
    Dec 31 '18 at 9:49






  • 1





    To expand on @f1sh 's reply -- having a dummy record in the db is just asking for trouble -- you will have to remember to exclude it whenever you write a query involving that table, which will make your code more complicated, error prone, and potentially slower. Just add the special value to the combo box when it is created.

    – tgdavies
    Dec 31 '18 at 9:53


















1















I'm using jComboBox to select a Supplier from table so I have created -1 id row in a table for "none" selected Supplier.



-1 row is also appearing to user where I've fetched all records in
jTable.



I don't want user delete this row may either accidentally or consciously.



So may I have something like this?

I'm using MySQL and Java.



String comName = (String) jSupplrsComboBox.getSelectedItem();

Suppliers suppliers = manager.searchSuppCompny(comName);
if (comName.equals("none")) {
receivings.getSuppliers().setId(-1);
receivings.setSuppliers(suppliers);
}
receivings.setSuppliers(suppliers);


Fetched Suppliers to jComboBox and just manually addd "none" text so it doesn't match in Suppliers table.










share|improve this question

























  • I don't think a "no supplier" thing should even be in the database.

    – f1sh
    Dec 31 '18 at 9:49






  • 1





    To expand on @f1sh 's reply -- having a dummy record in the db is just asking for trouble -- you will have to remember to exclude it whenever you write a query involving that table, which will make your code more complicated, error prone, and potentially slower. Just add the special value to the combo box when it is created.

    – tgdavies
    Dec 31 '18 at 9:53
















1












1








1








I'm using jComboBox to select a Supplier from table so I have created -1 id row in a table for "none" selected Supplier.



-1 row is also appearing to user where I've fetched all records in
jTable.



I don't want user delete this row may either accidentally or consciously.



So may I have something like this?

I'm using MySQL and Java.



String comName = (String) jSupplrsComboBox.getSelectedItem();

Suppliers suppliers = manager.searchSuppCompny(comName);
if (comName.equals("none")) {
receivings.getSuppliers().setId(-1);
receivings.setSuppliers(suppliers);
}
receivings.setSuppliers(suppliers);


Fetched Suppliers to jComboBox and just manually addd "none" text so it doesn't match in Suppliers table.










share|improve this question
















I'm using jComboBox to select a Supplier from table so I have created -1 id row in a table for "none" selected Supplier.



-1 row is also appearing to user where I've fetched all records in
jTable.



I don't want user delete this row may either accidentally or consciously.



So may I have something like this?

I'm using MySQL and Java.



String comName = (String) jSupplrsComboBox.getSelectedItem();

Suppliers suppliers = manager.searchSuppCompny(comName);
if (comName.equals("none")) {
receivings.getSuppliers().setId(-1);
receivings.setSuppliers(suppliers);
}
receivings.setSuppliers(suppliers);


Fetched Suppliers to jComboBox and just manually addd "none" text so it doesn't match in Suppliers table.







java mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 10:39







Swapnil Nagtilak

















asked Dec 31 '18 at 9:45









Swapnil NagtilakSwapnil Nagtilak

406110




406110













  • I don't think a "no supplier" thing should even be in the database.

    – f1sh
    Dec 31 '18 at 9:49






  • 1





    To expand on @f1sh 's reply -- having a dummy record in the db is just asking for trouble -- you will have to remember to exclude it whenever you write a query involving that table, which will make your code more complicated, error prone, and potentially slower. Just add the special value to the combo box when it is created.

    – tgdavies
    Dec 31 '18 at 9:53





















  • I don't think a "no supplier" thing should even be in the database.

    – f1sh
    Dec 31 '18 at 9:49






  • 1





    To expand on @f1sh 's reply -- having a dummy record in the db is just asking for trouble -- you will have to remember to exclude it whenever you write a query involving that table, which will make your code more complicated, error prone, and potentially slower. Just add the special value to the combo box when it is created.

    – tgdavies
    Dec 31 '18 at 9:53



















I don't think a "no supplier" thing should even be in the database.

– f1sh
Dec 31 '18 at 9:49





I don't think a "no supplier" thing should even be in the database.

– f1sh
Dec 31 '18 at 9:49




1




1





To expand on @f1sh 's reply -- having a dummy record in the db is just asking for trouble -- you will have to remember to exclude it whenever you write a query involving that table, which will make your code more complicated, error prone, and potentially slower. Just add the special value to the combo box when it is created.

– tgdavies
Dec 31 '18 at 9:53







To expand on @f1sh 's reply -- having a dummy record in the db is just asking for trouble -- you will have to remember to exclude it whenever you write a query involving that table, which will make your code more complicated, error prone, and potentially slower. Just add the special value to the combo box when it is created.

– tgdavies
Dec 31 '18 at 9:53














2 Answers
2






active

oldest

votes


















1














Well I tried myself and where I'm retrieving records I changed code:



DefaultTableModel suppliersModel = (DefaultTableModel) jSuppliersTable.getModel();
for(Suppliers suppliers : manager.allSuppliers()) {
suppliersModel.addRow(new Object{suppliers.getId()});

int id = (Integer) suppliersModel.getValueAt(0, 0);
if(id == -1) {
suppliersModel.removeRow(0);
}


That retrieve records after first row which is -1 id in the database table.

Tahnks.






share|improve this answer

































    0














    I'm not sure what exactly you're trying to achieve, but if you're utilizing a JComboBox, I imagine you're filtering your database by the supplier indicated by the comboBox selection. This also means your comboBox model needs to be populated with entries that exist in your database.



    This is a quick way to set Strings to the ComboBox from an Enum, passing entries from the database should work in a similar way, but ensuring duplication does not exist may need to be addressed



    jSupplrsComboBox.setModel(new DefaultComboBoxModel<>(SupplierId.values()));


    From link Java - Check Boxes in a JComboBox, response by Jonathan Feinberg might be better suited for more than one filter.



    As for MYSQL, I'm assuming you have access to make modifications, it may be better if you add an extra field, say isValid, to indicate if said entry is valid or not. In which case you can filter on jSupplrsComboBox.getSelectedItem() with an additional WHERE isValid = 1.
    But your question was geared more towards Java than MYSQL, so I won't mention more on that. Check out MySQL Boolean Question and associated links for more on that.






    share|improve this answer
























    • Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

      – DarceVader
      Dec 31 '18 at 13:47













    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%2f53985951%2fhow-to-restrict-a-specific-row-being-deleted-in-mysql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Well I tried myself and where I'm retrieving records I changed code:



    DefaultTableModel suppliersModel = (DefaultTableModel) jSuppliersTable.getModel();
    for(Suppliers suppliers : manager.allSuppliers()) {
    suppliersModel.addRow(new Object{suppliers.getId()});

    int id = (Integer) suppliersModel.getValueAt(0, 0);
    if(id == -1) {
    suppliersModel.removeRow(0);
    }


    That retrieve records after first row which is -1 id in the database table.

    Tahnks.






    share|improve this answer






























      1














      Well I tried myself and where I'm retrieving records I changed code:



      DefaultTableModel suppliersModel = (DefaultTableModel) jSuppliersTable.getModel();
      for(Suppliers suppliers : manager.allSuppliers()) {
      suppliersModel.addRow(new Object{suppliers.getId()});

      int id = (Integer) suppliersModel.getValueAt(0, 0);
      if(id == -1) {
      suppliersModel.removeRow(0);
      }


      That retrieve records after first row which is -1 id in the database table.

      Tahnks.






      share|improve this answer




























        1












        1








        1







        Well I tried myself and where I'm retrieving records I changed code:



        DefaultTableModel suppliersModel = (DefaultTableModel) jSuppliersTable.getModel();
        for(Suppliers suppliers : manager.allSuppliers()) {
        suppliersModel.addRow(new Object{suppliers.getId()});

        int id = (Integer) suppliersModel.getValueAt(0, 0);
        if(id == -1) {
        suppliersModel.removeRow(0);
        }


        That retrieve records after first row which is -1 id in the database table.

        Tahnks.






        share|improve this answer















        Well I tried myself and where I'm retrieving records I changed code:



        DefaultTableModel suppliersModel = (DefaultTableModel) jSuppliersTable.getModel();
        for(Suppliers suppliers : manager.allSuppliers()) {
        suppliersModel.addRow(new Object{suppliers.getId()});

        int id = (Integer) suppliersModel.getValueAt(0, 0);
        if(id == -1) {
        suppliersModel.removeRow(0);
        }


        That retrieve records after first row which is -1 id in the database table.

        Tahnks.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 1 at 11:01

























        answered Jan 1 at 10:36









        Swapnil NagtilakSwapnil Nagtilak

        406110




        406110

























            0














            I'm not sure what exactly you're trying to achieve, but if you're utilizing a JComboBox, I imagine you're filtering your database by the supplier indicated by the comboBox selection. This also means your comboBox model needs to be populated with entries that exist in your database.



            This is a quick way to set Strings to the ComboBox from an Enum, passing entries from the database should work in a similar way, but ensuring duplication does not exist may need to be addressed



            jSupplrsComboBox.setModel(new DefaultComboBoxModel<>(SupplierId.values()));


            From link Java - Check Boxes in a JComboBox, response by Jonathan Feinberg might be better suited for more than one filter.



            As for MYSQL, I'm assuming you have access to make modifications, it may be better if you add an extra field, say isValid, to indicate if said entry is valid or not. In which case you can filter on jSupplrsComboBox.getSelectedItem() with an additional WHERE isValid = 1.
            But your question was geared more towards Java than MYSQL, so I won't mention more on that. Check out MySQL Boolean Question and associated links for more on that.






            share|improve this answer
























            • Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

              – DarceVader
              Dec 31 '18 at 13:47


















            0














            I'm not sure what exactly you're trying to achieve, but if you're utilizing a JComboBox, I imagine you're filtering your database by the supplier indicated by the comboBox selection. This also means your comboBox model needs to be populated with entries that exist in your database.



            This is a quick way to set Strings to the ComboBox from an Enum, passing entries from the database should work in a similar way, but ensuring duplication does not exist may need to be addressed



            jSupplrsComboBox.setModel(new DefaultComboBoxModel<>(SupplierId.values()));


            From link Java - Check Boxes in a JComboBox, response by Jonathan Feinberg might be better suited for more than one filter.



            As for MYSQL, I'm assuming you have access to make modifications, it may be better if you add an extra field, say isValid, to indicate if said entry is valid or not. In which case you can filter on jSupplrsComboBox.getSelectedItem() with an additional WHERE isValid = 1.
            But your question was geared more towards Java than MYSQL, so I won't mention more on that. Check out MySQL Boolean Question and associated links for more on that.






            share|improve this answer
























            • Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

              – DarceVader
              Dec 31 '18 at 13:47
















            0












            0








            0







            I'm not sure what exactly you're trying to achieve, but if you're utilizing a JComboBox, I imagine you're filtering your database by the supplier indicated by the comboBox selection. This also means your comboBox model needs to be populated with entries that exist in your database.



            This is a quick way to set Strings to the ComboBox from an Enum, passing entries from the database should work in a similar way, but ensuring duplication does not exist may need to be addressed



            jSupplrsComboBox.setModel(new DefaultComboBoxModel<>(SupplierId.values()));


            From link Java - Check Boxes in a JComboBox, response by Jonathan Feinberg might be better suited for more than one filter.



            As for MYSQL, I'm assuming you have access to make modifications, it may be better if you add an extra field, say isValid, to indicate if said entry is valid or not. In which case you can filter on jSupplrsComboBox.getSelectedItem() with an additional WHERE isValid = 1.
            But your question was geared more towards Java than MYSQL, so I won't mention more on that. Check out MySQL Boolean Question and associated links for more on that.






            share|improve this answer













            I'm not sure what exactly you're trying to achieve, but if you're utilizing a JComboBox, I imagine you're filtering your database by the supplier indicated by the comboBox selection. This also means your comboBox model needs to be populated with entries that exist in your database.



            This is a quick way to set Strings to the ComboBox from an Enum, passing entries from the database should work in a similar way, but ensuring duplication does not exist may need to be addressed



            jSupplrsComboBox.setModel(new DefaultComboBoxModel<>(SupplierId.values()));


            From link Java - Check Boxes in a JComboBox, response by Jonathan Feinberg might be better suited for more than one filter.



            As for MYSQL, I'm assuming you have access to make modifications, it may be better if you add an extra field, say isValid, to indicate if said entry is valid or not. In which case you can filter on jSupplrsComboBox.getSelectedItem() with an additional WHERE isValid = 1.
            But your question was geared more towards Java than MYSQL, so I won't mention more on that. Check out MySQL Boolean Question and associated links for more on that.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 31 '18 at 13:18









            DarceVaderDarceVader

            907




            907













            • Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

              – DarceVader
              Dec 31 '18 at 13:47





















            • Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

              – DarceVader
              Dec 31 '18 at 13:47



















            Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

            – DarceVader
            Dec 31 '18 at 13:47







            Then perhaps the second half of my answer, where I mentioned adding an isValid column to your database table. Otherwise, I don't fully understand what it is you're asking

            – DarceVader
            Dec 31 '18 at 13:47




















            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%2f53985951%2fhow-to-restrict-a-specific-row-being-deleted-in-mysql%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas