What I need to do is retrieve the maximum primary key of all tables in my database at once? That is, my result will be like if I executed the below 2 queries:
SHOW TABLES FROM DATABASE_NAME
SELECT MAX(PRIMARY_KEY) AS maxId FROM TABLE
That is
(first column = TableName , second column = MAX(PK) Of that table)
Pardon if I am doing something wrong. I just do not want to write 80 queries because my database has 80 tables.
To find the maximum value of a column, use the MAX() aggregate function; it takes a column name or an expression to find the maximum value. In our example, the subquery returns the highest number in the column grade (subquery: SELECT MAX(grade) FROM student ).
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
SQL MIN() and MAX() Functions The MAX() function returns the largest value of the selected column.
To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.
If (and only if) your primary keys are AUTO INCREMENT
variables you can do this:
SELECT TABLE_NAME, AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'mydb'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With