Archive for the ‘SQL’ Category.

Zugriff aus dem lokalen Netz auf die Nordwind-DB

Um aus dem lokalen Netz Zugriff auf die Nordwind-DB zu erhalten, muss man folgendes tun:

  • die Datei 50-server.cnf in /etc/mysql/mariadb.conf.d bearbeiten. Hier statt 127.0.0.1 die IP-Adresse des Servers eingeben. An dieses Interface wird MariaDB gebunden.
  • einen neuen Nutzer anlegen:

    CREATE USER 'nwread' IDENTIFIED BY 'northwind';

  • Zugriff aus dem Netz für diesen User erteilen:

    GRANT USAGE ON *.* TO 'nwread'@'%' IDENTIFIED BY 'northwind';

    Kann sein, dass dieses Statement schon ausreichend ist, das werde ich noch überprüfen. Ich habe aber zusätzlich noch ausgeführt:

  • Zugriff auf die Datenbank geben:

    GRANT ALL privileges ON `northwind`.* TO 'nwread'@'%';

Nach dem Neustarts des MariaDB-Servers klappt der Zugriff, beispielsweise über HeidiSQL, das ich sehr empfehlen kann.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Die Nordwind-Datenbank für MySQL nutzen

Mit der Nordwind (Northwind) Datenbank gibt es eine ursprünglich für MS Access entwickelte Datenbank, an der sich viele SQL-Konstrukte üben lassen. Die Datenbank gibt es auch für MySQL, unter anderem unter https://github.com/dalers/mywind.

Hier nun die notwendigen Schritte für das Einspielen in eine MySQL/MariaDB-Datenbank:

  • git clone https://github.com/dalers/mywind.git
  • cd mywind
  • sudo mariadb < northwind.sql
  • sudo mariadb < northwind-data.sql

Mittels SQL kann man jetzt prüfen, ob die Daten vollständig geladen wurden:

  • sudo mariadb
  • use northwind
  • select count(*) from products;

bringt eine Zeilenzahl von 45 Zeilen.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

MariaDB installieren unter Linux

Für Apache Guacamole brauchte ich einen MySQL/MariaDB Server auf meinem Linux-Server, hier die Schritte zur Installation:

  1. sudo apt update
  2. sudo apt upgrade
  3. sudo apt install mariadb-server
  4. sudo mariadb

Standardmäßig kommt man als root sofort in die Datenbank, ohne ein Passwort einzugeben. Ich bin zwar den Hinweisen von https://kofler.info/root-login-problem-mit-mariadb/ gefolgt, um für den root-User ebenfalls ein Passwort festzulegen.

Aktuell wird aber jedes Passwort akzeptiert, wenn ich root bin, jedoch das festgelegte Passwort nicht akzeptiert, wenn ich als normaler User mysql -u root -p ausführe.

Sinnvolle mysql/mariadb-Befehle

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Retrieve MySQL/MariaDB data with Python

