I am debugging an web application that uses MySQL. I am seeing lot of tables getting modified between different flows ( enrollment , edit etc .. ) . And also , it is very difficult to go through the code base to understand what are all the tables got modified between different invocations .
Is there a better wat to know what are all the tables got modiifed between two times ( two invocations ).
Have general query log enabled to know what all queries are getting executed.
I came across this- How can I tell when a MySQL table was last updated?
In later versions of MySQL you can use the information_schema database
to tell you when another table was updated:
SELECT UPDATE_TIME FROM information_schema.tables WHERE
TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'tabname'
This does of course mean opening a connection to the database.
See show-table-status
SHOW TABLE STATUS FROM db_name WHERE Update_time BETWEEN (<date1> AND <date2>)
The following will return you different timestamps about your database. See if this helps you.
SELECT *
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'dbname'
You may also consider using a mysql profiler like http://www.jetprofiler.com/
Enable binlog on your server, which is better for your purpose than the querylog since it only contains changes (updates, inserts, deletes).
Hoevery, you have to use mysqlbinlog commandline tool to transform it into a plain text file:
mysqlbinlog server-bin.00001 > binlog.txt
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.
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 have a situation where I have an application that uses multiple SQL Server 2008 databases (all with different schemas - split by application function).
I now want to combine these schemas and data into a single overall application database (SQL Server 2008). I've searched for help on how to do this, but all the solutions I've seen have covered merging data from databases with identical schemas. In this case, there is no commonality in the schemas of the separate databases - all the tables are unique. For this, I just want to combine all the tables and data into a single database, and I'm really looking for a tool that will help me do it simply and easily.
Can anyone give some guidance on how this could best be achieved?
I guess you can create a script something like this ... in your Source database execute the following statement in with Results to Text selected
SELECT N'SELECT * INTO [dbo].['+ TABLE_NAME + N'] FROM [ServerName].[Database].['
+ TABLE_SCHEMA + N'].[' + TABLE_NAME + N']; ' + CHAR(10) + N'GO'
FROM information_schema.tables
WHERE TABLE_TYPE = 'BASE TABLE'
And it will generate the script as follows. all you need to do is copy it from results pane and paste it in query window , Target database context and job is done ....
SELECT * INTO [dbo].[TestTable] FROM [ServerName].[Database].[dbo].[TestTable];
GO
SELECT * INTO [dbo].[TableOne] FROM [ServerName].[Database].[Account].[TableOne];
GO
SELECT * INTO [dbo].[TableTwo] FROM [ServerName].[Database].[Account].[TableTwo];
GO
SELECT * INTO [dbo].[TableTHREE] FROM [ServerName].[Database].[Account].[TableTHREE];
GO
SELECT * INTO [dbo].[TableTHREE] FROM [ServerName].[Database].[HumanResource].[TableTHREE];
GO
SELECT * INTO [dbo].[TableOne] FROM [ServerName].[Database].[HumanResource].[TableOne];
GO
SELECT * INTO [dbo].[TableTwo] FROM [ServerName].[Database].[HumanResource].[TableTwo];
GO
Issues with this Script
1) Since you want to get tables from multiple schemas thrown into one
schema in the destination database there can be some duplicate Table
Name as I have shown in my above example.
2) Tables that has columns that references column in other
tables must me created after the table containing referecing column is
created.
I suppose you would not find such tool. You require custom functionality which is not very common. Therefore, I suppose nobody would make a tool for it. Your scenario is not very clear. I would see three ways of moving.
If this merge is going to happen once, then it is quite easy. You could export the tables in the form of a CREATE SQL statement, including data. You could use this tutorial to do it in SQL server. You should do this for every table and then run these scripts to create the tables into the new schema.
If you want to merge the databases contents in the target database in a regular basis, you have to create an SQL Server Job. This would have to execute the custom scripts which would pull data from the source databases and push them to the target database.
If you want to concatenate a number of smaller databases in a big data warehouse, then you are looking for the Business Intelligence Development Studio. You could use it to create an OLAP cube, concatenating data from the smaller databases in the big data warehouse.
Hope I helped!
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
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`;