quartz/content/notes/20-database-3.md
Jet Hughes 8a667e5693 update
2022-05-27 14:12:53 +12:00

4.2 KiB

title aliases tags sr-due sr-interval sr-ease
20-database-3
info201
lecture
2022-05-20 3 250

review questions

Data Integrity

GIGO

recall integrity

Types of error

  • ennecessary duplication of data
  • missing information (expecially nulls)
  • referential integrity problems: broken links, "orhpan" records (foreign keys)
  • data entry errors: typos/keying errors, value in wrong field
  • invalid/nonsensical data, e.g., nefative salary
  • going against business rules/policy law

Validation

checking that entered values are plausible.

  • values must make sens, be valid
  • simple checks to block obvisously bogus data

automated by constraints within the data base

valid ≠ correct

Verification

Checking that correct value was entered e.g.,

  • double check input
  • independent double entry
  • independent triple entry (or more) for critical checks

manual process invvolving human input and judgement

valid + verified ≠ correct.

  • malicious vandalism
  • human psychology rtends to promote dertain kinds of error
  • misread handwritten notes
  • incorrect for fake information provided

validation and verification examples student id check digit

Integrity constraints

machine readable conditions (true/false)

checked when data is changed

  • in oracle all existing data must conform
  • in SQL erver, you havel to explicity tell it to check existing data

ideally these are in the daabase. but some this is not possibe and must be implemented in code.

Defining constraints

col-level constraints

  • specified in line in col defs
  • can only refer to that column

table level constraints

  • are specified out of line alongside other columns
  • can refer to any col inthe table

constrainst should be named

Primary and foreign key constraints

example

primary keys are not null and are unique

some DBMSs support on delete and on update actios on foreign keys when a parent row is deleted or updated

  • cascade: "child" rows inherit the same operation as "parent" row
  • set null: "child" FK is set to null (if permitted)
  • set default: ""

Unique values (other than primary keys)

Check constrainsts

for chcking if within list. use a lookup table

Automation

Sequences

generate integer values

sequence sequences auto vs custome java accesing the sequence java accesing the sequence (fluent)

caution

Triggers

specific operation on table trigger other operations

normally written in a "proper" language e.g.,

  • Oracle: PL/SQL, Java
  • H2: java
  • SQL server: transact-SQL
  • PostgreSQL: most languages, e.g., python, ruby, perl, r, bash, java, php etc

used when:

  • as a last resort
  • computed columns
  • setting status values in reponse to updates
  • maintaining refernetial integrity
  • rewriting application input
  • integrity constraints that involve multiple tables
  • row based security policies
  • domain specific auditing (beyond standard logging features)
  • performing actions outside the DBMS

specifications

  • timing: before, after, instead of
  • type of operation: insert update delete
  • column affected (update only, optional)
  • table affected
  • other conditions (optional)

triggered operation

  • what to do
  • trigger will activate only once unless you tell to to execute once for each row affectted by the activation conditions

H2 trigger H2 trigger action

Stored procedure

programming code stored within the database

why?:

  • reduce or eliminate application/database round trips
  • offload database-oriented processing to the DBMS
  • encapsulate database code in the database for re use (DRY)
  • encapsulate query details

triggers often a special case h2 lets you create aliases to java functions, but these aren't stored in the database

h2 function alias