I have this SQL query which is used to delete users.
DELETE FROM USERS WHERE USERNAME = ?
The problem is that I don't know is there any row success row removal or not. I always get success at the end.
Is there any way to get for example some confirmation from Oracle that row is deleted in my Java code?
executeUpdate() method of PreparedStatement gives You the number of rows deleted.If no rows have been deleted by the query You get 0.I think that's the easiest solution.
If You need to know which rows have been deleted You can user "Returning" clause, that will give You rows deleted.
Regards
You can use SQL%ROWCOUNT. It is an implicit cursor that gives the number of rows affected by SQL statement
declare
begin
DELETE FROM USERS WHERE USERNAME = ?;
dbms_output.put_line('Total Deleted:' ||sql%rowcount);
end;
This will give you the count of the number of rows deleted.
Related
I am creating a Java program that is connected with a MySQL database. I want to create a table in MySQL that has a limitation for 10 inputs or for 10 insert to statement. Can someone help me with this problem?
Try seeing this question: How can I set a maximum number of rows in MySQL table?
You should create a stored procedure to control that limit!
I would like to do a real time reading from mysql.
The idea is simple. I use the binary log to trigger the select statement.
Meanwhile I'd like to read only the new rows on every change.
And currently I just consider insert.
So when someone do
insert into sometable(uid,somecolumn) values(uid,something)
My code will be triggered and do
select from sometable where uid=uid
Of course I have already written down which columns are the primary key because it seems no information from binlog.
I cannot find a tool to analysis mysql insert statement. So I use the regex to find out which column equals which value, then extract primary keys.
BUT the real problems what will happen if I do
Insert into `table` (`col`) values (select 0 as `col` from `dummy`);
How can I find out the col=0?
Is it impossible that make a select statement that select the new changed rows, triggered by the insert statement?
In a TRIGGER, you have access to the OLD and NEW values. With them, you can write code (in the TRIGGER) to log, for example, just the changes. Something like...
IF NEW.col1 != OLD.col1 THEN INSERT INTO LOG ...; END;
IF NEW.col2 != OLD.col2 THEN INSERT INTO LOG ...; END;
I have an oracle query which i am trying to execute using jdbc. Following is the query.
insert into bd_vehicles_temp select * from bd_vehicles_temp_1
table bd_vehicles_temp_1 contains about 7000-10000 rows. If a primary key in bd_vehicles_temp_1 is already present in bd_vehicles_temp i get an SQLException : Unique key constraint.
the line of exception is offcourse pstmt.executeUpdate() in my code. Is there a way to pinpoint the row in bd_vehicles_temp_1 which causes exception.
Or do i have to do loop through rows in bd_vehicles_temp_1 and insert each row one by one ?
Thanks in Advance !
The only way (I know of) to find out which row causes the problem is to use Oracle's "log errors into" feature. That way the insert won't throw an exception and any row violating any constraint will be written into the error table specified.
To do that you first need to create a log table that holds the rejected rows:
EXECUTE DBMS_ERRLOG.CREATE_ERROR_LOG('BD_VEHICLES_TEMP');
That will create a table named ERR$_BD_VEHICLES_TEMP
Then run change your statement to this:
insert into bd_vehicles_temp
select *
from bd_vehicles_temp_1
LOG ERRORS REJECT LIMIT UNLIMITED;
The statement will continue even if a row fails to validate the constraints. After the statement is finished you can check the contents of the table ERR$_BD_VEHICLES_TEMP for the rows that violated a constraint including the error message and the values.
(Edit): If you want to stop at the first error (and see that in the log table), then leave out the REJECT LIMIT UNLIMITED clause.
More details are in the manual:
for DBMS_ERRLOG http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_errlog.htm#CEGBBABI
for the LOG ERRORS INTO clause: http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9014.htm#BGBEIACB
If you know the column that can cause the exception you can use (Oracle specific)
SELECT col FROM bd_vehicles_temp
INTERSECT
SELECT col FROM bd_vehicles_temp_1;
to identify all rows that are in both tables.
You are trying to insert into a table right? You should be using pstmt.executeUpdate() instead of pstmt.execute(). If there are already existing records in your table, then its better to delete all rows and add again if this statement is executed more than once.
Earlier I had 2000 records.
After I fired below query, I would be having 1500 records.
DELETE FROM logInfo WHERE datediff(now(), whatTime) >= 2
Is there any query which would tell me how many records are deleted by above records?
I know I can use below query before delete command, however I am just curious is there any other way to find after deletion.
SELECT COUNT(*) FROM logInfo WHERE datediff(now(), whatTime) >= 2
I need this in JAVA or MYSQL.
I know in php it would be mysql_affected_rows()
The preparedStatement.executeUpdate() returns the number of affected rows.
When you execute query, return for this query would be how many rows effected (boolean value). Which is nothing but how many rows deleted.
DELETE FROM logInfo WHERE datediff(now(), whatTime) >= 2
When i'm trying to delete a last row in a table using a PreparedStatement (I'm using a MySQL database), the row wasn't been deleted, I tried to use a DELETE FROM... command and a TRUNCATE command (using the executeUpdate() command), but none of those commands deleted that last row, What should I do, in order to be able to delete that last row?
this is the command i have written:
String sqlDelete = "DELETE FROM free_time WHERE therapist_id=? AND from=? AND to=? AND date=?";
And I have checked that the parameters that i'm sending to the Prepared Statement are correct, but still, the row isn't been deleted.
Thanks in advanced.
Last row doesn't mean anything in a relational database. You don't need to know how the rows are stored.
You should be using a WHERE clause to identify what you need to DELETE.
I wouldn't recommend TRUNCATE.
You are using key words, and obviously suppressing exceptions ;).
String sqlDelete = "DELETE FROM free_time WHERE therapist_id=? AND `from`=? AND `to`=? AND `date`=?";