SQL scratchpad
Fra Harald Grovens wiki
Useful SQL queries
Innhold |
[rediger] Change collation of all fields and tables in one or more databases
-- change default collation of tables in a database SELECT CONCAT('ALTER TABLE `', TABLE_SCHEMA,'`.`',TABLE_NAME, '` DEFAULT CHARACTER SET utf8 COLLATE utf8_danish_ci ; ') AS collationchange FROM information_schema.TABLES WHERE TABLE_SCHEMA<>'information_schema' AND TABLE_SCHEMA<>'mysql' AND TABLE_COLLATION <>'utf8_danish_ci' AND TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='name of database' UNION DISTINCT -- change default collation of a fields SELECT CONCAT('ALTER TABLE `', TABLE_SCHEMA,'`.`', TABLE_NAME, '` CHANGE `', COLUMN_NAME, '` `', COLUMN_NAME, '` ', COLUMN_TYPE, ' CHARACTER SET utf8 COLLATE utf8_danish_ci ;') AS collationchange FROM information_schema.COLUMNS WHERE COLLATION_NAME <>'utf8_danish_ci' AND (COLUMN_TYPE LIKE 'varchar%' OR COLUMN_TYPE LIKE '%TEXT') AND TABLE_SCHEMA<>'information_schema' AND TABLE_SCHEMA='name of database' ;
[rediger] Generical import script
-- lage generisk importskript SELECT CONCAT('\n-- ', `TABLE_NAME` , '\n' 'INSERT INTO ', `TABLE_SCHEMA` , '.', `TABLE_NAME` , '\n\nSELECT \n\t\'\' AS ', GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION SEPARATOR '\n\t\'\' AS ' ), `TABLE_SCHEMA` , '.', `TABLE_NAME` , '\nFROM ', `TABLE_SCHEMA` , '.', `TABLE_NAME` , '\n; \n' ) AS createstatement FROM information_schema.COLUMNS -- sett inn navn på database som skal WHERE TABLE_SCHEMA IN ('utdno2_geo') -- sett inn liste over tabeller det skal lages skriptfor AND TABLE_NAME IN ('node') GROUP BY TABLE_NAME ;
[rediger] Convert html entities with sql
UPDATE files SET html=REPLACE(html,'&Agrave;','À') ; -- capital a, grave accent UPDATE files SET html=REPLACE(html,'&Aacute;','Á') ; -- capital a, acute accent UPDATE files SET html=REPLACE(html,'&Acirc;','Â') ; -- capital a, circumflex accent UPDATE files SET html=REPLACE(html,'&Atilde;','Ã') ; -- capital a, tilde UPDATE files SET html=REPLACE(html,'&Auml;','Ä') ; -- capital a, umlaut mark UPDATE files SET html=REPLACE(html,'&Aring;','Å') ; -- capital a, ring UPDATE files SET html=REPLACE(html,'&AElig;','Æ') ; -- capital ae UPDATE files SET html=REPLACE(html,'&Ccedil;','Ç') ; -- capital c, cedilla UPDATE files SET html=REPLACE(html,'&Egrave;','È') ; -- capital e, grave accent UPDATE files SET html=REPLACE(html,'&Eacute;','É') ; -- capital e, acute accent UPDATE files SET html=REPLACE(html,'&Ecirc;','Ê') ; -- capital e, circumflex accent UPDATE files SET html=REPLACE(html,'&Euml;','Ë') ; -- capital e, umlaut mark UPDATE files SET html=REPLACE(html,'&Igrave;','Ì') ; -- capital i, grave accent UPDATE files SET html=REPLACE(html,'&Iacute;','Í') ; -- capital i, acute accent UPDATE files SET html=REPLACE(html,'&Icirc;','Î') ; -- capital i, circumflex accent UPDATE files SET html=REPLACE(html,'&Iuml;','Ï') ; -- capital i, umlaut mark UPDATE files SET html=REPLACE(html,'&ETH;','Ð') ; -- capital eth, Icelandic UPDATE files SET html=REPLACE(html,'&Ntilde;','Ñ') ; -- capital n, tilde UPDATE files SET html=REPLACE(html,'&Ograve;','Ò') ; -- capital o, grave accent UPDATE files SET html=REPLACE(html,'&Oacute;','Ó') ; -- capital o, acute accent UPDATE files SET html=REPLACE(html,'&Ocirc;','Ô') ; -- capital o, circumflex accent UPDATE files SET html=REPLACE(html,'&Otilde;','Õ') ; -- capital o, tilde UPDATE files SET html=REPLACE(html,'&Ouml;','Ö') ; -- capital o, umlaut mark UPDATE files SET html=REPLACE(html,'&Oslash;','Ø') ; -- capital o, slash UPDATE files SET html=REPLACE(html,'&Ugrave;','Ù') ; -- capital u, grave accent UPDATE files SET html=REPLACE(html,'&Uacute;','Ú') ; -- capital u, acute accent UPDATE files SET html=REPLACE(html,'&Ucirc;','Û') ; -- capital u, circumflex accent UPDATE files SET html=REPLACE(html,'&Uuml;','Ü') ; -- capital u, umlaut mark UPDATE files SET html=REPLACE(html,'&Yacute;','Ý') ; -- capital y, acute accent UPDATE files SET html=REPLACE(html,'&THORN;','Þ') ; -- capital THORN, Icelandic UPDATE files SET html=REPLACE(html,'&szlig;','ß') ; -- small sharp s, German UPDATE files SET html=REPLACE(html,'&agrave;','à') ; -- small a, grave accent UPDATE files SET html=REPLACE(html,'&aacute;','á') ; -- small a, acute accent UPDATE files SET html=REPLACE(html,'&acirc;','â') ; -- small a, circumflex accent UPDATE files SET html=REPLACE(html,'&atilde;','ã') ; -- small a, tilde UPDATE files SET html=REPLACE(html,'&auml;','ä') ; -- small a, umlaut mark UPDATE files SET html=REPLACE(html,'&aring;','å') ; -- small a, ring UPDATE files SET html=REPLACE(html,'&aelig;','æ') ; -- small ae UPDATE files SET html=REPLACE(html,'&ccedil;','ç') ; -- small c, cedilla UPDATE files SET html=REPLACE(html,'&egrave;','è') ; -- small e, grave accent UPDATE files SET html=REPLACE(html,'&eacute;','é') ; -- small e, acute accent UPDATE files SET html=REPLACE(html,'&ecirc;','ê') ; -- small e, circumflex accent UPDATE files SET html=REPLACE(html,'&euml;','ë') ; -- small e, umlaut mark UPDATE files SET html=REPLACE(html,'&igrave;','ì') ; -- small i, grave accent UPDATE files SET html=REPLACE(html,'&iacute;','í') ; -- small i, acute accent UPDATE files SET html=REPLACE(html,'&icirc;','î') ; -- small i, circumflex accent UPDATE files SET html=REPLACE(html,'&iuml;','ï') ; -- small i, umlaut mark UPDATE files SET html=REPLACE(html,'&eth;','ð') ; -- small eth, Icelandic UPDATE files SET html=REPLACE(html,'&ntilde;','ñ') ; -- small n, tilde UPDATE files SET html=REPLACE(html,'&ograve;','ò') ; -- small o, grave accent UPDATE files SET html=REPLACE(html,'&oacute;','ó') ; -- small o, acute accent UPDATE files SET html=REPLACE(html,'&ocirc;','ô') ; -- small o, circumflex accent UPDATE files SET html=REPLACE(html,'&otilde;','õ') ; -- small o, tilde UPDATE files SET html=REPLACE(html,'&ouml;','ö') ; -- small o, umlaut mark UPDATE files SET html=REPLACE(html,'&oslash;','ø') ; -- small o, slash UPDATE files SET html=REPLACE(html,'&ugrave;','ù') ; -- small u, grave accent UPDATE files SET html=REPLACE(html,'&uacute;','ú') ; -- small u, acute accent UPDATE files SET html=REPLACE(html,'&ucirc;','û') ; -- small u, circumflex accent UPDATE files SET html=REPLACE(html,'&uuml;','ü') ; -- small u, umlaut mark UPDATE files SET html=REPLACE(html,'&yacute;','ý') ; -- small y, acute accent UPDATE files SET html=REPLACE(html,'&thorn;','þ') ; -- small thorn, Icelandic UPDATE files SET html=REPLACE(html,'&yuml;','ÿ') ; -- small y, umlaut mark
Convert UTF entities
UPDATE files SET html=REPLACE(html,'&#192;','À') ; -- capital a, grave accent UPDATE files SET html=REPLACE(html,'&#193;','Á') ; -- capital a, acute accent UPDATE files SET html=REPLACE(html,'&#194;','Â') ; -- capital a, circumflex accent UPDATE files SET html=REPLACE(html,'&#195;','Ã') ; -- capital a, tilde UPDATE files SET html=REPLACE(html,'&#196;','Ä') ; -- capital a, umlaut mark UPDATE files SET html=REPLACE(html,'&#197;','Å') ; -- capital a, ring UPDATE files SET html=REPLACE(html,'&#198;','Æ') ; -- capital ae UPDATE files SET html=REPLACE(html,'&#199;','Ç') ; -- capital c, cedilla UPDATE files SET html=REPLACE(html,'&#200;','È') ; -- capital e, grave accent UPDATE files SET html=REPLACE(html,'&#201;','É') ; -- capital e, acute accent UPDATE files SET html=REPLACE(html,'&#202;','Ê') ; -- capital e, circumflex accent UPDATE files SET html=REPLACE(html,'&#203;','Ë') ; -- capital e, umlaut mark UPDATE files SET html=REPLACE(html,'&#204;','Ì') ; -- capital i, grave accent UPDATE files SET html=REPLACE(html,'&#205;','Í') ; -- capital i, acute accent UPDATE files SET html=REPLACE(html,'&#206;','Î') ; -- capital i, circumflex accent UPDATE files SET html=REPLACE(html,'&#207;','Ï') ; -- capital i, umlaut mark UPDATE files SET html=REPLACE(html,'&#208;','Ð') ; -- capital eth, Icelandic UPDATE files SET html=REPLACE(html,'&#209;','Ñ') ; -- capital n, tilde UPDATE files SET html=REPLACE(html,'&#210;','Ò') ; -- capital o, grave accent UPDATE files SET html=REPLACE(html,'&#211;','Ó') ; -- capital o, acute accent UPDATE files SET html=REPLACE(html,'&#212;','Ô') ; -- capital o, circumflex accent UPDATE files SET html=REPLACE(html,'&#213;','Õ') ; -- capital o, tilde UPDATE files SET html=REPLACE(html,'&#214;','Ö') ; -- capital o, umlaut mark UPDATE files SET html=REPLACE(html,'&#216;','Ø') ; -- capital o, slash UPDATE files SET html=REPLACE(html,'&#217;','Ù') ; -- capital u, grave accent UPDATE files SET html=REPLACE(html,'&#218;','Ú') ; -- capital u, acute accent UPDATE files SET html=REPLACE(html,'&#219;','Û') ; -- capital u, circumflex accent UPDATE files SET html=REPLACE(html,'&#220;','Ü') ; -- capital u, umlaut mark UPDATE files SET html=REPLACE(html,'&#221;','Ý') ; -- capital y, acute accent UPDATE files SET html=REPLACE(html,'&#222;','Þ') ; -- capital THORN, Icelandic UPDATE files SET html=REPLACE(html,'&#223;','ß') ; -- small sharp s, German UPDATE files SET html=REPLACE(html,'&#224;','à') ; -- small a, grave accent UPDATE files SET html=REPLACE(html,'&#225;','á') ; -- small a, acute accent UPDATE files SET html=REPLACE(html,'&#226;','â') ; -- small a, circumflex accent UPDATE files SET html=REPLACE(html,'&#227;','ã') ; -- small a, tilde UPDATE files SET html=REPLACE(html,'&#228;','ä') ; -- small a, umlaut mark UPDATE files SET html=REPLACE(html,'&#229;','å') ; -- small a, ring UPDATE files SET html=REPLACE(html,'&#230;','æ') ; -- small ae UPDATE files SET html=REPLACE(html,'&#231;','ç') ; -- small c, cedilla UPDATE files SET html=REPLACE(html,'&#232;','è') ; -- small e, grave accent UPDATE files SET html=REPLACE(html,'&#233;','é') ; -- small e, acute accent UPDATE files SET html=REPLACE(html,'&#234;','ê') ; -- small e, circumflex accent UPDATE files SET html=REPLACE(html,'&#235;','ë') ; -- small e, umlaut mark UPDATE files SET html=REPLACE(html,'&#236;','ì') ; -- small i, grave accent UPDATE files SET html=REPLACE(html,'&#237;','í') ; -- small i, acute accent UPDATE files SET html=REPLACE(html,'&#238;','î') ; -- small i, circumflex accent UPDATE files SET html=REPLACE(html,'&#239;','ï') ; -- small i, umlaut mark UPDATE files SET html=REPLACE(html,'&#240;','ð') ; -- small eth, Icelandic UPDATE files SET html=REPLACE(html,'&#241;','ñ') ; -- small n, tilde UPDATE files SET html=REPLACE(html,'&#242;','ò') ; -- small o, grave accent UPDATE files SET html=REPLACE(html,'&#243;','ó') ; -- small o, acute accent UPDATE files SET html=REPLACE(html,'&#244;','ô') ; -- small o, circumflex accent UPDATE files SET html=REPLACE(html,'&#245;','õ') ; -- small o, tilde UPDATE files SET html=REPLACE(html,'&#246;','ö') ; -- small o, umlaut mark UPDATE files SET html=REPLACE(html,'&#248;','ø') ; -- small o, slash UPDATE files SET html=REPLACE(html,'&#249;','ù') ; -- small u, grave accent UPDATE files SET html=REPLACE(html,'&#250;','ú') ; -- small u, acute accent UPDATE files SET html=REPLACE(html,'&#251;','û') ; -- small u, circumflex accent UPDATE files SET html=REPLACE(html,'&#252;','ü') ; -- small u, umlaut mark UPDATE files SET html=REPLACE(html,'&#253;','ý') ; -- small y, acute accent UPDATE files SET html=REPLACE(html,'&#254;','þ') ; -- small thorn, Icelandic UPDATE files SET html=REPLACE(html,'&#255;','ÿ') ; -- small y, umlaut mark
[rediger] CREATE TABLE SELECT with table definitions
If you run a CREATE TABLE SELECT, column data types will be "guessed" by the query parser according to values in the originating columns, and resulting tables may be extreamly slow since no indexes are created. This may be solved by adding a table definitions at run time. Here's an example:
DROP TABLE IF EXISTS test ; CREATE TABLE test ( grepxml varchar(255) NOT NULL, KEY grepxml (grepxml) ) SELECT topicID AS grepxml FROM g_occurrence ;
[rediger] Export View definitions
-- reformat all views made unreadable by the mysql precompiler -- configuration options: change WHERE clause to limit to one database SELECT CONCAT('-- ', TABLE_NAME, '\n\n', REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( VIEW_DEFINITION, 'select','SELECT\n\t'), 'from ','\nFROM '), 'on(',' ON('), 'in (\'','\nIN ('), 'left join ','\nLEFT JOIN '), '`,`',',\n\t'), '`','') ) AS viewdefinitions FROM information_schema.VIEWS -- WHERE TABLE_NAME LIKE '%grep%' ;
[rediger] Ranking
-- object_count = column containing values to be ranked according to. -- word = column w label or similar SET @ranking := 0, @previousval:=NULL; SELECT @ranking := IF(@ranking=@previousval,@ranking,@ranking+1) AS ranking, @previousval := object_count AS object_count, word FROM `h_utdnoordfrekvens` ORDER BY object_count DESC ;
-- give some permissions to all tables in the database apachelogs CREATE USER 'username'@'localhost' IDENTIFIED BY 'secret'; GRANT SELECT, INSERT, UPDATE ON apachelogs.* TO 'logger'@'localhost' IDENTIFIED BY 'secret' ;
Change length of of GROUP_CONCAT results. Default VARCHAR
SET GLOBAL group_concat_max_len = 2000;

