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.
Related
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.
I've 700+ MySQL queries and now trying to create same set of queries in MSSQL.
I want to generate MSSQL query with same MYSQL.Is any way to convert MYSQL query into MSSQL query.
From this article by Brian Swan, you can download the SQL Server Migration Assistant for MySQL tool and use it to convert a single MySQL query to a SQL Server query:
Create a Project: All the information you need for downloading SSMA, creating a project and connecting to databases is in steps 1-6 of this blog post.
In the MySQL Metadata Explorer, navigate to the Statements directory of your MySQL database:
Paste the query you want to convert into the query editor window, e.g:
SELECT post_title, post_date FROM wp_posts ORDER BY post_date LIMIT 5 OFFSET 5;
Back in the MySQL metadata explorer, right-click Statements and select Convert Schema:
When prompted to save changes, select Yes:
Copy the converted query from the SQL Server query editor window:
Note that SSMA will not successfully translate all MySQL queries, but it does for most. It does not translate some MySQL-specific functions (for example FOUND_ROW()).
I managed to connect to MySQL DB via Sql Developer following this guide. MySQL DB shows and I can expolre tables via the navigator. However, I could not run SELECT statement to show any of these tables. In MySQL workbench I used to use :
use [database_name]
Then, run select statement in that database. But in Sql developer, I am not sure what should I add to the statement to make it work. I have tried the following:
select *
from [table_name].[database_name];
It does not work. I found this tutorial, but nothing is mentioned about simple select statement. Any help is deeply appreciated.
AFAIK, except MySQL specific commands; all other standard SQL commands like SELECT,INSERT,UPDATE,DELETE should work just fine using SQL Developer. but per your posted query, it looks total strange.
Your query
select * from [table_name]#[database_name];
remove that # sign.
you should qualify like database_name.table_name.
Unless it's a typo, remove those [] as well from your query
statement.
Your query should look like
select * from database_name.table_name;
You can always write your SQL including database as well, in the form of:
database.tablename
such as:
select * from wordpressdb.usertable where username="someone"
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