Skip to content

BatchSqlUpdate – how to get auto generated keys

I am using spring BatchSqlUpdate to insert a set of rows. How do I get the auto generated keys for all of the rows inserted? When doing a single insert I get the keys like this – Thanks! Answer There is no provided solution for this using BatchSqlUpdate as far as I know, but you can always query the las…

Get last record of a table in Postgres

I’m using Postgres and cannot manage to get the last record of my table: How can I do that knowning that timestamp is a unique identifier of the record ? Answer If under “last record” you mean the record which has the latest timestamp value, then try this:

MySQL syntax: What is this?

My exported SQL file contains the lines below: What do these lines mean, unlike CREATE TABLE and INSERT INTO? Answer They are variable assignments. The assignments are wrapped in executable comments in such a way that they are executed when MySQL is used and left alone if some other RDBMS is used. Furthermore…

SQL One-to-One Relationship Definition

I’m designing a database and I’m not sure how to define one of the relationships. Here’s the situation: An invoice is created If the product is not in stock then it needs to be manufactured and so a …

Count number of occurrences in SQL

I have a table with the following structure: (table_name, column_name) and for each row in this table I need to query the column_name in the table_name and do a COUNT(column_name) GROUP BY …

Prevent operational risk with SQL-dependent architecture

I am currently working on a very critical tool, which has a strong dependency to a MySQL database. This particularly means that if the SQL server goes down some day, all users won’t be able to use the application, since it is retrieving their configuration stored in database and launches components defi…

How to use “Partition By” or “Max”?

I’ve the following table (my_data): what is the best / smallest SQL statement to retrieve only the data related to the highest year and grouped by ‘X’ , like this: Note that this result table will be used in a join. Answer