CS145 Lecture Notes (4) -- SQL: Queries and Updates


(Diagram of how the SQL standard evolves over time)


















SQL is a big language, not just queries and updates.
Components of the language:

DDL = Data Definition Language
DML = Data Manipulation Language


Basic DDL commands

   CREATE TABLE name (att1 type1, att2 type2, ..., attN typeN)

   DROP TABLE name
See textbook for details of types.


DML commands

   SELECT, INSERT, DELETE, UPDATE

SELECT Statement

   SELECT A1, A2, ..., An
   FROM   R1, R2, ..., Rm
   WHERE  condition
Equivalent to relational algebra query:
   PROJECT_{A1, A2, ..., An} SELECT_{condition} (R1 X R2 X ... X Rm)
Returns unnamed relation whose schema is (A1, A2, ..., An)



[[On-Line SQL Demo]]

Schema for examples:
   Student(ID, name, GPA, sizeHS)       // ID is key
   Campus(location, enrollment, rank)   // location is key
   Apply(ID, location, major, decision) // (ID,location,major) is key
Constructs covered:

Summary of SQL SELECT Statement

   SELECT    attributes, aggregates
   FROM      relations
   WHERE     condition
   GROUP BY  grouping attributes
   HAVING    conditions on aggregates
   ORDER BY  attributes, aggregates
=> All optional except SELECT and FROM

("Flowchart" of clause evaluation)