A while ago I had some issues retrieving data from MySQL using Python, as my Python was more up-to-date than the official bindings. A few days ago I found a nice GIST (https://gist.github.com/stefanthoss/364b2a99521d5bb76d51) that uses pymysql:

import pandas as pd
import pymysql
from sqlalchemy import create_engine
 
engine = create_engine('mysql+pymysql://<user>:<password>@<host>[:<port>]/<dbname>')
df = pd.read_sql_query('SELECT * FROM table', engine)
df.head()

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Graphviz Dateien mit Autopreview erstellen

Graphviz ist seit Jahrzehnten das Tool, um Graphen zu erstellen. Für Visual Studio Code gibt es mit dem „Graphviz Preview“ Plugin die Möglichlkeit, eine Echtzeit-Preview zu erhalten. Dann fühlt es sich fast an wie Visio 🙂

Hier die Quelle für diesen Tipp: https://spin.atomicobject.com/2017/11/15/table-rel-diagrams-graphviz/

Tipp: Graphviz muss natürlich installiert und im Pfad sein, die Preview aktiviert man dann in VSC über -P ==> „Graphviz: Show Preview“.

Graphviz Preview in VSC

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

SQL: Splitting a Bitmask into separate columns

Today I needed to split an integer (representing a bitmask) into its components, each component in a separate column. The following code from http://www.sqlservercentral.com/Forums/Topic1101943-392-1.aspx did the trick.

SELECT
   N,
   SIGN(N & 1) AS Bit1,
   SIGN(N & 2) AS Bit2,
   SIGN(N & 4) AS Bit3,
   SIGN(N & 8) AS Bit4,
   SIGN(N & 16) AS Bit5,
   SIGN(N & 32) AS Bit6,
   SIGN(N & 64) AS Bit7,
   SIGN(N & 128) AS Bit8
FROM (
   SELECT 511
) TestData(N)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Beispiel für eine MySQL Funktion

Ich bereite momentan die Spendenquittungen der Dingfabrik mit Python, Jinja2, MySQL und LaTeX auf. Um den SQL Code sauber zu halten, lagere ich einiges aus dem Python-Code in MySQL Funktionen aus.

Hier ein einfaches Beispiel, das basierend auf Parametern für Mitglieds-ID und Namenstyp entweder der Vornamen, Nachnamen oder kompletten Namen eines Mitglieds zurückgibt.

DROP FUNCTION IF EXISTS fs_getname;
DELIMITER $$
CREATE FUNCTION fs_getname(nummer INT, typ CHAR)
 RETURNS VARCHAR(100)
 NOT DETERMINISTIC
 BEGIN
	IF typ = 'c' THEN
		RETURN (SELECT TRIM(CONCAT(COALESCE(Vorname,"")," ",COALESCE(Name," "))) FROM Stammdaten WHERE  ID = nummer);
	ELSEIF typ = 'f' THEN
		RETURN (SELECT COALESCE(Vorname,"") FROM Stammdaten WHERE  ID = nummer);
	ELSEIF typ = 'l' THEN
		RETURN (SELECT COALESCE(Name,"") FROM Stammdaten WHERE  ID = nummer);		
	END IF;
END$$
DELIMITER ;

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Blobs auf der Festplatte speichern

Hier ein Beispiel, wie man Blobs auf die Festplatte extrahieren kann:

http://blogs.technet.com/b/austria/archive/2010/02/25/wie-bekomme-ich-bin-re-dokumente-blobs-aus-dem-sql-server.aspx

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Kumulative Summe mit T-SQL

Hier ein kurzes Beispiel, wie man mit T-SQL eine kumulative Summe ausrechnet.

CREATE TABLE #test(datum DATE, VALUE INT)
 
INSERT INTO #test VALUES('2010-01-01',10)
INSERT INTO #test VALUES('2010-01-02',20)
INSERT INTO #test VALUES('2010-01-02',30)
 
SELECT t1.*, (SELECT SUM(VALUE) FROM #test t2 WHERE t2.datum<=t1.datum) [cumulative amount]
FROM #test t1
ORDER BY t1.datum
DROP TABLE #test

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

T-SQL: Median und Ausreißer berechnen

Unter http://www.insidesql.org/blogs/frankkalis/2004/07/13/median-berechnen und http://quantmeditate.blogspot.com/2005/03/computing-interquartile-range.html habe ich Informationen zur Berechnung von Median und Quartilen in T-SQL gefunden.

DROP TABLE #data
CREATE TABLE #data(NUMBER FLOAT)
 
INSERT INTO #data VALUES(1.0);INSERT INTO #data VALUES(2.0);
INSERT INTO #data VALUES(3.0);INSERT INTO #data VALUES(4.0);
INSERT INTO #data VALUES(5.0);INSERT INTO #data VALUES(6.0);
INSERT INTO #data VALUES(7.0);INSERT INTO #data VALUES(8.0);
INSERT INTO #data VALUES(9.0);INSERT INTO #data VALUES(10.0);
INSERT INTO #data VALUES(11.0);INSERT INTO #data VALUES(12.0);
INSERT INTO #data VALUES(13.0);INSERT INTO #data VALUES(14.0);
INSERT INTO #data VALUES(15.0);INSERT INTO #data VALUES(16.0);
INSERT INTO #data VALUES(17.0);INSERT INTO #data VALUES(18.0);
INSERT INTO #data VALUES(19.0);INSERT INTO #data VALUES(20.0);
INSERT INTO #data VALUES(21.0);INSERT INTO #data VALUES(22.0);
INSERT INTO #data VALUES(23.0);INSERT INTO #data VALUES(24.0);
INSERT INTO #data VALUES(25.0);INSERT INTO #data VALUES(26.0);
INSERT INTO #data VALUES(27.0);INSERT INTO #data VALUES(28.0);
INSERT INTO #data VALUES(29.0);INSERT INTO #data VALUES(30.0);
INSERT INTO #data VALUES(31.0);INSERT INTO #data VALUES(32.0);
INSERT INTO #data VALUES(33.0);INSERT INTO #data VALUES(34.0);
INSERT INTO #data VALUES(35.0);INSERT INTO #data VALUES(36.0);
INSERT INTO #data VALUES(37.0);INSERT INTO #data VALUES(38.0);
INSERT INTO #data VALUES(39.0);INSERT INTO #data VALUES(40.0);
INSERT INTO #data VALUES(-100.0);INSERT INTO #data VALUES(100.0);
 
DECLARE @median FLOAT
DECLARE @perc25 FLOAT
DECLARE @perc75 FLOAT
DECLARE @iqr FLOAT
 
SET @median = (SELECT   MAX(NUMBER) AS Median FROM (SELECT TOP 50 PERCENT NUMBER FROM #data ORDER BY NUMBER) a)
SET @perc25 = (SELECT   MAX(NUMBER) AS Median FROM (SELECT TOP 25 PERCENT NUMBER FROM #data ORDER BY    NUMBER) a)
SET @perc75 = (SELECT   MAX(NUMBER) AS Median FROM (SELECT TOP 75 PERCENT NUMBER FROM #data ORDER BY    NUMBER) a)
 
print @median
print @perc25
print @perc75
 
SET @iqr = @perc75-@perc25
print 'IQR'
print @iqr
 
SELECT * FROM #data WHERE NUMBER <  (@median + 1.5* @iqr) AND 
NUMBER >  (@median - 1.5* @iqr)
 
print 'number of outliers'
SELECT COUNT(*) FROM #data WHERE NUMBER <  (@median - 1.5* @iqr)
OR NUMBER >  (@median + 1.5* @iqr)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website