SQL Grammar

programming_language

conventions

tuple: a row in a table
relation: a table consisting of several tuples
database: several relations

relation algebra fundation

select, project, union, difference, Cartesian-product, rename
Union: the two relations must follow the same features and their domain won't conflict.

supplyment:

expand relation algebra

basic data type

basic operations

create a table/relation

  1. create table <table_name> (attr1 data_type,... ,
  2. primary key(attrs), foreign key(attr) another_table)

prime key is unique and not null
foreign key is a reference to other table.

select, from, where

  1. select <attrbutes>----projection
  2. from <relations> ----- Cartesian-product
  3. where <predicates> -----selection predicate

SQL returns a relation

select * means to select all attributes.
select distinct means to eliminate duplicates
select all means not to do that
name as T used to set an alias

insertion

add a tuple to a table
insert into <table_name> values(attr1 val1, attre2 val2...)
all attributes follow the order in the definition.
If you forget the order, use this:
insert into <table_name>(attr1,atr2,...) values(val1,val2...)

add an attribute to a table
alter table <table_name> add <attribute> <domain>

how to insert in batches?
One way to insert several tuples is:
insert into <table_name> select ... from ... where ...

delete

Note: delete clause only applies to ONE relation.
clear all tuples from the relation
delete from <table_name>
delete the table from the database
drop table <table_name>
delete an attribute from a table
alter table <table_name> drop <attribute>

join

<table_name1> natural join <table_name2>
Natural join returns tuples with the same value on the same attributes of both realtions.

<table1> join <table2> using(<attr1>,<attr2>...)
join tuples from two relations with the same values on the given attributes.

set order

order by <attr> desc/asc ascend default

string matching

where <attr> like '%abc%d_'
% matches any string
_ matches a single char
escape '/' means /% is just a %

set operations

  1. (select A from B) union/intersect/except (all) (select C from D)

union, intersect, except abandon dupllicates automatically

### aggregating functions
avg min max sum count
group by <attr> construct groups according to features to which aggregating functions apply
having <predicates> predicates apply to groups generated by group by

If there are both where and having subsentence in a query, SQL culculates predicates in where first and forms groups according to group by, before applying having. Groups not fitting having predicates will be abandaned and the left return to select as result.

useful components

detect null

is null / is not null /

check whether a tuple is in the relation

<tuple> in <relation>
not in

check an empty relation

exsists

compare

some at least one
all for all

between ... and ...

describe numeric range

with clause

decomposite the complicated query by defining temporary tiny view

  1. with tempo_view(attr1,attr2...) as
  2. select ... from ...
  3. select ...
  4. from tempo_view
  5. where ...

update

change the value of some features without changing the whole tuple.

  1. update <table_name>
  2. set <old_attr> = <new_attr>
  3. where ...

case clause

  1. case
  2. when <pred1> then <result1>
  3. when <pred2> then <result2>
  4. ...
  5. eles <result0>
  6. end

Advanced operations

grant

grant <operations> on <attributes> to <users> (with grant option)
authorize other users with centain operations (including the operation to autherize)
revoke <operations> on <attributes> to <users>
take back that authorization

integrity constrain

in case of data destruction

  1. create table <table_name> (attr1 data_type,... ,
  2. primary key(attrs),
  3. foreign key(attr) another_table
  4. check(attr1 in (val1,val2,val3...))
  5. )

the value of attribute1 must be among the given values.

transaction

A transaction consists of query expressions and update expressions.
When a SQL clause was excuted, a transaction started.
The following clause end a transaction:
commit work (submit the update made by SQL above and automatically start a new transaction)
or
rollback work

view

view is not actual logic model but a virtual relation shown to users.

More: mateiralized view & view maintenance.

linkage relations

inner join
left outer join
natural right outer join
full outer join

HackerRank Experience