When writing a query, can I reference the current database's name, sort of like with the this keyword in Java? I am writing a script that I will run against several similar databases, and I need to query the information_schema.KEY_COLUMN_USAGE table with the CONSTRAINT_SCHEMA column in my WHERE clause.
SELECT DATABASE() gives you the selected database
http://dev.mysql.com/doc/refman/5.0/en/creating-database.html
Related
I am facing a problem in searching a particular table_name.
I have around 50 databases on the server and i wish to search a table_name say X is created in which all databases.
Is there any straight-forward to find the exact databases in which X table is found in MYSQL through phpMyAdmin.
Any help will be appreciated.
Thanks
You can query the information_schema database for this. The below query will return the names of all the databases, which contains the table your_table_name.
SELECT `TABLE_SCHEMA`
FROM `information_schema`.`TABLES`
WHERE `TABLE_NAME` = 'your_table_name'
I hope this is what you are looking for.
According to the MySQL documentation about information_schema database,
INFORMATION_SCHEMA provides access to database metadata.
Metadata is data about the data, such as the name of a database or
table, the data type of a column, or access privileges. Other terms
that sometimes are used for this information are data dictionary and
system catalog.
Please query the information_schema.TABLES with table_type='BASE TABLE' and providing the name of the table(table_name).
This query will give us all the tables with '<your_table_name>' in all the databases that are currently in our server.
Please change '<your_table_name>' as per your requirement.
SELECT TABLE_SCHEMA, TABLE_NAME
FROM `information_schema`.`TABLES`
where table_type='BASE TABLE'
and table_name = '<your_table_name>'
limit 100;
I going to list all columns of a table except PRIMARY key in mysql.
Note : operation is automatically so I don't know the names of fields.
Can you help me for making this query?
This is something worth doing some research on, if you are going to be working with databases in any length.
All DBMS I have worked with thus far have a means of looking at the constraints, columns, and table information. The ones for MySQL that will help you do what you want are likely in the INFORMATION_SCHEMA:
TABLE_CONSTRAINTS The MySQL Reference for this is here.
SELECT table_name, constraint_name, constraint_type FROM INFORMATION_SCHEMA.table_constraints;
COLUMNS The MySQL reference for this is here.
SELECT column_name FROM INFORMATION_SCHEMA.columns;
KEY_COLUMN_USAGE
You should be able to do something like this to get what you want:
SELECT INFORMATION_SCHEMA.key_column_usage.column_name
FROM INFORMATION_SCHEMA.key_column_usage
JOIN INFORMATION_SCHEMA.table_constraints
ON INFORMATION_SCHEMA.key_column_usage.column_name = INFORMATION_SCHEMA.table_constraints.column_name
WHERE INFORMATION_SCHEMA.table_constraints.constraint_type <> 'PRIMARY KEY'
The should be essentially what you need. Views/tables like these can be your best friend when needing to get information about your schema.
I hope that this information helps.
Is there any way to know the time when last select statement has been performed on a table? I am using the InnoDB storage engine.
I have tried with the following query:
select update_time,table_name from information_schema.tables where table_schema='databasename';
..but I'm receiving NULL in the update_time column.
Unless you manually update a last_accessed-field on the table, my best bet would be to add query logging and parse the log-files.
I googled and found these relates questions:
When was the last time a mysql table was accessed?
How do you get the last access (and/or write) time of a MySQL database?
SELECT UPDATE_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'`
this seems stupid, but I really don't know this thing.
Today, I am in need to get number of properties in a mysql table (in phpmyadmin).
Can I get the count of the column names in phpmyadmin (mysql)?
For that You can write sql query in phpmyadmin.
SELECT count(*)
FROM information_schema.columns
WHERE table_name = '<table_name>'
I am trying to get a list of tables and their number of rows. I have been using this query:
SELECT TABLE_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA WHERE TABLE_SCHEMA = 'myDatabase'
I am finding this sometimes returns null. What I would like to do is catch this probably doing something similar to
IFNULL ( TABLE_ROWS, SELECT COUNT(*) FROM ????)
Only I'm not sure what I should enter for ????
how to make it dynamic based on the TABLE_NAME column?.
Edit: Additional information: I found that the 'tables' not displaying are actually views.
Might as well do
SHOW TABLE STATUS FROM YOUR_DATABASE;
It always return a column named as Rows which is number of rows in that table.
From the documentation:
The TABLE_ROWS column is NULL if the table is in the INFORMATION_SCHEMA database.
So, you will propably not need it.