In Sql Server Management Studio we could run query on a selected database using the USE statement. Is there an equivalent for this function in MySql?
The problem is, I would like to run a query on Information Schema:
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'campaign'
But in my program, all I have is a connection string and not the database name. If I am to use the above query under a specific database, I should first parse my connection string and look for the database name there and then change the query to:
SELECT * FROM databasename.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'campaign'
On Sql Server the first query only returned a result on my connection string to my destination database but here in MySql the first query apparently looks for the table in all databases and that is not what I want.
MySQL also has USE.
USE db1;
Documentation here: http://dev.mysql.com/doc/refman/5.0/en/use.html
Update: After re-reading your question, it seems like you might not actually want USE, but that you want to filter your results to a specific schema:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'campaign'
AND TABLE_SCHEMA = 'databasename'
It's not 100% clear from the INFORMATION_SCHEMA doc, but the TABLE_SCHEMA field in INFORMATION_SCHEMA.TABLES has the database name in MySQL. This is because in MySQL, schema and database are synonymous even so far that CREATE SCHEMA is a synonym to CREATE DATABASE.
If that doesn't work for you, one alternative is the SHOW TABLES statement.
Related
i'm working on a school project and i'm building a little server in NodeJS for our project, one of my tasks is to find a way to get the tables name of all "user"tables in the selected Database.
Since i need to connect to different SQL databases, MySQL - PostgreSQL - SQLite, i was looking to get the same result by exectuing the same query, i'll explain my self.
I've start working on MySQL and i've "found" this query:
`SELECT table_name FROM information_schema.tables WHERE table_schema ='${config.DB_Name}'`
And it correctly return all the table name from the selected database. After that i moved to work with PostgreSQL and that query (succesfully run) returns 0 result.
So i've start looking for another query that can return the same result in Postgres, and i found this:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
I was wondering, is there any Query which, run on MySQL-PostgreSQL-SQLite, give me the same kind of result?
Your PostgreSQL query will work fine on all databases that support SQL standard feature F021. If it doesn't work in MySQL, then either it doesn't support that feature, or it is a bug.
Note that what PostgreSQL and the SQL standard calls a schema is called "database" in MySQL.
how to create sql statement ?
say i have create a data base in MySQL and i want to port it another database.
so i want convert my database to sql statement.
Use mysqldump. It can do that and much more.
From the documentation:
The mysqldump client [produces] a set of SQL statements that can be run to reproduce the original schema objects, table data, or both.
I am using Squirrel SQL Client to access derby, sql server servers. I can access all the tables and their data from there. Could you please tell me a query to list all the column names, types with no data in Squirrel?
Not that familiar with Squirrel, but generally in SQL you can try:
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME='<YourTableName>'
I have a view on a Mysql database. I work with it using phpMyAdmin.
I need to see the select statement that builds up this query. Unfortunately I cannot have access to the database files (i know the query is stored there).
I need to find a way, using phpMyAdmin or an SQL statement, that allow me to see the select statement that is inside the view.
You can find this information in INFORMATION_SCHEMA database, e.g. -
SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = 'db_name' AND TABLE_NAME = 'view_name';
Also, try visual object editors or Object Viewer to view/edit views in dbForge Studio for MySQL (free express edition).
Try the sql query:
SHOW CREATE TABLE blah;
http://dev.mysql.com/doc/refman/5.0/en/show-create-table.html
I know about show tables and describe <table_name> commends from mysql, but I'll like to know if thee is any solution which will show me all the tables from a database togheter with the number of columns from each table.
It that possible?
The thing is that i need to compare two databases (with 52 tables each), which seems to have the same structure, but I'm not very sure.
If your user has the permissions, you can query the database "information_schema", table "COLUMNS".
It is a very easy thing to do, MySQL keeps information about its databases in the information_schema database, this acts as the metadata for MySQL so you can find pretty much any information you need provided you have the right privileges to access that database.
I just tested this on MySQL 5.1.54, run it in any database to get the tables with their associated number of columns
SELECT table_name, COUNT(column_name) AS num_columns
FROM information_schema.statistics S
WHERE table_schema = DATABASE()
GROUP BY table_name
ORDER BY table_name
Depending on your version of MySQL you can take a look at the INFORMATION SCHEMA: INFORMATION_SCHEMA Tables
If you are using PHPMyadmin, check for Data Dictionary option at the bottom on database structure tab.
You can compare your databases and view differences with Schema Comparison tool in dbForge Studio for MySQL.
Stand-alone tool - dbForge Schema Compare