I want to search a single string or word from MYSQL database that contains more than 50 table.I do not know what is the column or table where this string may be located.is there any command or function that return table and column name that contain the desired string.please help
You can get a list of all the tables and the columns available in a database using the below query.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS;
Using the values get from TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, you can get the matched table name and column name using the below query.
SELECT * FROM table_name WHERE column_name LIKE "%your_string%";
Related
I am new to SQL, I tried to dump all data from a table by using
SELECT *
FROM 'table_name;
but it says 'NO rows returned'
But when I try to fetch column name from the same table by
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name'
it shows all the column names available in the same table
So the question is there any way to get all the database available in the table?
SELECT * FROM 'table_name;
This query is returning empty because your table is empty.
If you want to fetch particular column, use below query.
SELECT column_name FROM table_name;
In MySQL, how to change all columns names of all tables to remove the string "_euro" from columns names?
I just could find a way to search tables having some columns containing "_euro" in their names:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
AND TABLE_SCHEMA='my_database'
For example, for the column named price_total_euro I want to rename it as price_total
Create a script with the following SQL:
SELECT CONCAT("ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," RENAME COLUMN ",COLUMN_NAME," TO ",REPLACE(COLUMN_NAME,"_euro",""),"; ")
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
output will be, multiple lines which look like this:
ALTER TABLE test.t1_euro RENAME COLUMN t1_euro TO t1;
Review the script, and execute it on your database (after making a backup....)
put your select into a stored procedure and make a cursor and loop on all and do the alter operation
you can find example for a procedure here : https://stackoverflow.com/questions/15786240/mysql-create-stored-procedure-syntax-with-delimiter#:~:text=Getting%20started%20with%20stored%20procedure%20syntax%20in%20MySQL%3A,Why%20didn%27t%20this%20work%3F%20...%20More%20items...%20
if your using python with MySQL Connector you could place the column headers into a list and then run it through a for loop?
for name in table_names:
if name.endswith("_euro"):
new_name = name.replace("_euro", "")
I have a Microsoft stored procedure that queries two MySQL databases using OpenQuery. The two MySQL databases should be have the same schemas, so I can run the same query on both.
However, we will soon alter the MySQL schemas, and add a column to a table. But the two MySQL databases won't happen at the same time, and I don't know the exact date of the releases.
I therefore want to write the query so that if the new column exists, then I use it in my select. If not, then I use a default value.
Is this possible? (That is have a query that handles differences in the table schema?)
(Not to be confused with 'coelesce' where the field definitely exists, but is simply null.)
You can use the following SELECT statement:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'database name' AND TABLE_NAME = 'your table name'
AND COLUMN_NAME = 'the column name you want to check for'
If the above returns a value, your column is there. If not, then run your alternative SELECT statement
Updated statement:
IF EXISTS (SELECT *
FROM OPENQUERY(servername, 'SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ''database name''
AND TABLE_NAME = ''your table name''
AND COLUMN_NAME = ''the column name you want to check for'' ))
I want to make an alias(as in bash) for the following MySQL query:
SHOW COLUMNS FROM table WHERE Field != 'col_name' AND Field != 'col_name';
I read something about views but it seems that I need a SELECT query to use them.
I want to type only something like: showcols in the MySQL prompt and in the background the above query to be executed, is that possible?
PS: I cannot use DESCRIBE because of the length of some enum fields in the table.
You can replace the show columns with a select from the information_schema database.
SELECT column_name FROM INFORMATION_SCHEMA.`columns`
WHERE column_name not IN ('col1','col2');
Now you can create a view based on this select:
CREATE VIEW as SELECT ......
I have a varchar with the value of say Foo somewhere in my database and I don't know where exactly.
Is it possible to create a query that will search all tables and all columns for this content? Similar to grep
There isn't an easy way to do this, but you could use the information schema to find all your varchar columns.
SELECT table_name, column_name
FROM information_schema.columns
WHERE data_type = 'varchar';
You could then write a query to produce a list of queries that you would need to run to search for your term.
SELECT CONCAT(
'select ', column_name,
' from ', table_name,
' where ', column_name, ' like \'%foo%\''
) AS stmt
FROM information_schema.columns
WHERE data_type = 'varchar';
A more advanced example might insert matches into a results table along with the table and column names.
There is a stored procedure here (http://forge.mysql.com/tools/tool.php?id=232) for MySQL which will create a table for storing output, then loop through information_schema database's COLUMNS table to obtain all database's table and column names. Next execute a count() query on database.table for each column with appropriate search string in where condition. If count() > 0, that perticular column has the search term, so it will insert that triplet (database name, table name, column name) into a table. Last Select * from table to view respective database table and column names having the search term.
I found a very very simple way of doing it.
mysqldump -u user -p mydatabase | grep foo