Docsity

Database Design: SQL and Constraints - Prof. Mihai Pop, Study notes of Principles of Database Management

Partial preview of the text

Download Database Design: SQL and Constraints - Prof. Mihai Pop and more Study notes Principles of Database Management in PDF only on Docsity!

CMSC 424 – Database design

Lecture 8

SQL, constraints

Mihai Pop

Next:

Integrity constraints

Prevent semantic inconsistencies

Key Constraints

Idea: specifies that a relation is a set, not a bag

SQL examples:

  1. Primary Key: CREATE TABLE branch( bname CHAR(15) PRIMARY KEY, bcity CHAR(20), assets INT); or CREATE TABLE depositor( cname CHAR(15), acct_no CHAR(5), PRIMARY KEY(cname, acct_no));
  2. Candidate Keys: CREATE TABLE customer ( ssn CHAR(9) PRIMARY KEY, cname CHAR(15), address CHAR(30), city CHAR(10), UNIQUE (cname, address, city));

Key Constraints

Effect of SQL Key declarations

PRIMARY (A1, A2, .., An) or

UNIQUE (A1, A2, ..., An)

Insertions: check if any tuple has same values for A1, A2, .., An as any inserted tuple. If found, reject insertion Updates to any of A1, A2, ..., An: treat as insertion of entire tuple Primary vs Unique (candidate)

  1. 1 primary key per table, several unique keys allowed.
  2. Only primary key can be referenced by “foreign key” (ref integrity)
  3. DBMS may treat primary key differently (e.g.: create an index on PK) How would you implement something like this?

Attribute Constraints

Domains: can associate constraints with DOMAINS rather than

attributes

e.g: instead of: CREATE TABLE depositor( .... balance INT NOT NULL, CHECK (balance >= 0) ) One can write: CREATE DOMAIN bank-balance INT ( CONSTRAINT not-overdrawn CHECK (value >= 0), CONSTRAINT not-null-value CHECK( value NOT NULL)); CREATE TABLE depositor ( ..... balance bank-balance, ) Advantages?

Attribute Constraints

Advantage of associating constraints with domains:

  1. can avoid repeating specification of same constraint for multiple columns
  2. can name constraints e.g.: CREATE DOMAIN bank-balance INT ( CONSTRAINT not-overdrawn CHECK (value >= 0), CONSTRAINT not-null-value CHECK( value NOT NULL)); allows one to:
  3. add or remove: ALTER DOMAIN bank-balance ADD CONSTRAINT capped CHECK( value <= 10000)
  4. report better errors (know which constraint violated)

SQL Query Examples

  • (^) Movie( title, year , length, inColor, studioName, producerC#)
  • StarsIn(movieTitle, movieYear, starName)
  • (^) MovieStar( name , address, gender, birthdate)
  • (^) MovieExec(name, address, cert#, netWorth)
  • (^) Studio( name , address, presC#)
  • (^) Queries:
    • (^) Producer with maximum average length of movies
    • (^) Find producer of Star Wars.
    • (^) All producers of movies in which harrison ford stars

SQL Query Examples

  • (^) Movie( title, year , length, inColor, studioName, producerC#)
  • StarsIn(movieTitle, movieYear, starName)
  • (^) MovieStar( name , address, gender, birthdate)
  • (^) MovieExec(name, address, cert#, netWorth)
  • (^) Studio( name , address, presC#)
  • (^) Queries:
    • (^) Find movie titles that appear more than once
    • (^) Find number of people 3 hops away from Kevin Bacon