Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert data and if already inserted then update in sql

Tags:

java

sql

I simply want to insert the data to a SQL database table and if there is some data inserted already then I want to update that data. How can I do this using Java. Kindly help me, and sorry for bad English in advance.

like image 764
zeeshan nisar Avatar asked Apr 06 '16 05:04

zeeshan nisar


People also ask

How do you update inserted data in SQL?

UPDATE Data Using SQL Just like with the SQL INSERT statement, first, we need to identify the table we're wanting to update. Then we use the SET clause which details the columns we want to update. Finally, we use the WHERE clause to pinpoint which rows we want to include in the update.

How do you update if exists else insert in one SQL statement?

How do you update if exists else insert in one SQL statement? If you only want to update one row of data in a table, you can use IF statement: IF EXISTS (SELECT 1 FROM Tbl WHERE UniqueColumn = 'Something') BEGIN. Using exists as below: UPDATE T SET –update.

Can we use insert or update in SQL?

INSERT OR UPDATE table query inserts or updates rows of data the come from the result set of a SELECT query. The columns in the result set must match the columns in the table. You can use INSERT OR UPDATE with a SELECT to populate a table with existing data extracted from other tables.

Does insert into update?

INSERT is a DML command for inserting one or more rows into a table in an RDBMS whereas UPDATE is a DML command to change or update values in a table of an RDBMS. Thus, this is the main difference between INSERT and UPDATE in SQL.


1 Answers

Set any field as the unique identity. For an example consider that employee details has to be entered in the table name **EmployeeDetails.**in this case employee_id can be considered as unique.

use SELECT query select * from EmployeeDetails where employee_id= "the unique keyvalue"; if the resultset is not empty then use UPDATE query to update the fields.

update EmployeeDetails set Employee_id=?,Full_name=?, Designation=?, Email_id=?, Password=? where Employee_id='" + id + "'"; If the resultset is empty then use the INSERT query to insert the values to the table

Insert into EmployeeDetails values(...)

like image 180
VALARMATHI Avatar answered Oct 10 '22 01:10

VALARMATHI