How do I use ONLY ONE sql query for conditional insert? -
i using swing menu java app. have mysql database. there's column (named brind) in 1 of tables in database has have unique values (other id column values auto inserted). through text fields on jframe form try insert values in table if brind value not exist in table (that is, in column). need have 1 mysql query this, call conditional insert. how do this? thanks
a query following should asking:
insert `thetable` select 'values', 'to', 'insert' not exists ( select * `thetable` brind='whatever' ) where values,to,insert should replaced values of fields inserting.
however, better served of other suggestions here, e.g.
- check value first , don't insert if exists (do within 1 transaction handle concurrency issues, presuming transaction isolation set appropriately),
- attempt insert , handle unique constraint violation failure.
option 2 option, in opinion.
Comments
Post a Comment