SQL基础学习笔记

上一篇 / 下一篇  2010-06-07 10:39:26 / 个人分类:数据库

数据定义
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 desc  asc升序 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/minus   M,N的A栏目合并且剔除重复/不剔除重复/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 M  New_B为CASE栏位的栏位名

 


TAG: 数据修改 数据查询 数据定义 数据库 SQL sql语句 SQL语句 表结构

引用 删除 轨迹   /   2010-08-27 16:35:54
calla181的个人空间 引用 删除 calla181   /   2010-08-24 15:39:05
 

评分:0

我来说两句

guobin_it

guobin_it

软件测试技术交流群60926703 (已满) CDN流媒体测试技术交流群126760166 (未满)

日历

« 2024-04-24  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 108180
  • 日志数: 57
  • 建立时间: 2007-12-14
  • 更新时间: 2011-07-07

RSS订阅

Open Toolbar