Docstoc

SQL _amp; Constraints

Document Sample
SQL _amp; Constraints Powered By Docstoc
					SQL Tables and Constraints
        Example




                             1
    The Database
   A hockey league with rinks, teams, and players:
    – Rink (name, phone, capacity)
    – Team (tname, city, color, wins, losses, tie, rname FK
      references Rink(name))
    – Player (id, name, num, pos, tname, tcity, FK (tname,
      tcity) references Team (tname, city))
 All relations are in BCNF
 The only FDs are PKall other fields
 Constraint: All players with uniform number 9
  must be goalies.
                                                              2
    Unpreserved Dependencies

   Suppose the users now decide that all rinks in
    the same city have the same capacity:
    – city  capacity
    – While trying to remain calm, you realize that:
    – This FD doesn’t exist in any single relation, so a
      join is required to check it each time we add or
      change a capacity value.
   The tradeoff:
    – Expensive to check, but
    – may not be checked often enough to justify
      creating a dependency-preserving decomposition.
                                                           3
Summary
   Keys can be specified:
    – With the attribute, for single-field keys
    – At end of “create table” statement (all keys)
    – In a separate “alter table” statements (all keys)
   “Check” constraints very useful
    – SQL Server: can only involve one relation
    – SQL Standard: more general
   Triggers, assertions for IC (FD) enforcement
    – SQL Server doesn’t support assertions
    – Must be careful, in general, with triggers
                                                          4

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:9/14/2012
language:Unknown
pages:4