Docsity

Missing Information-Introduction to Database System-Lecture 11 Slides-Computer Science, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
Missing Information G52DBS – Database System s
www.cs.nott.ac.uk/~smx/DBS
Missing Information
• Sometimes we don’t
know what value an
entry in a re lation
should have
• We know that there is
a value, but don’t
know what it is
• There is no value at
all that makes any
sense
• Two main methods
have been proposed
to deal with this
• NULLs can be used as
markers to show that
information is missing
• A default value can be
used to represent the
missing value
Missing Information G52DBS – Database System s
www.cs.nott.ac.uk/~smx/DBS
NULLs
• A NULL is a
placeholder for a
missing va lue
• It is not a value
• We’ve seen NULLs
before…
• Referential integrity
• Entity integrity
•SQL’s CREATE TABLE
statement
• NULLs provide a
convenient method
for marking missing
informat ion, but…
• They are not values
so can’t be used in
comparisons
• They introduce the
need for three-valued
logic (3VL)
Missing Information G52DBS – Database System s
www.cs.nott.ac.uk/~smx/DBS
Queries and NULLs
Parts
ID Name Wgt Qty
1 Nut 10 20
2 Bolt 15 NULL
3 Nail 3 100
4 Pin NULL 30
5 NULL 20 20
6 Screw NULL NULL
7 Brace 150 0
SELECT * FROM Parts
WHERE Qty < 0
SELECT * FROM Parts
WHERE Qty >= 0
UPDATE PARTS
SET Wgt = Wgt / 0
WHERE ID = 4
SELECT DISTINCT Qty
FROM Parts
Missing Information G52DBS – Database System s
www.cs.nott.ac.uk/~smx/DBS
Queries and NULLs
• When using NULLs:
• Arithmetic
expressions or
comparisons with a
NULL evaluate to
NULL
• SELECT statements
return those values
for which the
condition is known to
be true
• NULL is considered to
be a duplicate of
NULL, even though
NULL is not equal to
NULL
• Logical expressions
involving NULLs may
be evaluated using
three-valued logic
Missing Information G52DBS – Database System s
www.cs.nott.ac.uk/~smx/DBS
Queries and NULLs
Parts
ID Name Wgt Qty
1 Nut 10 20
2 Bolt 15 NULL
3 Nail 3 100
4 Pin NULL 30
5 NULL 20 20
6 Screw NULL NULL
7 Brace 150 0
Parts
ID Name Wgt Qty
1 Nut 10 20
2 Bolt 15 NULL
3 Nail 3 100
4 Pin NULL 30
5 NULL 20 20
6 Screw NULL NULL
7 Brace 150 0
SELECT * FROM Parts
WHERE Qty < 0
SELECT * FROM Parts
WHERE Qty >= 0
Missing Information G52DBS – Database System s
www.cs.nott.ac.uk/~smx/DBS
Queries and NULLs
Parts
ID Name Wgt Qty
1 Nut 10 20
2 Bolt 15 NULL
3 Nail 3 100
4 Pin NULL 30
5 NULL 20 20
6 Screw NULL NULL
7 Brace 150 0
UPDATE Parts
SET Wgt = Wgt/0
WHERE ID = 4
SELECT DISTINCT Qty
FROM Parts
This statement has no effect - it does
not even signal an error for dividing
by zero
If X has a NULL value, then ALL
arithmetic expressions involving X
evaluate to NULL including things
like X/ 0, X/X etc
pf3
pf4
pf5

Partial preview of the text

Download Missing Information-Introduction to Database System-Lecture 11 Slides-Computer Science and more Slides Introduction to Database Management Systems in PDF only on Docsity!

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Missing Information

  • Sometimes we don’t

know what value an

entry in a relation

should have

  • We know that there is a value, but don’t know what it is
  • There is no value at all that makes any sense - Two main methods

have been proposed

