Docsity

Understanding NULL Values, Constraints, and Triggers in SQL, Study notes of Logic

Partial preview of the text

Download Understanding NULL Values, Constraints, and Triggers in SQL and more Study notes Logic in PDF only on Docsity!

NULL values, SQL

Constraints & Triggers

Kathleen Durant PhD

CS 3200

Lesson 7

Lecture Outline

  • Constraints
  • NULL Values
  • Trigger Description
  • My SQL trigger example
  • MY SQL Installation

Missing data values in relations

  • Allowing fields to have no value allows us to model the real world as well as mathematics
  • We need to be able to semantically represent the NAN or a value that does not have a value (yet) or a value that does exist we just do not know it
  • Missing data is prevalent in many fields of study – the majority of data is missing - Goal: get a good representation of what is not there given the

minority that is provided

  • Many statistical and data mining techniques defined to deal with

missing data

NULLS in SQL

  • NULL is a placeholder for missing or unknown value of an attribute. - It is not itself a value, therefore it has no data type.
  • Codd proposed to distinguish two kinds of NULLs:
  • A-marks: data is applicable but not known (for example, someone’s age)
  • I-marks: data is Inapplicable (telephone number for someone who does not have a telephone, or spouse’s name for someone who is not married)

Problems with NULLs

  • Defining selection operation: if we check tuples for some property like Mark > 40 and for some tuple Mark is NULL, do we include it?
  • Defining intersection or difference of two relations: are two tuples <John, NULL> and <John,NULL> the same or not?
  • Additional problems for SQL: do we treat NULLs as duplicates?
  • Do we include them in count, sum, average and if yes, how? How do arithmetic operations behave when an argument is NULL?

Solutions to NULL: three values in Logic

  • Use three-valued logic instead of classical two-valued logic to evaluate conditions.
  • When there are no NULLs around, conditions evaluate to true or false, but if a null is involved, a condition will evaluate to the third value (‘undefined’, or ‘unknown’).
  • This is the idea behind testing conditions in WHERE clause of SQL SELECT: only tuples where the condition evaluates to true are returned.

SQL: NULLs in conditions

  • Select SID from Sailor where rating > 5
  • Execution: rating > 5 evaluates to ‘unknown’ on the last tuple SID Sname Rating Age 28 Yuppy 9 35. 31 Lubber 3 55. 44 Guppy 5 35. 58 Rusty NULL 35. SID Sname Rating Age 28 Yuppy 9 35.

SQL: NULLs in conditions

  • Select SID from Sailor where rating > 5 OR Name = ‘Rusty’
  • Execution: rating > 5 evaluates to true on the last tuple SID Sname Rating Age 28 Yuppy 9 35. 31 Lubber 3 55. 44 Guppy 5 35. 58 Rusty NULL 35. SID Sname Rating Age 28 Yuppy 9 35. 58 Rusty NULL 35.

SQL with NULLS: Aggregates

  • Select avg(Rating) as AVG, COUNT(Rating) as NUM, COUNT(*) as ALLNUM, SUM(Rating) as SUM from Sailors - AVG = 5. - NUM = 3 - ALLNUM = 4 - SUM = 17 SID Sname Rating Age 28 Yuppy 9 35. 31 Lubber 3 55. 44 Guppy 5 35. 58 Rusty NULL 35.

Outer Joins

  • When we take the join of two relations we match up tuples which share values - Some tuples have no match are ‘lost’ - These are called dangles
  • Outer joins include dangles in the result set and use NULLs to fill in the blanks - LEFT OUTER JOIN - RIGHT OUTER JOIN - FULL OUTER JOIN

Default Value Example

  • Default values are
    • ???? For Name
    • -1 for Rating and Age
  • Hopefully no one has a name of ???? and rating and age cannot really be = -1 so can identify your default values
  • What about
  • Update Sailors set age = age +1? SID Sname Rating Age 28 Yuppy 9 35. 31 Lubber 3 55. 44 ???? 5 - 58 Rusty -1 35.0 16

Problems with default values

  • They are real values in the domain of the variable
    • They can be updated like any other field value
    • You need to use a value that will not appear in any other

circumstances

  • They may not be interpreted correctly
  • You need compatibility in the domains
    • You can’t have a string such as ‘unknown’ stored in an integer field
  • You may want to just use NULL

NULL or Default Value

  • Which method to use?
  • Default values should not be used when they might be confused with ‘real’ values
  • NULLs can (and often are) used where the other approaches seem inappropriate

Integrity Constraints

  • An IC describes conditions that every legal instance of a relation must satisfy. - Inserts, deletes, updates that violate IC’s are disallowed. - Can be used to ensure application semantics (e.g., sid is a key), or

prevent inconsistencies (e.g., sname has to be a string, age must

be < 200)

  • Types of IC’s: Domain constraints, primary key constraints, foreign

key constraints, general constraints.

  • Domain constraints: Field values must be of right type. This is always enforced.