quartz/content/notes/18-advanced-sql-1.md
Jet Hughes 8a667e5693 update
2022-05-27 14:12:53 +12:00

4.0 KiB

title aliases tags sr-due sr-interval sr-ease
18-advanced-SQL
info201
lecture
2022-05-15 3 250

create table 2 create table 2

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

typical jdbc

JDBI

  • bette version of JDBI
    • layered on top of JDBC
    • better APIs
    • less code
  • simple class <-> table mapping
  • flexible plugin architecture

typical jdbi (fluent) typical jdbi (declarative)

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 atomc and consistent
  • 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)
    • improper isolation
  • 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

JDBC explicit transactions JDBI explcit transaction (fluent) JDBI explcit transaction (declarative)

Select

  • select <- wahat
  • from <- from where
  • where <- filter
  • group <- aggregation
  • order <- order

distinct removes duplicate rows

distinct

select

from