SQL query to find duplicate rows, in any table in Mysql database - mysql

I'm looking for a schema-independent query. The query should be equally capable of catching duplicate rows in either table in a database.I have number of tables without primary key. I have found a result for sql server [which i have most experience] but looking for same thing in mysql

Use dynamic SQL to generate the query using the column information in information_schema:
SET #tablename = 'yourTable';
SET #sql = (
SELECT CONCAT('SELECT *
FROM `', table_name, '`
GROUP BY ', GROUP_CONCAT(CONCAT('`', column_name, '`')), '
HAVING COUNT(*) > 1')
FROM information_schema.columns
WHERE table_name = #tablename );
PREPARE stmt FROM #sql;
EXECUTE stmt;

Related

How to pivot columns to rows in MySQL for N number of columns without using Union all

I already went through the details in the link (Mysql Convert Column to row (Pivot table )). As the number of Columns is high and using union all on all of them would be time taking. I decided to use the last resolution in the given link. I was able to run the query the results were:
The issue is the acct getting included as data and also I want to create a table from the result . So Can these entries be excluded and how can I create a table from the results? (new to SQL)
The Code:
SET SESSION group_concat_max_len = 92160;
SET #target_schema='rd';
SET #target_table='pbc_gl';
SET #target_where='`acct`';
SELECT
GROUP_CONCAT(qry SEPARATOR ' UNION ALL ')
INTO #sql
FROM (
SELECT
CONCAT('SELECT `acct`,', QUOTE(COLUMN_NAME), ' AS `Business_Unit`,`', COLUMN_NAME, '` AS `value` FROM `', #target_table, '` WHERE ', #target_where) qry
FROM (
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`=#target_schema
AND `TABLE_NAME`=#target_table
) AS `A`
) AS `B` ;
PREPARE s FROM #sql;
EXECUTE s;
DEALLOCATE PREPARE s;

How to call MySQL function for every column that is present in table?

I need to call a mySQL function for all columns in a table.
I know how to do it for a particular column
Like this:
UPDATE `table_name` set `column_name` = function_name(`column_name`)
But i have no clue how to do it for all columns at once.
Thanks in advance.
Little clarification: I dont want to manually mention all columns, as i probably could have 200 columns table.
But i have no clue how to do it for all columns at once.
You just can't - there is no such shortcut in the update syntax.
You can do this with a single update statement, but you need to enumerate each and every column, like:
update table_name set
column_name1 = function_name(column_name1),
column_name2 = function_name(column_name2),
column_name3 = function_name(column_name3)
An alternative would be to use dynamic SQL to programatically generate the proper query string from catalog table information_schema.columns, and then execute it. This seems uterly complicated for what looks like a one-shot task... But here is sample code for that:
-- input variables
set #table_schema = 'myschema';
set #table_name = 'mytable';
set #function_name = 'myfunction';
-- in case "GROUP_CONCAT()" returns more than 1024 characters
set session group_concat_max_len = 100000;
-- build the "set" clause of the query string
select
#sql := group_concat(
'`', column_name, '` = ', #table_schema, '.', #function_name, '(`', column_name, '`)'
separator ', '
)
from information_schema.columns
where table_schema = #table_schema and table_name = #table_name;
-- entire query string
set #sql := concat('update ', #table_schema, '.', #table_name, ' set ', #sql);
-- debug
select #sql mysql;
-- execute for real
prepare stmt from #sql;
execute stmt;
deallocate prepare stmt;

How to change all table prefix in a single query

I am pretty amateur in mysql..can you please tell me how can I change table prefixes of my whole database in a single query... I can do it manually, but its quite time consuming to change all the tables prefixes. Please help me out. Like isc_administrator_log to cus_administrator_log means isc_ to cus_
I found these two solutions but do not understand either of them.
SELECT
GROUP_CONCAT('RENAME TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` TO `',
TABLE_SCHEMA, '`.`prefix_', TABLE_NAME, '`;' SEPARATOR ' ')
FROM `TABLES` WHERE `TABLE_SCHEMA` = "test";
and
SELECT
CONCAT('RENAME TABLE ', GROUP_CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME,
'` TO `', TABLE_SCHEMA, '`.`prefix_', TABLE_NAME, '`')) AS q
FROM
`information_schema`.`Tables` WHERE TABLE_SCHEMA='test';
Both statements generate strings which happen to be SQL statements that you can then copy/paste at your prompt to rename your tables.
Just replace the following two strings with the actual values your need:
prefix_ : the prefix you want to add
'test' : the name of the database containing the tables you want to rename
Both statements are almost identical, the only difference is that information_schema is not explicitely mentioned in the first statement. Therefore the first statement must be run from the information_schema database (issue USE information_schema beforehands).
phpmyadmin : select database ; tab structure => Check all => (with selected list) select add prefix to table .
(is not query but it works)
You can do Dynamic SQL Query For MySQL 5.0.13
delimiter //
CREATE PROCEDURE dynamic(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
SET #s = CONCAT('SELECT 'RENAME TABLE ',
GROUP_CONCAT('', TABLE_SCHEMA, ''.'', TABLE_NAME,
' TO ', TABLE_SCHEMA, ''='.prefix_''', TABLE_NAME, '')) AS q
FROM
information_schema.Tables WHERE TABLE_SCHEMA='test'';;'
PREPARE stmt FROM #s;
EXECUTE stmt;
END
//
delimiter ;
Got the Reference from here

Determine max number of character in a field

The field size of the MySQL daabase I'm working on have been determine quite arbitrary and I'd like to review them based on its current data. So I'd like to determine the maximum number of character per field so I'm sure I won't lose any data when I update the field size.
Is there a feature in phpmyadmin or a SQL statement that can help me?
Thanks
use CHAR_LENGTH, ex
SELECT MAX(CHAR_LENGTH(column1)) maxCol1,
MAX(CHAR_LENGTH(column2)) maxCol2,
MAX(CHAR_LENGTH(column3)) maxCol3
FROM tableName
SQLFiddle Demo
You can also use Dynamic SQL if you have unknown number of columns. All you need to supply is the name of the database and the name of the table,
SET #db_Name = 'db_2_21a29';
SET #tb_name = 'TABLENAME';
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('MAX(CHAR_LENGTH(', COLUMN_NAME, ')) AS `', COLUMN_NAME, '`')
) INTO #sql
FROM information_schema.columns
WHERE TABLE_NAME = #tb_name AND
TABLE_SCHEMA = #db_Name;
SET #sql = CONCAT('SELECT ',#sql, 'FROM ', #tb_name);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SQLFiddle Demo
In phpmyadmin you are able to know this data from the Structure tab of your table. look at the attached image:

Select Statement For my Question

I have 10 fields in my table but i need 8 fields when i select , i dont want to specify select 1,2,3,4,5,6,7,8 from........ ,
Any easy way to get the 8 fields (Other hand i dont want to select primary,unique key fields)
see the answer in this :
Select all columns except one in MySQL?
Actually there is a way, you need to have permissions of course for doing this ...
SET #sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), '<columns_to_delete>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
Replacing <table>, <database> and <columns_to_delete>