Docsity

The SQL Query Language - Lecture Notes | CMPSCI 645, Study notes of Principles of Database Management

Partial preview of the text

Download The SQL Query Language - Lecture Notes | CMPSCI 645 and more Study notes Principles of Database Management in PDF only on Docsity!

SQL

CMPSCI 645

SQL Overview

  • SQL Preliminaries
  • Integrity constraints
  • Query capabilities
    • SELECT-FROM- WHERE blocks,
    • Basic features, ordering, duplicates
    • Set ops (union, intersect, except)
    • Aggregation & Grouping
      • Nested queries (correlation)
      • Null values
  • Modifying the database
  • Views Review in the textbook, Ch 5

Two parts of SQL

  • Data Definition Language (DDL)
    • Create/alter/delete tables and their attributes
    • establish and modify schema
  • Data Manipulation Language (DML)
    • Query and modify database instance

Creating Relations in SQL

  • Creates the Student relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified.
  • As another example, the Takes table holds information about courses that students take. CREATE TABLE Student (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL) CREATE TABLE Takes (sid CHAR(20), cid CHAR(20), grade CHAR(2))

Destroying and Altering Relations

  • Destroys the relation Student. The schema information and the tuples are deleted. DROP TABLE Student
  • !The schema of Student is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. ALTER TABLE Student ADD COLUMN firstYear integer

Integrity Constraints (ICs)

  • (^) IC: condition that must be true for any instance of the database. - (^) ICs are specified when schema is defined. - (^) ICs are checked when relations are modified.
  • A legal instance of a relation is one that satisfies all specified ICs. - (^) DBMS should only allow legal instances.
  • If the DBMS checks ICs, stored data is more faithful to real-world meaning. - (^) Avoids data entry errors, too!

Student table 10 sid name login age gpa 50000 Dave dave@cs 19 3. 53666 Jones jones@cs 18 3. 53688 Smith smith@ee 18 3. 53650 Smith smith@math 19 3. 53831 Madayan madayan@music 11 1. 53832 Guldu guldu@music 12 2.

STUDENT

Specifying Key Constraints in SQL 11 CREATE TABLE Student (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL, UNIQUE (name, age), PRIMARY KEY (sid) )

Foreign Keys, Referential Integrity

  • Foreign key : Set of fields in one relation that is used to referʼ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like alogical pointerʼ.
  • (^) E.g. sid is a foreign key referring to Students:
    • (^) Takes ( sid : string, cid : string, grade : string)
    • (^) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.
    • (^) Can you name a data model w/o referential integrity?
      • Links in HTML!

Foreign Keys in SQL

  • Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Takes (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ) sid name login 50000 Dave dave@cs 53666 Jones jones@cs 53688 Smith smith@ee 53650 Smith smith@math 53831 Madayan madayan@music sid cid grade 50000 445 A 53688 483 C 53666 435 B Takes

STUDENT

Referential Integrity in SQL

  • SQL/92 and SQL:1999 support all 4 options on deletes and updates. - (^) Default is NO ACTION ( delete/ update is rejected ) - (^) CASCADE (also delete all tuples that refer to deleted tuple) - (^) SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Takes (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT )

Where do ICs Come From?

  • ICs are based upon the semantics of the real-world enterprise that is being described in the database relations.
  • We can check a database instance to see if an IC is violated, but we can NEVER infer that an IC is true by looking at an instance. - (^) An IC is a statement about all possible instances! - (^) From example, we know name is not a key, but the assertion that sid is a key is given to us.
  • Key and foreign key ICs are the most common; more general ICs supported too.

Example database 19 Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day) Key for each table indicated by underlined attributes.

20 sid bid day 22 101 10/ 22 102 10/ 22 103 10/ 22 104 10/ 31 102 11/ 31 103 11/ 31 104 11/ 64 101 9/ 64 102 9/ 74 103 9/ sid sname rating age 29 brutus 1 33 85 art 3 25. 95 bob 3 63. 96 frodo 3 25. 22 dustin 7 45 64 horatio 7 35 31 lubber 8 55. 32 andy 8 25. 74 horatio 9 35 58 rusty 10 35 71 zorba 10 16 Sailors Reserves bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red Boats