Basic query on Mysql Insert Statement - mysql

I have N number of records in a table ,i wanna move all records from one table other say old table as table1 and new as table2 .I have a query with sub query to select the records from the table for insertion .
Assuming as 10000 records While inserting on 6000 record it gets some exception in it , it got to an end,but still the table2 is empty , Here i wanna know that is the 5999 records where it would have been inserted in a databse ?
Thanks in advance ,,
if its unworthy to answer or any cause let me know the reason to down vote i can improve it

I have a query with sub query to select the records from the table for insertion
I assume you have some INESRT INTO table2(<COLUMN LIST>) SELECT <COLUMN LIST> FROM table1 WHERE ... that you are running to move the records.
If so, the INSERT statement is run as part of a transaction and will be committed only if the statement is executed successfully, i.e. if it is able to INSERT all the records returned by that SELECT query. Otherwise, the transaction gets rolled back and no records will be inserted.
Here i wanna know that is the 5999 records where it would have been inserted in a database?
These records would have been inserted into the worktable in tmp location while executing the INSERT statement. It would have been committed to the main table if everything had gone well.

Related

Delete only inserted rows in MySQL

I have a table in a MySQL database from which I want to select some data and insert into another table. The query is pretty straightforward
QUERY-1
Insert into ArchiveTable
select * from OriginalTable where ColumnName<utc_timestamp();
Now I want to delete the records from OriginalTable which were inserted in the ArchiveTable
QUERY-2
delete from OriginalTable
where ID in(select ID from OriginalTable where ColumnName<utc_timestamp();
The problem here is that, while QUERY-1 executes, there would be records which would fullfill the ColumnName
condition but wont be picked up by the query since it has already started executing.
Now on executing the delete statement(QUERY-2), those extra rows would be deleted from OriginalTable but I would have no record of them in the ArchiveTable since it was not picked up by the previous query.
In SQLServer this can be resolved by using the OUTPUT clause, but I am not sure what would be the best approach to handle this scenario.
Any help in this regard would be highly appreciated

MySQL select / update racing condition

I would like to query a database table for some of it's oldest entries and update them with a second query afterwards.
But how can I prevent that another process (that does the same) will return the same rows by the SELECT query and the UPDATE part will modify the entries twice?
As far as I see a simple transaction cannot prevent this from happening.
Use the SELECT ... FOR UPDATE mechanism to do this (see http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html)

Insert and Select query at the same time

I have two tables in MS Access
Table1 (name, carname)
Table2 (carname, agency)
I executed the following command
st.executeUpdate("insert into Table1 values('"+name+"','"+carname+"')");
and the value is inserted.
At the same time if the carname in Table2 is the same as user given it has to select the agency. How can I write the query? (This two process has to be done at the same time).
You can't do them "at the same time", Access isn't capable of multi-threading. The only way to do it is to run the statements in succession, or one after the other. If that's the case, just put a second INSERT staement into an If/Then/Else block and only run it if it fulfills your desired criteria. I honestly don't understand the question; are you trying to match up the last record inserted? If that's the case, just select the Max(PrimaryKey) and INNER JOIN it back to the table, and that will give you the record you last inserted.

MySQL UPDATE table 1 and INSERT on table2 if id doesnt exist

I have a left join query that shows all the fields from a primary table (tblMarkers) and the values from a second table (tblLocations) where there is matching record.
tblLocations does not have a record for every id in tblMarkers
$query ="SELECT `tblMarkers`.*,`tblLocation`.*,`tblLocation`.`ID` AS `markerID`
FROM
`tblMarkers`
LEFT JOIN `tblLocation` ON `tblMarkers`.`ID` = `tblLocation`.`ID`
WHERE
`tblMarkers`.`ID` = $id";
I am comfortable with using UPDATE to update the tblMarkers fields but how do I update or INSERT a record into tblLocations if the record does not exist yet in tblLocations.
Also, how do I lock the record I ma working on to prevent someone else from doing an update at the same time?
Can I also use UPDATE tblMarkers * or do I have to list every field in the UPDATE statement?
Unfortunately you might have to implement some validation in your outside script. There is an IF statement in SQL, but I'm not sure if you can trigger different commands based on it's outcome.
Locking
In terms of locking, you have 2 options. for MyISAM tables, you can only lock the entire table using http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html
LOCK TABLE users;
For InnoDB tables, there is no explicit 'lock' for single rows, however you can use transactions, to get exclusive rights during the operation. http://dev.mysql.com/doc/refman/5.0/en/innodb-locks-set.html
Update
There might be some shorthand notation, but I think you have to list every field in your query. Alternatively, you can always read the entire row, delete it and insert again using shorthand INSERT query. It all depends on how many fields you've got.

is the UPDATE command more resource hungry than INSERT

the scripts i've been working with in SQL work with close to 40,000 records and i've noticed a huge increase in execution time for when i use an UPDATE command
in 2 tables that have like 10 fields in them each, INSERT executes quicker for both combined than this UPDATE command
UPADTE table1
INNER JOIN table2 ON table1.primarykey = table2.primarykey
SET table1.code = table2.code
really what the UPDATE is doing is copying the code from one table to another where the identical records exists, this is because table1 is a staging table between 2 databases while table2 is a possessing table to insert staging table's data across multiple tables, both tables have the same number of record which is about 40,000
now to me UPDATE should be executing a lot quicker, considering it's only connecting 2 identical tables and inserting data for 1 field it should be running quicker than 2 INSERTS where 40,000 records are being created over 10 fields (so in other words, inserting 800,000 pieces of data) and i'm running the queries in a SQL console windows to avoid php timeouts
is UPDATE somehow more resource hungry than INSERT and is there any way to get it to go faster (apart from changing the fact that i use a separate table for processing, the staging table updates frequently so i copy the data like a snapshot and work with that, the code field is NULL to begin with so i only copy over records with a NULL code meaning records where code is not NULLhave already been worked with)
Is that UPDATE command the actual SQL? Because you need a WHERE clause to avoid updating every record in the table...
Also, INSERT doesn't first need to find the record to update from 2 joined tables.