Databases
- 2. 1© 2019, LogMeIn, Inc.
Databases
• Basics
• Database types
• SQL
• Indexes
• Views
• Stored procedures
• MongoDB
- 3. 2© 2019, LogMeIn, Inc.
Data
• The data itself doesn’t have any meaning
– 300
– A=25
– Yellow
– [32,12,43,21,55,76,54]
- 4. 3© 2019, LogMeIn, Inc.
Information
• Data with meaning
– Ferenc is 39 years old
– My neighbor has four black dogs
– My grandfather was ten years older than my grandmother.
– A rose is a woody perennial flowering plant of the genus Rosa, in the
family Rosaceae, or the flower it bears.
- 6. 5© 2019, LogMeIn, Inc.
Database management system
The database management system (DBMS) is the software that interacts
with end users, applications, and the database itself to capture and analyze
the data.
- 7. 6© 2019, LogMeIn, Inc.
Relational database system
• A relational database management system (RDBMS) is a database
management system (DBMS) based on the relational model of data.
• The relational model (RM) for database management is an approach to
managing data using a structure and language consistent with first-order
predicate logic, first described in 1969 by English computer scientist Edgar
F. Codd where all data is represented in terms of tuples, grouped into
relations.
- 8. 7© 2019, LogMeIn, Inc.
RDBMS
Id Title Author Year ISBN10
1 Omerta Mario Puzo 2000 0-434-00870-2
2 The Godfather Mario Puzo 1969 0-399-10342-2
3 Birdy William
Wharton
1979 0-679-73412-0
4 Slaughterhouse-
five
Kurt
Vonnegut
1969 0-7910-9295-X
Id Name Year
1 Zoltan Kovacs 1998
2 Maria Vas 2000
3 Lajos Jo 2014
4 Klara Kis 1979
- 9. 8© 2019, LogMeIn, Inc.
Relational Database Management Systems
• MySQL
• PostgreSQL
• SQLServer
• Oracle RDBMS
• DB2
• Amazon Aurora
- 10. 9© 2019, LogMeIn, Inc.
RDBMS’s (Pro)
• SQL is a powerful query language
• There’s a lot’s of relational databases available, with good support
• It’s easy to find a developer with SQL experience
• They’re ACID complaint
– Atomicity
– Consistency
– Isolation
– Durabilty
- 11. 10© 2019, LogMeIn, Inc.
RDBMS (Con)
• The data has to be stored in a structured way, planning needed
• It’s hard to convert between database tables and real objects
• It’s hard to optimize on scaling, they’re mostly vertically scalable
- 12. 11© 2019, LogMeIn, Inc.
Non-relational databases (NoSQL)
Relational databases were never designed to cope with the scale and agility
challenges that face modern applications – and aren't built to take
advantage of cheap storage and processing power that's available today
through the cloud. NoSQL tries to solve these problems.
- 19. 18© 2019, LogMeIn, Inc.
NoSQL Pros
• Flexibile Scalability
• Stores Massive Amounts of data
• Database maintenance could be easier
• Cheaper to implement
- 20. 19© 2019, LogMeIn, Inc.
(Cap Theorem)
• The CAP theorem states that it is impossible for a distributed data store to
simultaneously provide more than two out of the following three
guarantees:
– Consistency: Every read receives the most recent write or an error
– Availability: Every request receives a (non-error) response – without the guarantee
that it contains the most recent write
– Partition tolerance: The system continues to operate despite an arbitrary number of
messages being dropped (or delayed) by the network between nodes
- 21. 20© 2019, LogMeIn, Inc.
NoSQL Cons
• They’re not mature
• Harder to get support
• Business Analytics and Business Intelligence could be harder to
implement
- 22. 21© 2019, LogMeIn, Inc.
SQL
SELECT b.title
FROM friends f
JOIN books b
ON b.year =
f.year
WHERE f.name
= ‘Klara Kis’
Id Title Author Year ISBN1
0
1 Omerta Mario
Puzo
2000 0-434-
00870-
2
2 The
Godfat
her
Mario
Puzo
1969 0-399-
10342-
2
3 Birdy William
Wharto
n
1979 0-679-
73412-
0
4 Slaught
erhous
e-five
Kurt
Vonneg
ut
1969 0-
7910-
9295-x
Id Name Year
1 Zoltan
Kovacs
1998
2 Maria
Vas
2000
3 Lajos Jo 2014
4 Klara Kis 1979
- 23. 22© 2019, LogMeIn, Inc.
DDL - Data definition language
• CREATE
• ALTER
• DROP
• TRUNCATE
- 24. 23© 2019, LogMeIn, Inc.
DQL - Data query language
• SELECT
• FROM
• WHERE
• GROUP BY
• ORDER BY
• HAVING
- 26. 25© 2019, LogMeIn, Inc.
JOIN
• A JOIN clause is used to combine rows from two or more tables, based on
a related column between them.
- 27. 26© 2019, LogMeIn, Inc.
INNER JOIN
SELECT * FROM friends f JOIN books b
+------+---------------+------+------+------------------+-----------------+------+---------------+
| id | Name | Year | id | Title | Author | Year | ISBN10 |
+------+---------------+------+------+------------------+-----------------+------+---------------+
| 1 | Kovacs Zoltan | 1998 | 1 | Omerta | Mario Puzo | 2000 | 0-434-00870-2 |
| 2 | Vas Maria | 2000 | 1 | Omerta | Mario Puzo | 2000 | 0-434-00870-2 |
| 3 | Jo Lajos | 2014 | 1 | Omerta | Mario Puzo | 2000 | 0-434-00870-2 |
| 4 | Kis Klara | 1979 | 1 | Omerta | Mario Puzo | 2000 | 0-434-00870-2 |
| 1 | Kovacs Zoltan | 1998 | 2 | The Godfather | Mario Puzo | 1969 | 0-399-10342-2 |
| 2 | Vas Maria | 2000 | 2 | The Godfather | Mario Puzo | 1969 | 0-399-10342-2 |
| 3 | Jo Lajos | 2014 | 2 | The Godfather | Mario Puzo | 1969 | 0-399-10342-2 |
| 4 | Kis Klara | 1979 | 2 | The Godfather | Mario Puzo | 1969 | 0-399-10342-2 |
| 1 | Kovacs Zoltan | 1998 | 3 | Birdy | William Wharton | 1979 | 0-679-73412-0 |
| 2 | Vas Maria | 2000 | 3 | Birdy | William Wharton | 1979 | 0-679-73412-0 |
| 3 | Jo Lajos | 2014 | 3 | Birdy | William Wharton | 1979 | 0-679-73412-0 |
| 4 | Kis Klara | 1979 | 3 | Birdy | William Wharton | 1979 | 0-679-73412-0 |
| 1 | Kovacs Zoltan | 1998 | 4 | Slaughterhouse-5 | Kurt Vonnegut | 1969 | 0-7910-9295-x |
| 2 | Vas Maria | 2000 | 4 | Slaughterhouse-5 | Kurt Vonnegut | 1969 | 0-7910-9295-x |
| 3 | Jo Lajos | 2014 | 4 | Slaughterhouse-5 | Kurt Vonnegut | 1969 | 0-7910-9295-x |
| 4 | Kis Klara | 1979 | 4 | Slaughterhouse-5 | Kurt Vonnegut | 1969 | 0-7910-9295-x |
+------+---------------+------+------+------------------+-----------------+------+---------------+
- 28. 27© 2019, LogMeIn, Inc.
LEFT (OUTER) JOIN
mysql> SELECT * FROM friends f LEFT JOIN books b ON b.Year = f.year
+------+---------------+------+------+--------+-----------------+------+---------------+
| id | Name | Year | id | Title | Author | Year | ISBN10 |
+------+---------------+------+------+--------+-----------------+------+---------------+
| 2 | Vas Maria | 2000 | 1 | Omerta | Mario Puzo | 2000 | 0-434-00870-2 |
| 4 | Kis Klara | 1979 | 3 | Birdy | William Wharton | 1979 | 0-679-73412-0 |
| 1 | Kovacs Zoltan | 1998 | NULL | NULL | NULL | NULL | NULL |
| 3 | Jo Lajos | 2014 | NULL | NULL | NULL | NULL | NULL |
+------+---------------+------+------+--------+-----------------+------+---------------+
- 29. 28© 2019, LogMeIn, Inc.
RIGHT (OUTER) JOIN
mysql> SELECT * FROM friends f RIGHT JOIN books b ON b.year = f.year
+------+-----------+------+------+------------------+-----------------+------+---------------+
| id | Name | Year | id | Title | Author | Year | ISBN10. |
+------+-----------+------+------+------------------+-----------------+------+---------------+
| 2 | Vas Maria | 2000 | 1 | Omerta | Mario Puzo | 2000 | 0-434-00870-2 |
| 4 | Kis Klara | 1979 | 3 | Birdy | William Wharton | 1979 | 0-679-73412-0 |
| NULL | NULL | NULL | 2 | The Godfather | Mario Puzo | 1969 | 0-399-10342-2 |
| NULL | NULL | NULL | 4 | Slaughterhouse-5 | Kurt Vonnegut | 1969 | 0-7910-9295-x |
+------+-----------+------+------+------------------+-----------------+------+---------------+
- 30. 29© 2019, LogMeIn, Inc.
SELF JOIN
mysql> SELECT * FROM friends a JOIN friends b;
+------+---------------+------+------+---------------+------+
| id | Name | Year | id | Name | Year |
+------+---------------+------+------+---------------+------+
| 1 | Kovacs Zoltan | 1998 | 1 | Kovacs Zoltan | 1998 |
| 2 | Vas Maria | 2000 | 1 | Kovacs Zoltan | 1998 |
| 3 | Jo Lajos | 2014 | 1 | Kovacs Zoltan | 1998 |
| 4 | Kis Klara | 1979 | 1 | Kovacs Zoltan | 1998 |
| 1 | Kovacs Zoltan | 1998 | 2 | Vas Maria. | 2000 |
| 2 | Vas Maria | 2000 | 2 | Vas Maria | 2000 |
| 3 | Jo Lajos | 2014 | 2 | Vas Maria | 2000 |
| 4 | Kis Klara | 1979 | 2 | Vas Maria | 2000 |
| 1 | Kovacs Zoltan | 1998 | 3 | Jo Lajos | 2014 |
| 2 | Vas Maria | 2000 | 3 | Jo Lajos | 2014 |
| 3 | Jo Lajos | 2014 | 3 | Jo Lajos | 2014 |
| 4 | Kis Klara | 1979 | 3 | Jo Lajos | 2014 |
| 1 | Kovacs Zoltan | 1998 | 4 | Kis Klara. | 1979 |
| 2 | Vas Maria | 2000 | 4 | Kis Klara | 1979 |
| 3 | Jo Lajos | 2014 | 4 | Kis Klara | 1979 |
| 4 | Kis Klara | 1979 | 4 | Kis Klara | 1979 |
+------+---------------+------+------+---------------+------+
- 31. 30© 2019, LogMeIn, Inc.
Indexes
mysql> SHOW CREATE TABLE neos_largeG
*************************** 1. row ***************************
Table: neos_large
Create Table: CREATE TABLE `neos_large` (
`id` int(11) DEFAULT NULL,
[...]
`ink` double DEFAULT NULL,
`om` double DEFAULT NULL,
`w` double DEFAULT NULL,
KEY `tau` (`tau`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> SELECT COUNT(*) FROM neos_large;
+----------+
| COUNT(*) |
+----------+
| 18254688 |
+----------+
1 row in set (4.04 sec)
- 32. 31© 2019, LogMeIn, Inc.
Indexes
mysql> SELECT MAX(ink) FROM neos_large;
+-------------------+
| MAX(ink) |
+-------------------+
| 154.3668580789491 |
+-------------------+
1 row in set (5.10 sec)
mysql> ALTER TABLE neos_large ADD INDEX(ink);
Query OK, 0 rows affected (44.76 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT MAX(ink) FROM neos_large;
+-------------------+
| MAX(ink) |
+-------------------+
| 154.3668580789491 |
+-------------------+
1 row in set (0.00 sec)
- 33. 32© 2019, LogMeIn, Inc.
Views
mysql> CREATE VIEW neos_ink AS SELECT id, ink FROM neos_large;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM neos_ink LIMIT 5;
+----+---------------------+
| id | ink |
+----+---------------------+
| 1 | 10.82856921420592 |
| 2 | 11.56484503237207 |
| 3 | 9.384537356044309 |
| 4 | 26.68761210417409 |
| 5 | 11.87652956555689 |
+----+---------------------+
5 rows in set (0.00 sec)
- 34. 33© 2019, LogMeIn, Inc.
Stored procedures
DELIMITER $$
DROP PROCEDURE IF EXISTS duplicate_rows$$
CREATE PROCEDURE duplicate_rows (
IN tablename VARCHAR(255),
IN times INT
)
BEGIN
SET @statement = CONCAT(CONCAT(CONCAT(CONCAT("INSERT INTO ",tablename),"
SELECT * FROM "),tablename),";");
PREPARE stmt FROM @statement;
WHILE times > 0 DO SELECT times as "Remaining";
EXECUTE stmt;
SET times = times - 1;
END WHILE;
DEALLOCATE PREPARE stmt;
END $$
- 35. 34© 2019, LogMeIn, Inc.
Functions
CREATE FUNCTION circle_area (radius REAL)
RETURNS REAL DETERMINISTIC
RETURN POW(radius,2) * PI();
CREATE FUNCTION circle_circumference (radius REAL)
RETURNS REAL DETERMINISTIC
RETURN radius * 2 * PI();
- 36. 35© 2019, LogMeIn, Inc.
Triggers
DELIMITER $$
CREATE TRIGGER remove_credit_card
BEFORE INSERT ON users FOR EACH ROW
BEGIN
IF new.credit_card_number = '' THEN
SET credit_card_number = NULL;
END IF;
END
$$
- 37. 36© 2019, LogMeIn, Inc.
MongoDB
“MongoDB is a document database with the scalability and flexibility that you
want with the querying and indexing that you need”
- 38. 37© 2019, LogMeIn, Inc.
INSERT
db.konyvek.insert(
[
{ Cim: "Omerta", Szerzo: "Mario Puzo", Ev: 2000, ISBN10:
"0-434-00870-2"},
{ Cim: "The Godfather", Szerzo: "Mario Puzo", Ev: 1969,
ISBN10: "0-399-10342-2"},
{ Cim: "Birdy", Szerzo: "William Wharton", Ev: 1979,
ISBN10: "0-679-73412-0"},
{ Cim: "Slaughterhouse-five", Szerzo: "Kurt Vonnegut ", Ev:
2000, ISBN10: "0-7910-9295-x"}
]
)
- 39. 38© 2019, LogMeIn, Inc.
Find
> db.konyvek.find(
{Cim: "Slaughterhouse-five"}
)
{ "_id" : ObjectId("5c927003d9e410e1bb9fa583"), "Cim" :
"Slaughterhouse-five", "Szerzo" : "Kurt Vonnegut ", "Ev" :
2000, "ISBN10" : "0-7910-9295-x" }
- 40. 39© 2019, LogMeIn, Inc.
Update
> db.konyvek.update(
{ISBN10: "0-7910-9295-x"},
{Cim: "Slaughterhouse-five", Szerzo: "Kurt Vonnegut ", Ev:
2000, ISBN10: "0-7910-9295-X"}
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" :
1 })
- 41. 40© 2019, LogMeIn, Inc.
“Schemaless”
> db.konyvek.update({"Cim" : "Slaughterhouse-five", "Szerzo" : "Kurt Vonnegut ",
"Ev" : 1969, "ISBN10" : "0-7910-9295-x"},{"Cim" : "Slaughterhouse-five", "Szerzo"
: "Kurt Vonnegut ", "Ev" : 2000, "ISBN10" : "0-7910-9295-X", ISBN13: "978-0-7910-
9295-8"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.konyvek.find()
{ "_id" : ObjectId("5c929445345a87cb9fea0bc1"), "Cim" : "Omerta", "Szerzo" :
"Mario Puzo", "Ev" : 2000, "ISBN10" : "0-434-00870-2" }
{ "_id" : ObjectId("5c929445345a87cb9fea0bc2"), "Cim" : "The Godfather", "Szerzo"
: "Mario Puzo", "Ev" : 1969, "ISBN10" : "0-399-10342-2" }
{ "_id" : ObjectId("5c929445345a87cb9fea0bc3"), "Cim" : "Birdy", "Szerzo" :
"William Wharton", "Ev" : 1979, "ISBN10" : "0-679-73412-0" }
{ "_id" : ObjectId("5c929445345a87cb9fea0bc4"), "Cim" : "Slaughterhouse-five",
"Szerzo" : "Kurt Vonnegut ", "Ev" : 1969, "ISBN10" : "0-7910-9295-X", "ISBN13" :
"978-0-7910-9295-8" }