重点:这里包含了我日常用(基本)的sql语句,大家可以复制黏贴出来,在文本进行搜索来查找自己所需要的语句来使用。
SQL支持下列类别的命令:
数据定义语言(DDL):create alter drop
数据操纵语言(DML):insert select update delete
事务控制语言(TCL):commit savepoint rollback
数据控制语言(DCL):grant revoke
Oracle数据类型:
创建表时,必须为各个列制定数据类型。
以下是Oracle数据类型的类别:
字符、数值、日期类型、RAW/LONG RAW LOB。
字符数据类型:char varchar2 long
当需要固定长度的字符串时,使用char数据类型。
char数据类型存储字母数字值。
char数据类型的列长度可以是1到2000的字节。
VARCHAR2数据类型支持可变长度字符串
VARCHAR2数据类型存储字母数字值
VARCHAR2数据类型的大小在1至4000个字节范围内
Long数据类型存储可变长字符数据。
Long数据类型最多可存储2GB.
数值数据类型:
可以存储整数,浮点数和实数。
最高精度38位,范围:负的10的38次方到正的10的38次方。
数值数据类型声明的语法:
NUMBER[(p[,s])]
P表示精度,S表示小数点的位数。
日期时间数据类型
用于存储日期和时间值,包括年、月日,小时,分钟,秒。
主要的日期类型有:
DATE-存储日期和时间部分,精确到整个秒。
TIMESTAMP-存储日期,时间和时区信息,秒值精确到小输掉后6位。
RAW 数据类型用于存储二进制数据
RAW 数据类型最多能存储 2000 字节
LONG RAW 数据类型用于存储可变长度的二进制数据
LONG RAW 数据类型最多能存储 2 GB
LOB 称为“大对象”数据类型,可以存储多达 128TB 的非结构化信息,例如声音剪辑和视频文件等(LOB类型的容量从原来的4G增加到了最大128T)
LOB 数据类型允许对数据进行高效、随机、分段的访问
CLOB 即 Character LOB(字符 LOB),它能够存储大量字符数据
BLOB 即 Binary LOB(二进制 LOB),可以存储较大的二进制对象,如图形、视频剪辑和声音文件
BFILE 即 Binary File(二进制文件),它用于将二进制数据存储在数据库外部的操作系统文件中
其他数据类型:
Oracle 中伪列就像一个表列,但是它并没有存储在表中
伪列可以从表中查询,但不能插入、更新和删除它们的值
常用的伪列有ROWID和ROWNUM
ROWID 是表中行的存储地址,该地址可以唯一地标识数据库中的一行,可以使用 ROWID 伪列快速地定位表中的一行
ROWNUM 是查询返回的结果集中行的序号,可以使用它来限制查询返回的行数
常用操作:
1 创建表:
create table student(sno number(6),sname varchar2(10),birthday date);
2 添加列:
alter table student add column tele varchar2(11);// 报错
alter table student add tele varchar2(11);//正确;
3 查看表结构:
desc student;
4 修改表结构
alter table studnet modify tele varchar(20);
5 删除列:
alter table student drop column tele;
6 查看表结构:
desc student;
7 插入记录:
insert into student values(1,'A','2010-5月-3');
oracle插入时间的格式是这样的,可以通过以下方式修改时间格式
8 修改数据库当前会话的时间格式:
alter session set nls_date_format='时间格式';
eg:alter session set nls_date_format='yyyyMMdd';
9 查询系统时间格式:
select sysdate from dual;
10 再次插入记录:
insert into student values(2,'B','19800211');
11 在此插入记录:
insert into student values(3,null,to_date('19810223','yyyymmdd');
12 查询表
select * from student;
13 插入null数据
insert into student(sno,birthday) values(4,to_date('19230323','yyyymmdd'));
14 第二种方式插入null数据
insert into student(sno,birthday) values(4,to_date('12342423','yyyymmdd'));
15 查询学生表中姓名为空的行
select * from student where sname is null;// 唯一方式
___________________________________________________
16 创建student2,并复制student表的数据
create table student2 as select * from student;
17 创建student3,仅需student的表结构,而不包含数据
create table student3 as select * from student where 1>2;
18 select * from student3;
19 desc student3;
-----------------------------------------------------------------------------------------
20 清空表数据
truncate table student2;
或者:
delete from student2;
commit;
// 与truncate不同之处是,还需要添加一个commit语句,同时也会将删除操作写入日志当中。
21 另外的insert方法:
sqlserver实现:
insert into student2 select * from student;
oracle的实现:
insert into student2 select * from student;
-------------------------------------------------------------------------------------------------
22 update的语句
update student2 set sname='AA' where sno = 3;
23 删除语句
delect student where sno = 4;
24 查询结果集:
select count(*) from student2;
25 select count(*) from student2;
26 select count(*) from student2 where sname is not null;
27 sql不区分大小写,指的是语句不区分,但是存储的数据是区分大小写的。
select * from student2 where sname='A';
28 转换大写:
select * from student2 where upper(sname)='A';
29 select * from student2;
30 插入特殊格式的字符
update student2 set sname='A''B' where sno=4;
31 查询插入特殊字符结果集
select * from student2;
32 在oracle语句中,like关键字也仅有两种用法%和_:百分号和下划线
select * from student2 where sname like 'A%';
33 更新内容:
update student2 set sname='张三' where sno=1;
update student2 set sname='张三三' where sno=2;
34 like查询:
select * from student2 where sname like '张_';
select * from student2 where length(sname)=2;
35 order by操作:
select * from student2 order by sno desc,sname;
select * from student2 order by 1 desc,2;
36 使用别名
select sno 学号 ,sname “姓名” from student;
37 若别名中有空格,用双引号
select sno "学 号" from student;
---------------------------------------------------------------------------------------------------------
38 创建成绩表
create table 成绩(sno n umber,km varchar(10),score number);
38 插入数据:
insert into 成绩 values(1,'语文',60);
insert into 成绩 values(1,'数学',69);
insert into 成绩 values(1,'英语',70);
insert into 成绩 values(2,'语文',70);
insert into 成绩 values(2,'数学',80);
insert into 成绩 values(3,'语文',88);
然后commit;提交
39 查看成绩表:
select * from 成绩;
40 进行分组,得到每一名学生的总分:
select sno,sum(score) from 成绩 group by sno;
41 得到每门课程的平均成绩:
select km,avg(score) from 成绩 group by km;
42 得到 平均成绩 大于 60 的学生的 学号 和 成绩
select sno,avg(score) from 成绩 group by sno having(avg(score)>60);
43 查出 平均成绩 大于 所有学生的 平均成绩 的 学生的 学号 和 平均成绩
select sno,avg(score) from 成绩 group by sno having(avg(score)>(select avg(score) from 成绩));
44 查出 平均成绩 大于 60 的 学生的学号 和 平均成绩,并按照学号的降序排列(说明了 几个 谓词的顺序);
select sno,avg(score) from 成绩 group by sno having(avg(score)>(select avg(score) from 成绩)) order by sno desc;
45 多表连接查询
drop talbe t1 purge;
drop table t2 purge
drop table t3 purge;
select * from tab;// 查看数据库中还有多少张表
drop table t4 purge;
drop table 成绩 purge;
select * from tab;
重新建立四张表
查看表内容
select * from student;
select * from address;
select s.sno,s.name,s.sno,a.zz from student s,address a where s.sno=a.sno// 等值连接
select s.nsno,s.sname,a.sno,a.zz from student s inner join address a on s.sno=a.sno// 第二种方式等值连接
select s.sno,s.sname,a.sno,a.zz from student s,address a where s.sno=a.sno(+);// 左外连接的第一种方式的写法
select s.sno,s.sname,a.sno,a.zz from student s left outer join address a on s.sno=a.sno;// 左外连接的第二种方式
select s.sno,s.sname,a.sno,a.zz from student s right outer join address a on s.sno=a.sno;// 右外连接
46 集合操作符
clear screen;
select * from student;
select * from student
union
select * from student where sno=1;
select * from student;
union all
select * from student where sno=1;
select * from student
minus
select * from student where sno=1;
select * from student wehre sno=1
minus
select * from student;
select * from student
intersect
select * from student where sno=1;
---------------------------------------------------------------------------------------------
47 表重命名
rename student to newstudent;
select * from tab;// 查看表
desc nesstudent;
alter table newstudent rename column sno to newsno;
----------------------------------------------------------------------------------
48 sql语句执行顺序:
常见的select、from、where的顺序:
1, from 2, where 3, select
完整的select、from、where、group by、having、order by的顺序:
1, from 2, where 3, group by 4,having 5, select 6, order by
---------------------------------------------------------------------------------------
49 两个表连接查询
select s.sno,s.sname,a.sno,a.zz from student s,address a ;// 无where条件,则是笛卡尔乘积;
// 在此结果上就可以设置连接查询条件
select s.sno,s.sname,a.sno,a.zz from student s,address a where s.sno=a.sno;
-----------------------------------------------------------------------------
50 子查询的使用
drop table student purge;
drop table address purge;
create table student(sno number(6) ,birthday date, sname varchar2(10));
insert into student values(1, '11-1月-81' , '张三');
insert into student values(2, '10-3月-82' , '李四');
insert into student values(3, '06-1月-83' , '王五');
insert into student values(4, '26-1月-83' , '赵六');
create table address(sno number(6) , zz varchar2(10));
insert into address values(1, '郑州');
insert into address values(2, '开封');
insert into address values(3, '洛阳');
insert into address values(4, '郑州');
select * from student;
select * from address;
// 找出zz是郑州的学生中,sno最大的学生的sname。
select sname from student where sno=(select max(sno) from address where zz='郑州');
------------------------------------------------------------------------------------
51 group by 的练习
drop table student purge;
create table student(xh number,xm varchar2(10),nl int);
insert into student values (1,'A',21);
insert into student values (2,'B',22);
insert into student values (3,'A',23);
insert into student values (4,'A',24);
insert into student values (5,'A',25);
insert into student values (6,'C',26);
insert into student values (7,'B',27);
select xm,count(*) from student group by xm;
select xm,count(*) from student group by xm having(count(*)>1);
-----------------------------------------------------------------------------------------------------
52 exists的使用
Exists用来判断查询所得到的结果中,是否有满足条件的记录存在?
eg:select * from student where exists(select * from address where zz='郑州');
从select 、from 、 where 三者的先后执行顺序来分析。
---------------------------------------------------------------------------------------------------
53 自连接的使用:
CREATE TABLE 管理人员 (
编号 char(10) ,
姓名 varchar2(10) ,
管理人员编号 char(10)
);
insert into 管理人员 values('001', '张一', '004');
insert into 管理人员 values('002', '张二', '004');
insert into 管理人员 values('003', '张三', '003');
insert into 管理人员 values('004', '张四', '004');
select * from 管理人员;
select * from 管理人员 a,管理人员 b;// 从中找出规律 where条件
select a.编号,a.姓名,b.姓名 from 管理人员 a,管理人员 b where a.管理人员编号=b.编号;
-------------------------------------------------------------------------------
54 select case when的使用
语法:
CASE
WHEN 条件1 THEN action1
WHEN 条件2 THEN action2
WHEN 条件3 THEN action3
…..
ELSE actionN
END CASE
示例:
select case
when substr('20090310',5,2) = '01' then '一月份'
when substr('20090310',5,2) = '02' then '二月份'
when substr('20090310',5,2) = '03' then '三月份'
when substr('20090310',5,2) = '04' then '四月份'
else null
end
from dual;
操作:
// 创建成绩表:
create table 成绩(sno number, km varchar2(10), score number,grade char(6));
insert into 成绩 values(1, '语文', 65,null);
insert into 成绩 values(2, '数学', 76,null);
insert into 成绩 values(3, '英语', 86,null);
insert into 成绩 values(4, '语文', 94,null);
select * from 成绩;
update 成绩 set grade = (
select grade from (
select sno ,
case when score >= 90 then '优秀'
when score >= 80 then '良好'
when score >= 70 then '中等'
when score >= 60 then '及格'
else '不及格'
end grade
from 成绩
) a
where 成绩.sno = a.sno );
--------------------------------------------------------------------------------------
55 复杂更新语句的使用
create table T1(a int ,b int ,c int ,d int ,e int);
create table T2(a int ,b int ,c int );
insert into T1 values(1,2,3,4,5);
insert into T1 values(10,20,3,4,5);
insert into T1 values(10,20,4, 40,50);
insert into T2 values( -1, -1 , 3);
insert into T2 values( -2, -2, 4);
--------------------------------------------------------------------------------------
sql的操作符
1 连接操作符:||
select '学号是:'||sno ||'姓名是:'sname from student2;
2 操作符的优先级:
算术操作符 ------最高优先级
连接操作符
比较操作符
NOT逻辑操作符
AND逻辑操作符
OR 逻辑操作符 ------最低优先级
oracle中的函数
分为三部分:
单行函数
分组函数
分析函数
单行函数:
单行函数对于从表中查询的每一行只返回一个值。
可以出现在select子句中和where子句中。
单行函数可划分为:
字符函数
日期时间函数
日期函数对日期值进行运算,并声称日期数据类型或数值类型的结果。
日期函数包括:
ADD_MONTHS
MONTHS_BETWEEN
LAST_DAY
ROUND
NEXT_DAY
TRUNC
EXTRACT
实例:
select add_months(sysdate,5) from dual;
select moths_between(sysdate,to_date('20100605','yyyymmdd')) from dual;
select extrace(year from sysdate) from dual;// 提取年
select extrace(month from sysdate) from dual;// 提取月
select extract(day from sysdate) from dual;// 提取日
select last_day(sysdate) from dual;
新创建一个student表,将原student表永久删除:
drop table student purge;
创建student表:
create table student(sno number(6),birthday date,sname varchar2(10));
insert into student values(1,'11-1月-81','张三');
insert into student values(2,'10-3月-22',‘李四’);
insert into student values(3,'06-1月-33',‘王五’);
commit;
select * from student;
出生日期向后一年的日期:
select sno,add_months(birthday,12) from student;
select * from student where add_months(bitthday,27*12) < sysdate;
从出生到现在有几天:
select (sysdate - birthday) from student;
select floor(sysdate-birthday) from student;
select floor(4.67) from dual;
获取到出生月份的倒数的20天:
select * from student where last_day(birthday)-20=birthday;
数字函数
round和trunc在日期中的作用
转换函数
转换函数将值从一种数据类型转换为另一种数据类型。
常用的转换函数有:
to_char
to_date
to_number
select to_date("2015-12-06","yyyy-mm-dd") from dual;
select to_number("100") from dual;
select to_char(sysdate,'yyyy''年''fmMM''月''fmDD''日''HH24:MI:SS') from dual;
select to_char(itemrate,'C99999') from itemfile;
常用与转换控制的函数
NVL,第一为空返回二;否则返回一。
NVL2,第一个不空则返回二;否则返回三。
NULLIF,两个表达式,相等则返回空;否则第一个。
分组函数:
分组函数基于一组行来返回结果:
为每一组行返回一个值:
avg,min,max,sum,count
select avg(re_level) from itemfile where p_category='accessories';
select max(max_level) from itemfile;
select sum(itemrate*max_level) from itemfile;
select count(*) from itemfile;
select count(itemrate) from itemfile;
select count(distinct qty_hand) from itemfile;
Group BY和HAVING的子句
GROUP BY子句
用于将信息划分为更小的组。
每一组行返回针对该组的单个结果。
HAVING子句
用于指定GROUP BY 子句检索行的条件
select sno,max(score) as 最高分,sum(score) as 总分 from 成绩 group by sno;