SQL基础学习笔记【Forward】

上一篇 / 下一篇  2014-01-02 17:39:00 / 个人分类:数据库方面

数据定义
create
alter
drop

table、view、index、procedure、trigger、schema、domain

数据操作
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)为栏目A,B创建索引

alter table M add C char(50)添加栏目C
alter table M change A C char(50) 修改栏目A为C
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 ……将N表A,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字段交集(N中A与M中A相同的数据)/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栏位的栏位名

SQL基础学习笔记

SQL基础学习笔记

SQL基础学习笔记

SQL基础学习笔记


TAG:

 

评分:0

我来说两句

日历

« 2024-04-24  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 33341
  • 日志数: 73
  • 建立时间: 2013-08-01
  • 更新时间: 2014-02-17

RSS订阅

Open Toolbar