to deal with this

  • NULLs can be used as markers to show that information is missing
  • A default value can be used to represent the missing value

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

NULLs

  • A NULL is a

placeholder for a

missing value

  • It is not a value
  • We’ve seen NULLs

before…

  • Referential integrity
  • Entity integrity
  • SQL’s CREATE TABLE statement - NULLs provide a

convenient method

for marking missing

information, but…

  • They are not values so can’t be used in comparisons
  • They introduce the need for three-valued logic (3VL)

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Queries and NULLs

Parts ID Name Wgt Qty 1 Nut 10 20 2 Bolt 15 NULL 3 Nail 3 100 4 Pin NULL 30 5 NULL 20 20 6 Screw NULL NULL 7 Brace 150 0

SELECT * FROM Parts

WHERE Qty < 0

SELECT * FROM Parts

WHERE Qty >= 0

UPDATE PARTS

SET Wgt = Wgt / 0

WHERE ID = 4

SELECT DISTINCT Qty

FROM Parts

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Queries and NULLs

  • When using NULLs:
    • Arithmetic expressions or comparisons with a NULL evaluate to NULL
    • SELECT statements return those values for which the condition is known to be true - NULL is considered to be a duplicate of NULL, even though NULL is not equal to NULL - Logical expressions involving NULLs may be evaluated using three-valued logic

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Queries and NULLs

Parts ID Name Wgt Qty 1 Nut 10 20 2 Bolt 15 NULL 3 Nail 3 100 4 Pin NULL 30 5 NULL 20 20 6 Screw NULL NULL 7 Brace 150 0

Parts ID Name Wgt Qty 1 Nut 10 20 2 Bolt 15 NULL 3 Nail 3 100 4 Pin NULL 30 5 NULL 20 20 6 Screw NULL NULL 7 Brace 150 0

SELECT * FROM Parts

WHERE Qty < 0

SELECT * FROM Parts

WHERE Qty >= 0

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Queries and NULLs

Parts ID Name Wgt Qty 1 Nut 10 20 2 Bolt 15 NULL 3 Nail 3 100 4 Pin NULL 30 5 NULL 20 20 6 Screw NULL NULL 7 Brace 150 0

UPDATE Parts

SET Wgt = Wgt/

WHERE ID = 4

SELECT DISTINCT Qty

FROM Parts

This statement has no effect - it does not even signal an error for dividing by zero

If X has a NULL value, then ALL arithmetic expressions involving X evaluate to NULL including things like X/0, X/X etc

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Queries and NULLs

  • According to the rules
    • Avg = NULL
    • Num = 4, 3, or NULL?
    • Sum = NULL
  • According to SQL
    • Avg = 20,
    • Num = 3
    • Sum = 60,
    • SELECT COUNT()...* gives a result of 4

SELECT

AVG(Salary) AS Avg, COUNT(Salary) AS Num, SUM(Salary) AS Sum FROM Employee

Employee Name Salary John 25, Mark 15, Anne 20, Chris NULL

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Three-Valued Logic

  • Two-valued logic
    • Truth values
      • True (T)
      • False (F)
    • Operators
      • NOT
      • AND
      • OR
        • Three-valued logic
          • Truth values
            • True (T)
            • False (F)
            • Unknown (U)
          • Operators
            • NOT
            • AND
            • OR
            • MAYBE

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Three-Valued Logic

NOT AND T U F

T F T T U F

U U U U U F

F T F F F F

MAYBE OR T U F

T F T T T T

U T U T U U

F F F T U F

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

3VL Expressions

  • Any comparison with

NULL gives an

Unknown truth value

  • What are the value

of the expressions

on the right if

• A = 10

• B = 5

• C = NULL

(A > B) AND (B > C)

(A > B) OR (B > C)

(A > C) OR NOT (A > C)

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

NULLs in the Relational

Model

  • Primary keys cannot

