if (条件) then
SQL语句
elsif(条件) then
SQL语句
else
SQL语句
end if
Java数据类型 SQL Server Oracle
int int integer
double float float real 浮点数
Date datettime date
String varchar(n) varchar2(n)
char(n)定长 char(n)
number numeric decimal(9,3) 定点数
--表名:BUSI_TD_BOOKS1
--id,name,author,publisher,srcprice,realprice
create table BUSI_TD_BOOKS
(
id int primary key,
name varchar2(60) not null,
author varchar2(60) ,
publisher varchar2(60),
srcprice real,
realprice real
);
-- Created on 2017/2/8 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select count(*) into i from BUSI_TD_BOOKS;
if(i = 0) then
insert into BUSI_TD_BOOKS(Id,name,Author)
values(1,'凯瑟琳','123456');
end if;
end;
-- Created on 2017/2/8 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here 如果这个表没有如何记录,就添加一条记录
select count(*) into i from BUSI_TD_BOOKS;
if(i = 0) then
insert into BUSI_TD_BOOKS(id,name,Author)
values(1,'凯瑟琳','123456');
elsif(i = 1) then
insert into BUSI_TD_BOOKS(ID,name,Author)
values(2,'嘻嘻嘻','笑嘻嘻');
end if;
else
代码
end;
case 变量(必须是整数数字)
when 常量 then
语句
when 常量 then
语句
else
end case
-- Created on 2017/2/8 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here 如果这个表没有如何记录,就添加一条记录
select count(*) into i from BUSI_TD_BOOKS;
case i
when 0 then
insert into BUSI_TD_BOOKS(id,name,Author)
values(1,'凯瑟琳','123456');
when 1 then
insert into BUSI_TD_BOOKS(ID,name,Author)
values(2,'嘻嘻嘻','笑嘻嘻');
end case;
commit;
end;
Oracle怎么创建一个账号?
左边的树:选择user-->右键-->New...-->写好用户名 密码-->翻到 Role privileges-->connect 和 resource dba
-->Apply 应用
如何选择工具窗口:
1、如果是做查询 SQL Window
2、如果是做单行的增删改 SQL Window 或者 Command Window 要用分号结尾
3、如果是做多行的SQL Command Window--> Eidtor 仅限简单脚本
4、如果写复杂脚本 Test Window
4 提交或者回滚
同一个账号,可以同时重复登录,互相无干扰,各自有会话
如果执行了增删改 SQL 必须要提交才会生效Commit
如果不提交,新的数据只能自身会话看到,其他会话看不到,只能看到以前的旧数据
都是:DLL不在此 创建表(或者删除、修改)、视图、存储过程、函数、触发器、序列
create drop alter 自动提交 自动生效
5 创建序列
序列: Sequence
-- Create sequence
create sequence seq_id
minvalue 1
maxvalue 999999999
start with 1
increment by 1;
cache 20;缓存
调用seq_id.inextval 可以得到一个不断变大的值,永不重复
序列确保会得到一个不同的数字
6使用序列
Select SEQ_ID.NEXTVAL into i from dual;
查询语句 select*from dual
Oracle 的from子句一定不能少
case
when(条件) then
SQL
when(条件) then
end case;
7伪列
select t.*, t.rowid from BUSI_TD_BOOKS t
8查询数据添加新的列
select t.*,
case
when (srcprice>=50) then
'小贵'
when (srcprice>=30) then
'价格合理'
else
'便宜'
end remark
from BUSI_TD_BOOKS t
where srcprice is not null
9 显示一句话
dbms_output.put_line(a);
10 循环
死循环
Loop
exit when(条件); 条件成立的时候exit就相当于Java语言break
end loop;
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
a varchar2(100);
begin
-- Test statements here
i :=1;
loop
i:=i+1;
exit when(i>100);
a :=case
when (i>=50) then
'小贵'||to_char(i)
when (i>=30) then
'价格合理'||to_char(i)
else
'便宜'||to_char(i)
end ;
dbms_output.put_line(a);
end loop;
dbms_output.put_line('循环结束');
end;
while 循环
while 条件 loop
SQL语句
exit when(条件);
end loop;
for 循环
for i in 游标/数组
for i in 1..10 loop
SQL语句
exit when(条件);
end loop;
双重嵌套循环
1、循环体内部又有一个循环的情况,称之为双重嵌套循环。
2、exit when();语法默认情况下,只能退出内层循环。
3、外层循环可以给自己取个名字,写法是循环之前写:<<outer>>,循环结束写:end loop outer;
4、可以写exit outer when y语法,直接退出外层循环。
输出一个9*9乘法表
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
j integer;
a varchar2(100);
begin
-- Test statements here
for i in 1..9 loop
for j in 1..i loop
a := to_char(i) ||'*'||to_char(j) ||'='||to_char( i*j );
dbms_output.put(a);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop;
dbms_output.put_line('循环结束');
end;
语法糖
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
j integer;
a varchar2(100);
begin
-- Test statements here
<<outer>>
for i in 1..9 loop
for j in 1..i loop
exit outer when(j>5); --相当于java循环里面的break
a := to_char(i) ||'*'||to_char(j) ||'='||to_char( i*j );
dbms_output.put(a);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop outer;
dbms_output.put_line('循环结束');
end;
空语句
语法: null;
null语句不会执行任何操作,并且会直接传递到下一条语句。
占位用
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select pwd into i from SYSM_TD_CUSTOMER t;
exception
when no_data_found then
dbms_output.put_line('这个表没有数据,需要插入数据');
--不知道的其他异常
when others then
null;
end;
declare
定义变量 SQL要求 所有变量变量都必须在declare集中定义
begin
执行SQL代码
exception
处理异常的
when 异常名字 then
sqlcode 预置变量
SQL语句
end;
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select pwd into i from SYSM_TD_CUSTOMER t;
exception
when no_data_found then
dbms_output.put_line('这个表没有数据,需要插入数据');
end;
错误编号
NO_DATA_FOUND SQLCODE 1403 100
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select pwd into i from SYSM_TD_CUSTOMER t;
exception
--when no_data_found then
--dbms_output.put_line('这个表没有数据,需要插入数据');
--不知道的其他异常
when others then
i :=sqlcode;
dbms_output.put_line('这个表没有数据,需要插入数据'||to_char(i));
raise NO_DATA_FOUND;
end;
raise NO_DATA_FOUND;相当于throw
raise_application_error(-20001,'错误信息');
sqlerrm 报出错误的具体原因
ORA-01403: 未找到任何数据
隐式游标 写在 for循环里面的查询语句
for rec in (select distinct t.name, t.author from BUSI_TD_BOOKS t) loop
dbms_output.put_line( '书名:'||rec.name||',作者'||rec.author);
end loop;
每当执行insert update delete之后,系统会自动生成一个游标,这个游标内容是影响的行数
SQL%found 表示增删改成功,至少影响到了一行数据
SQL%notfound 表示增删改失败,没有如何记录被影响
SQL%rowcount 表示增删改操作影响到的行数
SQL%isopen 总是返回false
commit 之后就游标无效
数据字典表 系统内部预先就存在的 表或者视图 记录数据库的一些逻辑信息
all_objects 保存在SYS的账号 SYSTEM 表空间
SQL语句
elsif(条件) then
SQL语句
else
SQL语句
end if
Java数据类型 SQL Server Oracle
int int integer
double float float real 浮点数
Date datettime date
String varchar(n) varchar2(n)
char(n)定长 char(n)
number numeric decimal(9,3) 定点数
--表名:BUSI_TD_BOOKS1
--id,name,author,publisher,srcprice,realprice
create table BUSI_TD_BOOKS
(
id int primary key,
name varchar2(60) not null,
author varchar2(60) ,
publisher varchar2(60),
srcprice real,
realprice real
);
-- Created on 2017/2/8 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select count(*) into i from BUSI_TD_BOOKS;
if(i = 0) then
insert into BUSI_TD_BOOKS(Id,name,Author)
values(1,'凯瑟琳','123456');
end if;
end;
-- Created on 2017/2/8 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here 如果这个表没有如何记录,就添加一条记录
select count(*) into i from BUSI_TD_BOOKS;
if(i = 0) then
insert into BUSI_TD_BOOKS(id,name,Author)
values(1,'凯瑟琳','123456');
elsif(i = 1) then
insert into BUSI_TD_BOOKS(ID,name,Author)
values(2,'嘻嘻嘻','笑嘻嘻');
end if;
else
代码
end;
case 变量(必须是整数数字)
when 常量 then
语句
when 常量 then
语句
else
end case
-- Created on 2017/2/8 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here 如果这个表没有如何记录,就添加一条记录
select count(*) into i from BUSI_TD_BOOKS;
case i
when 0 then
insert into BUSI_TD_BOOKS(id,name,Author)
values(1,'凯瑟琳','123456');
when 1 then
insert into BUSI_TD_BOOKS(ID,name,Author)
values(2,'嘻嘻嘻','笑嘻嘻');
end case;
commit;
end;
Oracle怎么创建一个账号?
左边的树:选择user-->右键-->New...-->写好用户名 密码-->翻到 Role privileges-->connect 和 resource dba
-->Apply 应用
如何选择工具窗口:
1、如果是做查询 SQL Window
2、如果是做单行的增删改 SQL Window 或者 Command Window 要用分号结尾
3、如果是做多行的SQL Command Window--> Eidtor 仅限简单脚本
4、如果写复杂脚本 Test Window
4 提交或者回滚
同一个账号,可以同时重复登录,互相无干扰,各自有会话
如果执行了增删改 SQL 必须要提交才会生效Commit
如果不提交,新的数据只能自身会话看到,其他会话看不到,只能看到以前的旧数据
都是:DLL不在此 创建表(或者删除、修改)、视图、存储过程、函数、触发器、序列
create drop alter 自动提交 自动生效
5 创建序列
序列: Sequence
-- Create sequence
create sequence seq_id
minvalue 1
maxvalue 999999999
start with 1
increment by 1;
cache 20;缓存
调用seq_id.inextval 可以得到一个不断变大的值,永不重复
序列确保会得到一个不同的数字
6使用序列
Select SEQ_ID.NEXTVAL into i from dual;
查询语句 select*from dual
Oracle 的from子句一定不能少
case
when(条件) then
SQL
when(条件) then
end case;
7伪列
select t.*, t.rowid from BUSI_TD_BOOKS t
8查询数据添加新的列
select t.*,
case
when (srcprice>=50) then
'小贵'
when (srcprice>=30) then
'价格合理'
else
'便宜'
end remark
from BUSI_TD_BOOKS t
where srcprice is not null
9 显示一句话
dbms_output.put_line(a);
10 循环
死循环
Loop
exit when(条件); 条件成立的时候exit就相当于Java语言break
end loop;
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
a varchar2(100);
begin
-- Test statements here
i :=1;
loop
i:=i+1;
exit when(i>100);
a :=case
when (i>=50) then
'小贵'||to_char(i)
when (i>=30) then
'价格合理'||to_char(i)
else
'便宜'||to_char(i)
end ;
dbms_output.put_line(a);
end loop;
dbms_output.put_line('循环结束');
end;
while 循环
while 条件 loop
SQL语句
exit when(条件);
end loop;
for 循环
for i in 游标/数组
for i in 1..10 loop
SQL语句
exit when(条件);
end loop;
双重嵌套循环
1、循环体内部又有一个循环的情况,称之为双重嵌套循环。
2、exit when();语法默认情况下,只能退出内层循环。
3、外层循环可以给自己取个名字,写法是循环之前写:<<outer>>,循环结束写:end loop outer;
4、可以写exit outer when y语法,直接退出外层循环。
输出一个9*9乘法表
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
j integer;
a varchar2(100);
begin
-- Test statements here
for i in 1..9 loop
for j in 1..i loop
a := to_char(i) ||'*'||to_char(j) ||'='||to_char( i*j );
dbms_output.put(a);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop;
dbms_output.put_line('循环结束');
end;
语法糖
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
j integer;
a varchar2(100);
begin
-- Test statements here
<<outer>>
for i in 1..9 loop
for j in 1..i loop
exit outer when(j>5); --相当于java循环里面的break
a := to_char(i) ||'*'||to_char(j) ||'='||to_char( i*j );
dbms_output.put(a);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop outer;
dbms_output.put_line('循环结束');
end;
空语句
语法: null;
null语句不会执行任何操作,并且会直接传递到下一条语句。
占位用
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select pwd into i from SYSM_TD_CUSTOMER t;
exception
when no_data_found then
dbms_output.put_line('这个表没有数据,需要插入数据');
--不知道的其他异常
when others then
null;
end;
declare
定义变量 SQL要求 所有变量变量都必须在declare集中定义
begin
执行SQL代码
exception
处理异常的
when 异常名字 then
sqlcode 预置变量
SQL语句
end;
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select pwd into i from SYSM_TD_CUSTOMER t;
exception
when no_data_found then
dbms_output.put_line('这个表没有数据,需要插入数据');
end;
错误编号
NO_DATA_FOUND SQLCODE 1403 100
-- Created on 2017/2/9 by ADMINISTRATOR
declare
-- Local variables here
i integer;
begin
-- Test statements here
select pwd into i from SYSM_TD_CUSTOMER t;
exception
--when no_data_found then
--dbms_output.put_line('这个表没有数据,需要插入数据');
--不知道的其他异常
when others then
i :=sqlcode;
dbms_output.put_line('这个表没有数据,需要插入数据'||to_char(i));
raise NO_DATA_FOUND;
end;
raise NO_DATA_FOUND;相当于throw
raise_application_error(-20001,'错误信息');
sqlerrm 报出错误的具体原因
ORA-01403: 未找到任何数据
隐式游标 写在 for循环里面的查询语句
for rec in (select distinct t.name, t.author from BUSI_TD_BOOKS t) loop
dbms_output.put_line( '书名:'||rec.name||',作者'||rec.author);
end loop;
每当执行insert update delete之后,系统会自动生成一个游标,这个游标内容是影响的行数
SQL%found 表示增删改成功,至少影响到了一行数据
SQL%notfound 表示增删改失败,没有如何记录被影响
SQL%rowcount 表示增删改操作影响到的行数
SQL%isopen 总是返回false
commit 之后就游标无效
数据字典表 系统内部预先就存在的 表或者视图 记录数据库的一些逻辑信息
all_objects 保存在SYS的账号 SYSTEM 表空间