SQL scratchpad

Fra Harald Grovens wiki

Gå til: navigasjon, søk

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,'&amp;Agrave;','À') ;  	-- capital a, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;Aacute;','Á') ;  	-- capital a, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;Acirc;','Â') ;  	-- capital a, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;Atilde;','Ã') ;  	-- capital a, tilde 
UPDATE files SET html=REPLACE(html,'&amp;Auml;','Ä') ;  	-- capital a, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;Aring;','Å') ;  	-- capital a, ring 
UPDATE files SET html=REPLACE(html,'&amp;AElig;','Æ') ;  	-- capital ae 
UPDATE files SET html=REPLACE(html,'&amp;Ccedil;','Ç') ;  	-- capital c, cedilla 
UPDATE files SET html=REPLACE(html,'&amp;Egrave;','È') ;  	-- capital e, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;Eacute;','É') ;  	-- capital e, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;Ecirc;','Ê') ;  	-- capital e, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;Euml;','Ë') ;  	-- capital e, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;Igrave;','Ì') ;  	-- capital i, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;Iacute;','Í') ;  	-- capital i, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;Icirc;','Î') ;  	-- capital i, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;Iuml;','Ï') ;  	-- capital i, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;ETH;','Ð') ;  	-- capital eth, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;Ntilde;','Ñ') ;  	-- capital n, tilde 
UPDATE files SET html=REPLACE(html,'&amp;Ograve;','Ò') ;  	-- capital o, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;Oacute;','Ó') ;  	-- capital o, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;Ocirc;','Ô') ;  	-- capital o, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;Otilde;','Õ') ;  	-- capital o, tilde 
UPDATE files SET html=REPLACE(html,'&amp;Ouml;','Ö') ;  	-- capital o, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;Oslash;','Ø') ;  	-- capital o, slash 
UPDATE files SET html=REPLACE(html,'&amp;Ugrave;','Ù') ;  	-- capital u, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;Uacute;','Ú') ;  	-- capital u, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;Ucirc;','Û') ;  	-- capital u, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;Uuml;','Ü') ;  	-- capital u, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;Yacute;','Ý') ;  	-- capital y, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;THORN;','Þ') ;  	-- capital THORN, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;szlig;','ß') ;  	-- small sharp s, German 
UPDATE files SET html=REPLACE(html,'&amp;agrave;','à') ;  	-- small a, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;aacute;','á') ;  	-- small a, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;acirc;','â') ;  	-- small a, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;atilde;','ã') ;  	-- small a, tilde 
UPDATE files SET html=REPLACE(html,'&amp;auml;','ä') ;  	-- small a, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;aring;','å') ;  	-- small a, ring 
UPDATE files SET html=REPLACE(html,'&amp;aelig;','æ') ;  	-- small ae 
UPDATE files SET html=REPLACE(html,'&amp;ccedil;','ç') ;  	-- small c, cedilla 
UPDATE files SET html=REPLACE(html,'&amp;egrave;','è') ;  	-- small e, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;eacute;','é') ;  	-- small e, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;ecirc;','ê') ;  	-- small e, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;euml;','ë') ;  	-- small e, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;igrave;','ì') ;  	-- small i, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;iacute;','í') ;  	-- small i, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;icirc;','î') ;  	-- small i, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;iuml;','ï') ;  	-- small i, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;eth;','ð') ;  	-- small eth, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;ntilde;','ñ') ;  	-- small n, tilde 
UPDATE files SET html=REPLACE(html,'&amp;ograve;','ò') ;  	-- small o, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;oacute;','ó') ;  	-- small o, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;ocirc;','ô') ;  	-- small o, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;otilde;','õ') ;  	-- small o, tilde 
UPDATE files SET html=REPLACE(html,'&amp;ouml;','ö') ;  	-- small o, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;oslash;','ø') ;  	-- small o, slash 
UPDATE files SET html=REPLACE(html,'&amp;ugrave;','ù') ;  	-- small u, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;uacute;','ú') ;  	-- small u, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;ucirc;','û') ;  	-- small u, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;uuml;','ü') ;  	-- small u, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;yacute;','ý') ;  	-- small y, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;thorn;','þ') ;  	-- small thorn, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;yuml;','ÿ') ;  	-- small y, umlaut mark

