0% found this document useful (0 votes)
21 views7 pages

WDI2

WDI2 pls

Uploaded by

kuba kwiatkowski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

WDI2

WDI2 pls

Uploaded by

kuba kwiatkowski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

© Jerzy Nawrocki, Introduction to Computing

Introduction to Computing Introduction to Computing

CREATE TABLE SQL – Basic operations

CREATE an empty table with a given set of columns


INSERT a row into a given table
Workers SELECT (read) data from table(s)
FName SName
Basic data types: UPDATE some rows of a given table
VARCHAR (n) DELETE some rows of a given table
CHAR (n)
INT
NUMERIC (n.f)
Databases & ML (25) Databases & ML (26)

Introduction to Computing Introduction to Computing

INSERT SQL – Basic operations

CREATE an empty table with a given set of columns


INSERT a row into a given table
Workers SELECT (read) data from table(s)
FName SName UPDATE some rows of a given table
Olaf Ciszak
DELETE some rows of a given table
Piotr Miklosik
Jerzy Nawrocki
Jerzy Suchanek
Databases & ML (27) Databases & ML (28)

Introduction to Computing Introduction to Computing

Edgar Frank Codd Edgar Frank Codd

Basic variants of SELECT: Basic variants of SELECT:


• projection, • projection,
• selection, • selection,
• join • join

Databases & ML (29) Databases & ML (30)

Databases & ML 5
© Jerzy Nawrocki, Introduction to Computing

Riddle
Introduction to Computing Introduction to Computing

Result

How to get all the above people but deans?


Databases & ML (55) Databases & ML (56)

Introduction to Computing Introduction to Computing

Solution

Useful functions

Databases & ML (57) Databases & ML (58)

Introduction to Computing Introduction to Computing

Aggregation functions Aggregation functions


Workers Workers
Key Hours SName Key Hours SName
20 170 Ciszak 20 170 Ciszak
21 90 Miklosik 21 90 Miklosik
22 140 Nawrocki 22 140 Nawrocki
23 160 Pacholski 23 160 Pacholski
SELECT COUNT(SName) SELECT COUNT(*)
FROM Workers; FROM Workers;
count count
------- -------
4 4

Databases & ML (59) Databases & ML (60)

Databases & ML 10
© Jerzy Nawrocki, Introduction to Computing

Introduction to Computing Introduction to Computing

Updating data SQL – Basic operations


Workers
Key FName SName
20 Konrad Ciszak
Jerzy, not Konrad 21 Piotr Miklosik
CREATE an empty table with a given set of columns
UPDATE Workers 22 Jerzy Nawrocki
INSERT a row into a given table
SET FName= ’Jerzy’ 23 Leszek Pacholski
WHERE SName= ‘Nawrocki’; SELECT (read) data from table(s)
UPDATE some rows of a given table
DELETE some rows of a given table

Databases & ML (67) Databases & ML (68)

Introduction to Computing Introduction to Computing

Deleting data Agenda


Workers
Key FName SName
20 Konrad Ciszak
21 Piotr Miklosik • Relational databases
22 Konrad Nawrocki
• Basics of SQL
23 Leszek Pacholski
• Advanced features of SQL
DELETE FROM Workers
WHERE FName= ‘Konrad’; • Python interface to SQLite3
• Machine learning & ID3
Key FName SName
21 Piotr Miklosik
23 Leszek Pacholski
Databases & ML (69) Databases & ML (70)

Introduction to Computing Introduction to Computing

Distinct items
Workers
Key FName SName
20 Konrad Ciszak
First names of the workers? 21 Piotr Miklosik
22 Konrad Nawrocki
23 Leszek Pacholski
SELECT FName
FROM Workers FName
ORDER BY FName ASC; Konrad
Konrad
Group of rows:
the same value Leszek
of a given attrib. Piotr
Databases & ML (71) Databases & ML (72)

Databases & ML 12
© Jerzy Nawrocki, Introduction to Computing

Introduction to Computing Introduction to Computing