contain NULLs

  • Candidate keys might, but then what are they candidates for?
  • NULL is not a value
  • It isn’t in any domain, so not all ‘values’ come from domains
  • Relational algebra
  • Some operators are unaffected
  • Some need to know if two NULLs are duplicates
  • Some needs three- valued logic

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Outer Joins

  • When we take the

join of two relations

we match up tuples

which share values

  • Some tuples have no match, and are ‘lost’
  • These are called ‘dangles’ - Outer joins include

dangles in the result

and use NULLs to fill

in the blanks

  • Left outer join
  • Right outer join
  • Full outer join

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Splitting Tables

  • NULLs and defaults

both try to fill entries

with missing data

  • NULLs mark the data as missing
  • Defaults give some indication as to what sort of missing information we are dealing with - Often you can remove

entries that have

missing data

  • You can split the table up so that columns which might have NULLs are in separate tables
  • Entries that would be NULL are not present in these tables

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Splitting Tables Example

Parts ID Name Wgt Qty 1 Nut 10 20 2 Bolt 15 NULL 3 Nail 3 100 4 Pin NULL 30 5 NULL 20 20 6 Screw NULL NULL 7 Brace 150 0

ID Name 1 Nut 2 Bolt 3 Nail 4 Pin 6 Screw 7 Brace

ID Wgt 1 10 2 15 3 3 5 20 7 150

ID Qty 1 20 3 100 4 30 5 20 7 0

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Problems with Splitting

Tables

  • Splitting tables has

its own problems

  • We might introduce many extra tables
  • Information gets spread out over the database
  • Queries become more complex and require many joins - We can recover the

original table, but

  • We need to do an outer join to do so
  • This introduces NULLs, which brings in all the associated problems again

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

SQL Support

  • SQL allows both

NULLs and defaults:

  • A table to hold data on employees
  • All employees have a name
  • All employees have a salary (default 10000)
  • Some employees have phone numbers, if not we use NULLs

CREATE TABLE Employee

Name CHAR(50)

NOT NULL,

Salary INT

DEFAULT 10000,

Phone CHAR(15)

NULL

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

SQL Support

SQL allows you to

insert NULLs

INSERT INTO Employee

VALUES (‘John’,

12000,NULL)

UPDATE Employee

SET Phone = NULL

WHERE Name = ‘Mark’

You can also check for

NULLs

SELECT Name FROM

Employee WHERE

Phone IS NULL

SELECT Name FROM

Employee WHERE

Phone IS NOT NULL

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Which Method to Use?

  • Often a matter of

personal choice, but

  • Default values should not be used when they might be confused with ‘real’ values
  • Splitting tables shouldn’t be used too much or you’ll have lots of tables - NULLs can (and often are) used where the other approaches seem inappropriate - You don’t have to always use the same method - you can mix and match as needed

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Example

  • For an online store

we have a variety of

products - books,

CDs, and DVDs

  • All items have a title, price, and id (their catalogue number)
  • Any item might have an extra shipping cost, but some don’t - There is also some

data specific to each

type

  • Books must have an author and might have a publisher
  • CDs must have an artist
  • DVDs might have a producer or director

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Example

• We could put all the data in one table

  • There will be many entries with missing

information

  • Every row will have missing information
  • We are storing three types of thing in one

table

ID Title Price ShippingAuthor Publisher Artist Producer Director

Items

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Example

  • It is probably best to

split the three types

into separate tables

  • We’ll have a main Items table
  • Also have Books, CDs, and DVDs tables with FKs to the Items table ID^ Artist

CDs

ID Author Publisher

Books

ID Producer Director

DVDs

ID Title Price Shipping

Items

Missing Information G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Example

  • Each of these tables

might still have

some missing

information

  • Shipping cost in items could have a default value of 0
  • This should not disrupt computations
  • If no value is given, shipping is free - Other columns could

allow NULLs

  • Publisher, director, and producer are all optional
  • It is unlikely we’ll ever use them in computation