Oracle基础

索引

一种以快速访问满足条件的数据
 - 是一个表中用简单的  语句创建再一个或多个列上的
 - 索引与创建的表分离,可删除不影响表数据,但表删除索引也删除

DDl(Data definition Language/数据定义语言)

 - create table/index
		create table user_info
		(
			userId varchar2(10) not null,
			userName varchar2(30) not null,
			userAge varchar2(3),
			userAdress varchar2(255),
			userSex varchar2(4) not null
		)
		
 - alter table
 - drop table
 - grant 将权限/角色授予用户/角色
 - truncate 删除表中所有行
 - revoke 撤销用户/角色权限

DML(Data manipulation Language/数据操纵语言)

 - select
 - insert
		insert into user_info
			(userId, userName, userAge, userSex)
		values
			('1', '小红', '12', '女'),
			('3', '小兰', '11', '女'),
			('2', '小明', '11', '男'),
			('4', '小刚', '13', '男');
 - update table set column where ?
 - delete from table where ?

数据排序

 - order by column desc/asc
		select u.* from user_info u order by u.userAge desc;

字符函数

 - lower(char) 转换为小写
 - upper(char) 大写
 - initcap(char)char中每个单元的第一个字符大写

 - replace(char,str1,str) char中str1替换成str2

 - substr(char,m,n) 截取char

 - length(char) 获取字符长度

 - rpad(exp1,n,exp2) 在exp1右边补充n个exp2
 - lpad(exp1,n,exp2) 在exp1左边补充n个exp2
 
 - trim(str)

数值函数

 - ceil(n) 返回大于或等于n的最近整数
 - floor(n) 返回小于或等于n的最近整数
 - round(n,m) n四舍五入,保留小数点后m位
 - power(m,n) m的n次幂
 - mod(m,n) 返回m除以n的余(n=0,返回0 / n>m,返回m)
 - sqrt(n) 返回n的平方根

聚合函数

 - count(expr) 
 - avg(x) 平均值
 - sum()
 - min()
 - max()

日期函数

 - sysdate 返回系统日期(to_char)
 - last_day(date) 返回日期所在月份的最后一天
 - add_months(d,n) 在d上增加n个月
 - months_between(d1,d2) 返回日期d1/d2间的相差月数
 - next_day(d,day) 返回日期d之后day天后对应的日期
 - current_timestamp 返回当前时区的当前时间戳
	 
	 - Y/YY/YYY
	 - YEAR 全文拼写的年(英文)
	 - Q 季度
	 - MM 月份
	 - Month 月份(英文)
	 - WW 年第几周
	 - W 月第几周
	 - DDD 年第几天
	 - DD 月第几天
	 - D 周第几天

连接(内/外/自)

 - 内连接
select a.*,b.* from a join b on a.id = b.id
select a.*,b.* from a join b using(id)
 - 外连接
select a.* from a left join b on a.id = b.id
select a.* from a right join b on a.id = b.id
select a.* from a full join b on a.id = b.id

分组

 - group by	对函数结果分组(必须有聚合函数)
 - having	对函数结果做选择 

子查询(嵌套查询)

嵌套select
		select * from user_info u where u.id = (
			select id = from user where name = ?
			)
 - 集合操作符
	 - union 返回两表中无任何重复的所以行
		select * from a
		union
		select * from b;
	 - union all 返回表中所有行
	 - intersect 返回两表中都存在的行
	 - minus 返回a表减去b表中相同行的剩余行

视图

基于一个或多个表的数据库对象
create view viewTest
	 as
	 select column...
	 from table
	 where ?
	 group by column;

序列

create sequence [schema.]sequence_name
	[ start with i ] [ increment by i ]
	[ maxvalue n ] [ nomaxvalue ]
	[ minvalue m ] [ nominvalue ]
	[ cycle | nocycle ] [ cache p | nocache ] 
sequence_name:序列名,创建在schema下
i:第一个序列
j:步进
m:最大值
n:最小值
cycle:达到最大/最小值后是否继续生成序列号
cache:先预取p个数据在缓存中,提高序列值的生成效率
	
 - select seq_test.NEXTVAL from dual;获取序列下一个值
 - select seq_test.CURRVAL from dual;获取序列当前值
 - drop sequence seq_test;

约束

 - null 非空
 - unique 唯一,可以有一个空
		alter table user_info add constraint un_userId unique(user_id);
 - primary key 主键,非空
		alter table user_info add constraint pk_userId primary key(user_id);
 - foreign key 外键
		alter table user add constraint fk_id foreign key(id) references user_info(userId); 
 - check 定义值可选内容
		alter table user_info add constraint ck_age check(userAge>0 and userAge<100);

PL/语法

declare
	<declarations section>
begin
	<executable command>
exception
	<exception handling>
end;
 - 		declare
			message varhchar2(20):= 'Hello,World';
		begin
			dbms_output.put_line(message);
		end;

DECLARE
   -- constant declaration
   pi constant number := 3.141592654;
   -- other declarations
   radius number(5,2); 
   dia number(5,2); 
   circumference number(7, 2);
   area number (10, 2);
BEGIN 
   -- processing
   radius := 9.5; 
   dia := radius * 2; 
   circumference := 2.0 * pi * radius;
   area := pi * radius * radius;
   -- output
   dbms_output.put_line('Radius: ' || radius);
   dbms_output.put_line('Diameter: ' || dia);
   dbms_output.put_line('Circumference: ' || circumference);
   dbms_output.put_line('Area: ' || area);
END;

结果:
Radius: 9.5
Diameter: 19
Circumference: 59.69
Area: 283.53

case语句

	case variable
		when exp1 then val1
		when exp2 then val2
		else val3
	end;

if then

	IF (a <= 20) THEN
	   c:= c+1;
	END IF;

if then else

	if color = red then
		c:= red;
	else
		c:= blue;
	end if;

if then elseif

	if(bool 1) then
		s1;
	elseif(bool 2) then
		s2;
	elseif(bool 3) then
		s3;
	else
		s4;
	end if;

loop

	for i in 1..3 loop
		dbms_output.put _line(i);
	end loop inner_loop;

----------

	loop 
		dbms_output.put_line(x);
		x := x + 10;
		if x > 50 then
			exit;
		end if;
	end loop;

while

	while a < 20 loop
		dbms_output.put_line(a);
		a := a + 1;
	end loop;

for

	for a in 10 .. 20 loop
		dbms_output.put_line(a);
	end loop;
	
----------

	反转for(倒序输出)
	for a in 10 .. 20 loop
		dbms_output.put_line(a);
	end loop;

存储过程

	create [or replace] procedure procedure_name
	[(parameter_name [in | out | in out] type [, ...])]
	{is | as}
	begin
		< procedure_body >
	end procedure_name;

----------

	create or replace procedure greetings
	as
	begin
		dbms_output.put_line('Hello World');
	end;

----------

	执行 procedure
	execute greetings;

##待完善…


学习书籍:****Oracle Database 11g初学者指南

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值