Distinct items Patterns


Workers Workers
Key FName SName Key FName SName
20 Konrad Ciszak 20 Konrad Ciszak
First names of the workers? 21 Piotr Miklosik 21 Piotr Miklosik
22 Konrad Nawrocki 22 Konrad Nawrocki
23 Leszek Pacholski 23 Leszek Pacholski
SELECT DISTINCT FName SELECT FName, SName
FROM Workers FROM Workers
ORDER BY FName ASC; FName WHERE SName LIKE ‘%ki’;
FName SName
Konrad
Konrad Nawrocki
Leszek
Leszek Pacholski
Piotr

Databases & ML (73) Databases & ML (74)

Introduction to Computing Introduction to Computing

Patterns Storing results in a table


Workers Workers
Key FName SName Key FName SName
20 Konrad Ciszak First names of the workers 20 Konrad Ciszak
21 Piotr Miklosik CREATE TABLE WorkNams 21 Piotr Miklosik
22 Konrad Nawrocki ( 22 Konrad Nawrocki
23 Leszek Pacholski Name VARCHAR(40), 23 Leszek Pacholski
SELECT FName, SName );
FROM Workers WorkNams
WHERE SName LIKE ‘_a%’; INSERT INTO WorkNams Name
FName SName
SELECT DISTINCT FName Konrad
Konrad Nawrocki
FROM Workers Leszek
Leszek Pacholski
ORDER BY FName ASC; Piotr

Databases & ML (75) Databases & ML (76)

Introduction to Computing Introduction to Computing

Nested queries Agenda


Workers
Key Hours SName
20 170 Ciszak
21 90 Miklosik • Relational databases
22 140 Nawrocki
• Basics of SQL
23 160 Pacholski
• Advanced features of SQL
SELECT * FROM Workers • Python interface to SQLite3
WHERE Hours > (SELECT AVG(Hours) FROM Workers);
• Machine learning & ID3
Key Hours SName
20 170 Ciszak
23 160 Pacholski
Databases & ML (77) Databases & ML (78)

Databases & ML 13
© Jerzy Nawrocki, Introduction to Computing

Introduction to Computing Introduction to Computing

Mute vs. talkative statements Mute vs. talkative statements

CREATE an empty table with a given set of columns CREATE an empty table with a given set of columns
INSERT a row into a given table INSERT a row into a given table
UPDATE some rows of a given table UPDATE some rows of a given table
DELETE some rows of a given table DELETE some rows of a given table

SELECT (read) data from table(s) SELECT (read) data from table(s)

Databases & ML (79) Databases & ML (80)

Introduction to Computing Introduction to Computing

Python interface to SQLite Example 1 (mute statements only)

import sqlite3
db = sqlite3.connect('fileName')
c = db.cursor()
c.execute("SQLstatement")
db.commit()
Workers
db.close() FName SName
Olaf Ciszak
Piotr Miklosik

Databases & ML (81) Databases & ML (82)

Introduction to Computing Introduction to Computing

Using 'mute' statements Talkative statements

import sqlite3
db = sqlite3.connect('my.db') CREATE an empty table with a given set of columns
c = db.cursor()
c.execute('''create table Workers INSERT a row into a given table
(FName VARCHAR(40),
SName VARCHAR(40) UPDATE some rows of a given table
);''')
c.execute("insert into Workers values('Olaf', 'Ciszak' );") DELETE some rows of a given table
c.execute("insert into Workers values('Piotr','Miklosik');")
db.commit()
db.close()
SELECT (read) data from table(s)

Databases & ML (83) Databases & ML (84)

Databases & ML 14
© Jerzy Nawrocki, Introduction to Computing

Introduction to Computing Introduction to Computing

Using 'talkative' statements Using 'talkative' statements


