(Relational DataBase Management System)
Relation
Relation
RDBMS
SQL
User
Other applications
accept SQL!
VS
Cheaper but still expensive
No infra cost
More reliable
Cheaper still
Only get a portion of resources
Might have performance issues
Only pay for what you use
Slightly pricier if run all the time
Allow for either dedicated or shared servers
(AWS) Relational Database Service
Local:
psql
Remote (RDS)
psql -h <address> -p <port> -U <user> <db>
To allow many users to use one database without interfering with each other.
To organize database objects into logical groups to make them more manageable.
Third-party applications can be put into separate schemas so they do not collide with the names of other objects.
(Table)
Create <THING> <NAME>
Create TABLE THING(
NAME VARCHAR(50)
)
Create TABLE THING(
<ATTRIBUTE> <ATTR_TYPE> (,)
)
CREATE TABLE FOOBAR(
FOO INT NOT NULL,
BAR INT
)
INSERT INTO FOOBAR VALUES (1,0)
INSERT INTO FOOBAR (BAR) VALUES (1)
TLDR
Scale Matters
4 Bytes
-2147483648 to +2147483647
DECIMAL
Variable
CAST(5.00 as MONEY)
Variable Character Array
Create Table With Constraint
Alter Table to add new attribute
Drop new attribute
UPDATE <TABLE> SET <COLUMN>=<VALUE>
SELECT retrieves rows from zero or more tables
SELECT 1+1;
SELECT * FROM <TABLE>;
SELECT * FROM <TABLE> WHERE <COLUMN> = <VALUE>;
Person
Address
Customer
Profile
Employee
Info
"Kind of" From Both Tables
T1 { [INNER] | { LEFT | RIGHT | FULL } [OUTER] } JOIN T2 ON boolean_expression
What we have in common
Look to the left
Look to the right
From Both Tables
Allows for more fine grained control over types.
Limits Values Allowed
Must Have Value
+ - / * %
abs() pi() sqrt()
cos() sin() tan()
REGEXP_MATCHES(NAME, '(.+)(e)$')
SELECT NAME AS PRODUCT_NAME
FROM PRODUCT
Using nested selects, query all of the items purchased which were over $3 per Person. The result should be Person Name, Product Purchased and Price. There should be only 1 record for Person/Product Combo
CREATE FUNCTION <Function Name>(<Param Type>...) RETURNS <Return Type> AS <Function Query>
LANGUAGE <Lang>
"It is not possible to change the name or argument types of a function this way (if you tried, you would actually be creating a new, distinct function)"
We to take the following schema...
CREATE TABLE ADDRESS(
ID SERIAL PRIMARY KEY,
ADDRESS_LINE_1 VARCHAR(50),
ADDRESS_LINE_2 VARCHAR(50),
CITY VARCHAR(50),
STATE VARCHAR(2),
ZIPCODE VARCHAR(5)
);
INSERT INTO ADDRESS (ADDRESS_LINE_1, CITY, STATE, ZIPCODE) VALUES ('301 W. 2nd Ave', 'Columbus', 'OH', '43201');
And create a function to concat the values
|| concats a string so `SELECT 'A' || 'B';` returns `AB`
Schema
Data
Standard Out
Database MUST be STOPPED
To back up an individual table copy "pg_clog/*" as well!
Other issues if clustered
Postgres
Logs Query
Log
Storage and Playback
Stores Query For Backup
Person
Customer
Customer
Customer
Transaction
Transaction
Transaction
TransactionProduct
Product
Product
Product
AKA Mapping Table
BEGIN;
<Do Something>
SAVEPOINT <Savepoint_Name>;
<Do More Stuff>
ROLLBACK TO <Savepoint_Name>;
<Do Even More Stuff>
COMMIT;
tomicity
urability
solation
onsistency
All or Nothing.
Transaction is a unit.
Database goes from a valid state before the transaction to a valid state after the transaction.
Database goes from a valid state before the transaction to a valid state after the transaction.
Transactions happen in isolation of other transactions.
Once committed a transaction remains committed.
Other DBs might not be fully complaint
Everyone gets in
Stores passwords in PostGres
Stores Password in LDAP
`When Validation Isn't Enough`
C Library For Postgres Communication
...
#include <libpq-fe.h>
...
PGconn *conn = PQconnectdb("user=postgres dbname=postgres");
if (PQstatus(conn) == CONNECTION_BAD) {
...
}
PGresult *res = PQexec( conn, "DROP TABLE IF EXISTS Cars" ); if (PQresultStatus(res) != PGRES_COMMAND_OK) { ... }
Native server driver, must be installed per OS
Same drive all OSes but must have Java
Complex SQL
Complex SQL
JSON Object
ORM
REST Controller
Mapping Service
JPA Repo
@Entity("Schema1")
...
@Qualifier("Schema1")
@Entity("Schema2")
...
@Qualifier("Schema2")
Schema1.User
Schema2.User
Deals with streams of real time data
Create a real-world use case
Use a Postgres DB to store the data for this use-case
Give a presentation on the Use Case
Deliverables
"No SQL" or Non-Relational
Real Time Data Streams
Extremely Large Datasets
Neural Network
Tables
Common Use Cases
Topic
Subscriber
Subscriber
Subscriber
Extremely large datasets
Distributed or parallel
Flatmap Vs Spack Map
Allows for parallel processing
You guys did great!