Retrieve Comments from tables and columns in a Mysql database - mysql

i have to build an application to manage an existing MySQL database. This database was created with MySQLworkbench and some useful comments were added to its tables and columns.
I think it would be great to somehow, query that comments and show them to the user to explain "what that field is". The problem is i don't know if its possible to retrieve that comments (they are only visible from the workbench).
EDIT:
in the MySQL existing database i have to work with there is no INFORMATION_SCHEMA table. I think is something usual to find it but in my model there is no :S

Try with:
SELECT column_comment FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table_name';

You can parse the output of
show create table `YOURTABLE`;

Related

Is there any way join tables of MySql and PostgreSQL?

I have a table in MySql in one server and a table in PostgreSQL in another server.
I want use use JOIN operation with those tables.
Is there any way to join the columns?
If not, is there any way to print the rows in same order?
Please help!!
Use mysql_fdw to define the MySQL table as a foreign table in PostgreSQL. Then you can join it with the PostgreSQL table in PostgreSQL.
You can use Materialize to achieve this.
Here is a sample demo project that you can run to see this in action:
You can find the code for the demo project and how to run it on GitHub here:
https://github.com/bobbyiliev/materialize-tutorials/tree/main/mz-join-mysql-and-postgresql
Hope this reference helps.
Yes, it is possible to work with multiple databases at the same time but you're looking in the wrong place. psycopg2 is just a library that simplifies accessing and manipulating data coming out of PostgreSQL but it doesn't go far beyond what you can do with psql. What you're looking to do you can solve on the database level by using Foreign Data Wrappers.
This does become more complicated in your schema definition but brings remote tables from host some.other.server database remote_db to appear as though they live on localhost in database local_db....
More:
https://dba.stackexchange.com/a/120682/197899

MySQL Update of Columns using INFORMATION_SCHEMA - is this possible?

I know that I can view the properties and do some amazing things to learn my db/table/field structure using INFORMATION_SCHEMA.
However, is it possible to update a field value in for example the COLUMNS table and thus update the actual column? For example setting nullable from NO to YES.
If this is not directly possible, I realize that I could use the query to CONCAT an ALTER string and then run those strings. However is there in that case a way to instead run an eval() command to do this in one operation? Thanks.
To learn INFORMATION_SCHEMA: I'd start from reading documentation - INFORMATION_SCHEMA Tables. Then I'd try to edit objects and see what happens in these tables.
INFORMATION_SCHEMA tables are system tables, they cannot be modified. ALTER TABLE is the only way to change a table.
It was a beautiful idea, but unfortunately, it won't work.
The tables in INFORMATION_SCHEMA are read-only views, and are not actually tables. So there aren't any files or directories associated with them. You can only read their contents and can't run INSERT, UPDATE, or DELETE operations on them.
See https://dev.mysql.com/doc/refman/5.7/en/information-schema-introduction.html#information-schema-usage-notes for the lowdown.
And FYI, there's a Database Administrators Stack Exchange site if you want more targeted answers to database questions.

Get MySQL database structure alter queries

I'm working on a version control program, and I would like to implement database structure versioning as well.
Is there a way to get a list of all the queries that have altered the databse structure in any way?
For example I added a column to the 'users' table called 'remember_token'. Is there a way I can get the specific query that was executed on the MySQL server in order to add that column?
You may want to enable the mysql query log and then filter on ALTER queries or anything you need

mysql details about tables

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

How to get list of tables, that don`t have a specific column in MySQL

I need to have a list of tables in MySQL, that don`t have a column 'created' or 'modified', so I can add them if non existant. How can I achieve this?
Thanks in advance for any help / hint.
Query INFORMATION_SCHEMA database for this.
http://dev.mysql.com/doc/refman/5.1/en/information-schema.html
You can do this from PHP. Connect to the database and browse the mysql table. It will have all table definitions, then run DESCRIBE on each table, that will get you the structure.
I can give you some code examples if you like