In Sqlite, I can query for tables names and their structure like this:
SELECT name, sql
FROM sqlite_master
WHERE type = 'table' AND Name NOT LIKE '%sqlite%'
I want to query for database schematic for MySQL database. Some digging in phpMyAdmin, I end with this:
SELECT SCHEMA_NAME, TABLE_NAME
FROM SCHEMATA, TABLES
WHERE SCHEMA_NAME = TABLE_SCHEMA AND SCHEMA_NAME ='myDb'
Unfortunately, this will only output tables names. Is there any sql query do the some work of:
mysqldump -u root myDb --no-data=true --add-drop-table=false > myDb.sql
select *
from information_schema.columns
where table_schema = 'sqlite_master'
order by table_name, ordinal_position
based on the accepted answer from this SO question
How about
SHOW CREATE TABLE tablename;
Related
This query gives all the column names from all the databases.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'Customers'
But I need to get all the column names from a table from a specific database. Because I have multiple tables with the same name. How to do that? Is there anything like
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATABASE_NAME = 'database_name' TABLE_NAME = 'table_name'
I am using xampp server and phpMyAdmin.
Quote the fine manual:
26.3.8 The INFORMATION_SCHEMA COLUMNS Table
...
The COLUMNS table has these columns:
TABLE_CATALOG
The name of the catalog to which the table containing the column belongs. This value is always def.
TABLE_SCHEMA
The name of the schema (database) to which the table containing the column belongs.
...
..you should see it when you do SELECT * ?
You want something like this.
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'whatever'
ORDER BY TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION;
The MySQL INFORMATION_SCHEMA uses columns named TABLE_SCHEMA to hold the database for each table.
This is the SQL for my expected result
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='database_name' AND TABLE_NAME = 'table_name';
Could not validate table exists in MySQL.
Tried below query but no output.
if !(select * from information_schema.tables where table_name='sy_code')
To see the schema of a table, use query
DESCRIBE table_name;
To see all the existing table in our database, use query
SELECT table_name FROM information_schema.tables WHERE table_schema = 'your_db'
As in subject title.
My database contains a huge amount of auxiliary tables.
Would like to build a dropdown list to select table I would like to load content from.
This code ... does not work:
SELECT table_name FROM sys.tables WHERE table_name LIKE 'Mytable_%'
In mysql you could access to information_schema filter your db name
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_db_name'
and table_name LIKE 'Mytable_%'
and table_type = 'BASE TABLE';
I want to drop all databases with zero tables I was able to get the databases with tables using
SELECT table_schema, count(table_name) FROM information_schema.tables group by table_schema
But how can I delete the dbs not in this list. I can't do it manually because there are more then 500 dbs there.
About to know schemas without tables, you can try this:
SELECT * FROM information_schema.schemata S
WHERE NOT EXISTS
(SELECT 'TABLE' from information_schema.tables T
WHERE T.table_schema = S.schema_name)
Because in system table SCHEMATA you'll find all schemas of your server and in table TABLES you'll find all tables in all schemas
The upper query must be input on cursor, so you must use a prepared statement to execute your cursor, because your DROP DATABASE has a variable (your schema_table) and it can be ran only with a prepared statement
Used the method posted by #dnoeth in the comments with a slightly diffrent query to get the drop commands and then with some Notepad++ magic executed them to drop all empty databases
SELECT concat('drop database ',schema_name) FROM information_schema.schemata
WHERE schema_name NOT IN
(SELECT TABLE_SCHEMA FROM information_schema.tables)
When executing SHOW TABLES FROM your_db_name_here inside MySQL, the result has Tables_in_{your_db_name_here} as its indexes. I'd like to change that, like this:
SHOW TABLES as _tables FROM mydb;
or something like
SELECT Tables_in_mydb AS _tables FROM (SHOW TABLES FROM mydb);
Any ideas?
It's usually easier to interact with the INFORMATION_SCHEMA database:
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
Example:
SELECT TABLE_NAME AS _tables
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mydb'