programming_language
tuple: a row in a table
relation: a table consisting of several tuples
database: several relations
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
create table <table_name> (attr1 data_type,... ,primary key(attrs), foreign key(attr) another_table)
prime key is unique and not null
foreign key is a reference to other table.
select <attrbutes>----projectionfrom <relations> ----- Cartesian-productwhere <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
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 ...
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>
<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.
order by <attr> desc/asc ascend default
where <attr> like '%abc%d_'
% matches any string
_ matches a single char
escape '/' means /% is just a %
(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.
is null / is not null /
<tuple> in <relation>
not in
exsists
some at least one
all for all
describe numeric range
decomposite the complicated query by defining temporary tiny view
with tempo_view(attr1,attr2...) asselect ... from ...select ...from tempo_viewwhere ...
change the value of some features without changing the whole tuple.
update <table_name>set <old_attr> = <new_attr>where ...
casewhen <pred1> then <result1>when <pred2> then <result2>...eles <result0>end
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
in case of data destruction
create table <table_name> (attr1 data_type,... ,primary key(attrs),foreign key(attr) another_tablecheck(attr1 in (val1,val2,val3...)))
the value of attribute1 must be among the given values.
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 is not actual logic model but a virtual relation shown to users.
create view <name> as <query expression> create view <name>(attr1,attr2...) as <query expression> More: mateiralized view & view maintenance.
inner join
left outer join
natural right outer join
full outer join
select count(city)-(select count(distinct city) from table) from table