目录
sql中表的定义语言
CREATE TABLE Movies(
title CHAR(100),
year INT,
length INT,
);
修改表
DROP TABLE movies; //删除关系
ALTER TABLE movies ADD producer INT;//增加一个属性
ALTER TABLE movies DROP title;//删除一个属性
ALTER TABLE movies MODIFY title;//增写一个属性,(直接写最后结果)
ALTER TABLE movies RENAME moviesNEW;//改变表名
ALTER TABLE movies RENAME title TO moviename;//改变属性名
键的声明
1)primary 2)unique
sql中单表查询语言
select* from customer;
select cusid,name from customer;
select custid,name as customer form customer;
select orderno,lineno,unitprice*quantilty as account from salesltem;
select orderno,lineno,unitprice*quantilty as account,'Yuan'as RMB from salesltem;
字符串比较:两个字符串里字符序列完全相同时称两个字符串相等。空字符串最小
模式匹配like
s like p
%:能匹配s中任何长度的字符
—:能匹配s中任何一个字符
select* from salesman
where name like'wang%';//找一个姓王的的销售员
select* from salesman
where name like'Wang_';//找一个叫王x的销售员
日期和时间
日期:“yyyy-mm-dd”
时间:“hh:mm:ss.ssss”
空值的比较
两条规则:null和任何值做算术运算,结果仍为null
使用比较操作符时,比较null和任意值时结果为unknown
select*form salesman where deptid is null;//判断是不是空值
三值逻辑
ture=1,false=0,unknown=0.5
a and b=min(a,b)
a or b=max(a,b)
not a=1-a
expression is true/false(不能是1/0,不能是unknown)
对结果进行排序
order by { expression|integer}[ASC/DESC]
调整行的顺序,asc表示升序,dsc表示降序
多表查询
区分不同关系的同名属性
r.a关系r里的属性a
若关系或属性的名字和sql里的保留子相同,则把对应的关系or属性加双引号
元组变量
允许为from子句每次出现的关系用as来定义别名
select s1.empid,s1.name;
from salesman as s1,salesman as s2;
where s1.empid<>s2.empid and s1.name=s2.name;
order by s1.empid
子查询
一个查询是另一个查询的一部分时,成为子查询(subquery)
本质上子查询返回的是关系
用=来比较结果时要求子查询返回单值(标量值)
select name, phone;
from customer;
where custom = (select custid from saleorder where empid ='a0043');
关系的条件表达式
any,all,in(在关系中),exists(当且仅当r不为空返回真/假)
//find the name of the students who learned the course 'c1'
select name;
from student;
where Sid in (select Sid from enrolment where cid='c1');
//in or =any
关联子查询
复杂的嵌套子查询要求一个子查询计算多次,每次赋给查询中的某项来自子查询外部的某个元组变量的值
select name
from salesman as s
where empid <> any(select empid from salesman where name=s.name);
from子句中的子查询
每个子查询返回的结果都是一个关系,可以直接用在from中,需要对其命名
select type, spec
from merchandise,
(select merid from salesitem,salesorder where salesitem.orderno=salesitem.orderno)
sales
where merchandise.merid=sales.merid;
外链接中on和where的区别
不满足on的取null,不满足where的去除元组
select cid
from enrolment
去除重复元组
select distinct
聚集操作符
sum,avg,min,max,count(计数)
//find Id of the order forms that have the maximum unit price of merchandise'fr8633'
select orderno
from salesitem
where merid = 'fr8633' and unitprice = (select max(unitprice) from salesitem where merid ='fr8633');
分组
group,select后面属性和group by后面属性相同
sql中表的更新语言
insertion
insert into r(a1,a2,...an) values(v1,v2...vn)
deletion
delete from r where cond
满足条件的所有元组都会被r删除,若没有满足where的,则r中所有元组都被删除
仅删除元组,表依旧存在
update
update r set a1=v1...an=vn where cond
满足条件的元组对相应的属性进行修改赋值,没有where语句则对所有元组进行属性取值进行更新修改
事务
-事务的执行
sql里用commit语句来提交事务,一旦提交成功,对数据库的修改是持久性的
用rollback语句来撤回事务,该事务的操作已引起的修改都将被撤回。
(commit和rollback都导致事务结束)
-并行事务引起的问题
读脏数据、不可重复读、幻像元组
-sql的隔离层次
l0 读未提交 l1 读已提交 l2 可重复读 l3 可串行化
//set transaction isolation level x
where x =
serialisable
repeatable read
read committed
read uncommitted