Convert UTF entities

UPDATE files SET html=REPLACE(html,'&amp;#192;','À') ;  -- capital a, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#193;','Á') ;  -- capital a, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#194;','Â') ;  -- capital a, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#195;','Ã') ;  -- capital a, tilde 
UPDATE files SET html=REPLACE(html,'&amp;#196;','Ä') ;  -- capital a, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#197;','Å') ;  -- capital a, ring 
UPDATE files SET html=REPLACE(html,'&amp;#198;','Æ') ;  -- capital ae 
UPDATE files SET html=REPLACE(html,'&amp;#199;','Ç') ;  -- capital c, cedilla 
UPDATE files SET html=REPLACE(html,'&amp;#200;','È') ;  -- capital e, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#201;','É') ;  -- capital e, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#202;','Ê') ;  -- capital e, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#203;','Ë') ;  -- capital e, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#204;','Ì') ;  -- capital i, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#205;','Í') ;  -- capital i, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#206;','Î') ;  -- capital i, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#207;','Ï') ;  -- capital i, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#208;','Ð') ;  -- capital eth, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;#209;','Ñ') ;  -- capital n, tilde 
UPDATE files SET html=REPLACE(html,'&amp;#210;','Ò') ;  -- capital o, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#211;','Ó') ;  -- capital o, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#212;','Ô') ;  -- capital o, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#213;','Õ') ;  -- capital o, tilde 
UPDATE files SET html=REPLACE(html,'&amp;#214;','Ö') ;  -- capital o, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#216;','Ø') ;  -- capital o, slash 
UPDATE files SET html=REPLACE(html,'&amp;#217;','Ù') ;  -- capital u, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#218;','Ú') ;  -- capital u, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#219;','Û') ;  -- capital u, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#220;','Ü') ;  -- capital u, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#221;','Ý') ;  -- capital y, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#222;','Þ') ;  -- capital THORN, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;#223;','ß') ;  -- small sharp s, German 
UPDATE files SET html=REPLACE(html,'&amp;#224;','à') ;  -- small a, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#225;','á') ;  -- small a, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#226;','â') ;  -- small a, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#227;','ã') ;  -- small a, tilde 
UPDATE files SET html=REPLACE(html,'&amp;#228;','ä') ;  -- small a, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#229;','å') ;  -- small a, ring 
UPDATE files SET html=REPLACE(html,'&amp;#230;','æ') ;  -- small ae 
UPDATE files SET html=REPLACE(html,'&amp;#231;','ç') ;  -- small c, cedilla 
UPDATE files SET html=REPLACE(html,'&amp;#232;','è') ;  -- small e, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#233;','é') ;  -- small e, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#234;','ê') ;  -- small e, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#235;','ë') ;  -- small e, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#236;','ì') ;  -- small i, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#237;','í') ;  -- small i, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#238;','î') ;  -- small i, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#239;','ï') ;  -- small i, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#240;','ð') ;  -- small eth, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;#241;','ñ') ;  -- small n, tilde 
UPDATE files SET html=REPLACE(html,'&amp;#242;','ò') ;  -- small o, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#243;','ó') ;  -- small o, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#244;','ô') ;  -- small o, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#245;','õ') ;  -- small o, tilde 
UPDATE files SET html=REPLACE(html,'&amp;#246;','ö') ;  -- small o, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#248;','ø') ;  -- small o, slash 
UPDATE files SET html=REPLACE(html,'&amp;#249;','ù') ;  -- small u, grave accent 
UPDATE files SET html=REPLACE(html,'&amp;#250;','ú') ;  -- small u, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#251;','û') ;  -- small u, circumflex accent 
UPDATE files SET html=REPLACE(html,'&amp;#252;','ü') ;  -- small u, umlaut mark 
UPDATE files SET html=REPLACE(html,'&amp;#253;','ý') ;  -- small y, acute accent 
UPDATE files SET html=REPLACE(html,'&amp;#254;','þ') ;  -- small thorn, Icelandic 
UPDATE files SET html=REPLACE(html,'&amp;#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;