Docsity

SQL Document: Selections, Joins, Inserts, Updates, Deletes, Aggregates, Grouping, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Document: Selections, Joins, Inserts, Updates, Deletes, Aggregates, Grouping and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Structured Query Language

Overview

  • Language
  • Selections
  • Joins
  • Insertions
  • Updates and Deletes
  • Aggregate Functions
  • Grouping
  • SQL Training Resources

Selections

  • Selects
    • select < element > from < table > where < criteria >
    • select is Relational algebra’s projection operation.
    • where is relational algebra’s selection operation.
    • Select does not maintain relational algebra’s uniqueness property. Hence the ‘distinct’ keyword.

Selection Criteria

  • Numbers/Identities
    • Can be indicated with an equal sign
  • Strings
    • Require single quotes
    • Can be wildcarded using
      • % (multichar)
      • _ (singlechar)
    • Depending on database implementation, may or may not have to worry about case. - Oracle is case sensitive, SQL Server, Postgres, etc. are not

Joins (Continued)

  • The previous joins follow the format:
    • Select < elems > from tableA < joinType > tableB on < criteria > ie: select * from employee left join department on (employee.dept = dept.id);
  • Also, manual joins can be used
    • Select < elems > from tableA, tableB where < criteria > ie: select * from employee, department where employee.dept = dept.id; // note, not necessarily same result as above

Table Aliasing

  • Used in manual joins.
  • Useful for joining multiple tables, saving

typing in where criteria. Follows format:

  • select < tableAlias> .property from < tableName> < tableAlias> where < tableAlias >.property = foo;
  • select p.name, r.balanceOwed from person p, record r where r.patientId = p.id;

Updates and Deletes

  • Updates
    • Update < tableName > set < attrib > = < value > where < criteria >
  • Deletes
    • Delete from where Leaving out where criteria will delete/Update everything in the table

Aggregate Functions

  • Count – returns count of matching tuples
  • Average – returns average of specified attribue
  • Max – returns max of specified attribute
  • Min – returns min of specified attribute
  • Sum – returns sum of all rows in table attribute
  • Follow format:

select fn (< elem >) from < table > where < criteria >

Schema Creation, Deletion

  • Primary Keywords
    • Create create table { attribName … } usual attribute types include char, varChar(#), int.
    • Drop
      • Cascade – delete all connected tables (ie: matching key pairs)
      • Restrict – Delete only empty tables
  • These have already been covered in-depth in previous presentations.

Common Errors

  • Using double-quotes instead of single for

string literals

  • Forgetting escape characters for words

featuring apostrophes

ie: ‘o’reilly’ will terminate the string after o

should be ‘o\’reilly’