mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 05:14:06 -06:00
142 lines
4.0 KiB
Markdown
142 lines
4.0 KiB
Markdown
---
|
|
title: "18-advanced-SQL"
|
|
aliases:
|
|
tags:
|
|
- info201
|
|
- lecture
|
|
sr-due: 2022-05-15
|
|
sr-interval: 3
|
|
sr-ease: 250
|
|
---
|
|

|
|

|
|
|
|
varchar usually bigger than you think
|
|
|
|
# CRUD
|
|
- insert adds a row 
|
|
- select retrieves rows from the table
|
|
- ouput can be "saved" as a view 
|
|
- changes to the underlying table also chagnes the view
|
|
- update modifies rows 
|
|
- delete removes rows  
|
|
- test as a select statement first
|
|
|
|
# SQL DAO programming
|
|
We want to miniminse load on sql as connecting to database is expensive.
|
|
|
|
Optimisations:
|
|
- prefer muultiple row operations
|
|
- connection pools (keep connections "alive")
|
|
- reuse prepared statements (reduce unnecessary SQL parsing)
|
|
- consider combining queries to reduce round trips
|
|
- batched requests
|
|
|
|
Follow Optimistic approach
|
|
- assume operatios will succeed (no pre checking)
|
|
- handle errors with exception handling
|
|
- consider using merge if available
|
|
|
|
## Merge
|
|
 aka replace etc.
|
|
|
|
- update if they exist other wise insert
|
|
|
|
## JDBC
|
|
Java framework for interacting with sql databases.
|
|
|
|
plaform and DBMS independent
|
|
- driver provided by DBMS vendors
|
|
- same Java code will work with any DBMC
|
|
|
|
key concepts
|
|
- connections and connection pools
|
|
- sql strings and prepared statements
|
|
- result sets
|
|
- transactions
|
|
- batched requrests
|
|
|
|

|
|
|
|
|
|
## JDBI
|
|
- bette version of JDBI
|
|
- layered on top of [JDBC](#JDBC)
|
|
- better APIs
|
|
- less code
|
|
- simple class <-> table mapping
|
|
- flexible plugin architecture
|
|
|
|

|
|

|
|
|
|
|
|
# Transactions
|
|
interaction between two entities
|
|
- follow explicit or implied forms
|
|
- usually involves exchange on resources
|
|
- may require several steps
|
|
- often considered a single unit
|
|
|
|
## In data bases
|
|
- group of db operations is considered a *single logical unit*
|
|
- transfer (read and update)
|
|
- recieve shipement (update accounts)
|
|
- customer sale
|
|
|
|
transactions are all or nothing. (commit vs rollback)
|
|
|
|
## ACID
|
|
- Atomic
|
|
- all or nothing
|
|
- operations should be related
|
|
- Consistent
|
|
- transactions move dbs from one consistent state to another
|
|
- "consistent" ⇒ all integrity rules are satisfied
|
|
- db may be inconsistent during a transaction
|
|
- require defferable constraints
|
|

|
|
- Isolated
|
|
- concurrent transactios shouldn't interfere with each other
|
|
- ideally behave as if other transactions dont exist
|
|
- read committed isolation
|
|
- uncommited changes are visible to other transactions
|
|
- require some form of concrrency mangement (e.g., locking)
|
|
- 
|
|
- Durability
|
|
- once a transaction is commited it is permanent
|
|
- uncommited transaction dont survive crashes
|
|
- auto rollback of uncommitted transaction
|
|
|
|
## commit and rollback
|
|
- changes are made to "live" data
|
|
- commit makes database changes permanent
|
|
- rollback removes all changes since that transaction start
|
|
|
|
## Transaction in Java
|
|
default to auto commit.
|
|
- each statement is a separate transaction
|
|
- if transaction has multiple statements
|
|
- disable auto commit
|
|
- you must manage commit and rollback yourself
|
|
|
|

|
|

|
|

|
|
|
|
# Select
|
|
- select <- wahat
|
|
- from <- from where
|
|
- where <- filter
|
|
- group <- aggregation
|
|
- order <- order
|
|
|
|
distinct removes duplicate rows
|
|
|
|

|
|
|
|

|
|
|
|

|
|
|