SQL基础学习笔记【Forward】

上一篇 / 下一篇  2014-01-03 17:01:39

数据定义
create
alter
drop

tableviewindexproceduretriggerschemadomain

数据操作
select
insert
delete
update

数据控制
grant
deny
revoke

事物控制
commit
rollback
set transaction

注明:以下SQL语句中M,N为表A,B,C为栏目
select distinct A from M where A>100 or (A<50 and A>30)
对栏目A排重
select * from M where A in ('namea','nameb')
取出A=namea,A=nameb的数据
select * from M where A between '1982' and '1992'
取出1982-1992的数据
select * from M where A like 'a_z' 'a%' '%a' '%a%'_:
一个字符%:多个字符
select * from M order by A asc, B descasc
升序desc降序

select sum(A),B from M group by A having sum(A)>100如果被select的只有函数栏,不需要group by子句//avg,count,max,min,sum
select count(distinct A),B from M group by B

内部连接
select * from M,N where M.A=N.A
外部连接
where A1.A=A2.A(+)
新表数据都要
select concat(A,B) from M
A,B数据字符串串连起来
select substr(A,3,4) from M
抓出A中第3个字符开始向后抓4个字符
select trim(' sample ')'sample'
移除sample左右字符
select ltrim(' sample ')'sample '
select rtrim(' sample ')' sample'

表格处理
create table M (SID integer Unique,First_name char(50) not null,Last_name char(50),Birth_date date)
create table M (SID integer Unique)
create table M (SID integer Check (SID>0))
create table M (SID integer
Primary Key(SID))//MySQL
create table M (SID integer Primary Key)//Oracle,SQLServer
alter table M Add Primary Key (SID)//
必须先确认主键栏位not null

限制
not null
unique
不允许输入重复值
check
栏位中数据符合某些条件(未用在MySQL

create view视图名as select * from M创建视图
create index
索引名on M(A,B)为栏目AB创建索引

alter table M add C char(50)添加栏目C
alter table M change A C char(50)
修改栏目AC
alter table M modify C char(30)
修改栏目C
alter table M drop C
删除栏目C

drop table M删除表
truncate table M
清空表

insert into M (A,B) values ('a','b')插入数据
insert into M (A,B) select A,B from N where ……
NA,B数据插入M
update M set A=a,B=2 where ……
修改数据
delete from M where ……
删除数据

select A from M
union/union all/intersect/minusM,N
A栏目合并且剔除重复/不剔除重复/A字段交集(NAMA相同的数据)/M中有N中没有
select A from N

子查询
select * from M exists (select * from N where ……)
如果内查询(select * from N where ……)有数据,则执行外查询select * from M
select A ,case A
when
条件then B*2//条件可以是一个值或公式
when
条件then B*2
……
else B
end
New_B,C from MNew_B
CASE栏位的栏位名

 


TAG:

 

评分:0

我来说两句

Open Toolbar