mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-24 13:24:05 -06:00
4.0 KiB
4.0 KiB
| title | aliases | tags | sr-due | sr-interval | sr-ease | ||
|---|---|---|---|---|---|---|---|
| 18-advanced-SQL |
|
2022-05-15 | 3 | 250 |
varchar usually bigger than you think
CRUD
- insert adds a row

- select retrieves rows from the table
- 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
- 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
- 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
- Isolated
- 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