import sqlite3 import sqlite3
db = sqlite3.connect('my.db') db = sqlite3.connect('my.db')
c = db.cursor() c = db.cursor()
c.execute('''create table Workers c.execute('''create table Workers
(FName VARCHAR(40), (FName VARCHAR(40),
SName VARCHAR(40) SName VARCHAR(40)
);''') );''')
c.execute("insert into Workers values('Olaf', 'Ciszak' );") c.execute("insert into Workers values('Olaf', 'Ciszak' );")
c.execute("insert into Workers values('Piotr','Miklosik');") c.execute("insert into Workers values('Piotr','Miklosik');")
db.commit() db.commit()
for row in c.execute("select SName from Workers;"): for row in c.execute("select SName from Workers;"):
print(row) print(row[0])
db.close() db.close()

Databases & ML (85) Databases & ML (86)

Introduction to Computing Introduction to Computing

The three basic functions Database pointer

#include <sqlite3.h>
C #include <sqlite3.h>
C
... ...
sqlite3 *db; sqlite3 *db;
... ...
Err= sqlite3_open(fileName, &db); Err= sqlite3_open(fileName, &db);
... ...
Err= sqlite3_exec(db, query, NULL, NULL, NULL); Err= sqlite3_exec(db, query, NULL, NULL, NULL);
... ...
Err= sqlite3_close(db); Err= sqlite3_close(db);

Databases & ML (87) Databases & ML (88)

Introduction to Computing Introduction to Computing

Return codes (0 = OK) Executing mute statements

#include <sqlite3.h>
C #include <sqlite3.h>
C
... ...
sqlite3 *db; sqlite3 *db;
... ...
Err= sqlite3_open(fileName, &db); Err= sqlite3_open(fileName, &db);
... ...
Err= sqlite3_exec(db, query, NULL, NULL, NULL); Err= sqlite3_exec(db, query, NULL, NULL, NULL);
... ...
Err= sqlite3_close(db); Err= sqlite3_close(db);

Databases & ML (89) Databases & ML (90)

Databases & ML 15
© Jerzy Nawrocki, Introduction to Computing

Introduction to Computing Introduction to Computing

ID3 – General idea ID3 – General idea

Training data Training data


1. If the data are unanimous

?
then STOP.
2. If not: Which column to
choose?
3. What are the values in the
column?
4. Divide the table into
subtables by those values.
5. Repeat the procedure for
each subtable.

https://automaticaddison.com/iterative-dichotomiser-3-id3-algorithm-from-scratch/ https://automaticaddison.com/iterative-dichotomiser-3-id3-algorithm-from-scratch/
Databases & ML (103) Databases & ML (104)

Introduction to Computing Introduction to Computing

ID3 – General idea ID3 – General idea

Training data
1. If the data are unanimous 1. If the data are unanimous
then STOP. then STOP.
2. If not: Which column to 2. If not: Which column to
choose? choose?
3. What are the values in the 3. What are the values in the
column? column?
4. Divide the table into 4. Divide the table into
subtables by those values. subtables by those values.
5. Repeat the procedure for 5. Repeat the procedure for
each subtable. each subtable.

https://automaticaddison.com/iterative-dichotomiser-3-id3-algorithm-from-scratch/ https://automaticaddison.com/iterative-dichotomiser-3-id3-algorithm-from-scratch/
Databases & ML (105) Databases & ML (106)

Introduction to Computing Introduction to Computing

ID3 – General idea ID3 – General idea


Overcast Overcast
Rain Rain
Sunny Sunny

1. If the data are unanimous 1. If the data are unanimous


then STOP. then STOP.
2. If not: Which column to 2. If not: Which column to
choose? choose?
3. What are the values in the 3. What are the values in the
column? column?
4. Divide the table into 4. Divide the table into
subtables by those values. subtables by those values.
5. Repeat the procedure for 5. Repeat the procedure for
each subtable. each subtable.

https://automaticaddison.com/iterative-dichotomiser-3-id3-algorithm-from-scratch/ https://automaticaddison.com/iterative-dichotomiser-3-id3-algorithm-from-scratch/
Databases & ML (107) Databases & ML (108)

Databases & ML 18

You might also like