--简单的select语句
select deptno,dname,loc from DEPT where deptno='40';
--描述表结构 部门表
desc dept;
--雇员表
desc emp;
--asc 升序排列(默认) desc 降序排列
select empno,ename,job,mgr,to_char(HIREDATE,'yyyy-mm-dd') as 入职日期,sal,comm,deptno from emp order by empno desc;
--通过系统函数获取年份
select to_char(sysdate,'yyyy')from dual;
--通过系统函数获取月份
select to_char(sysdate,'mm')from dual;
--通过系统函数获取天
select to_char(sysdate,'dd')from dual;
--通过系统函数获取季度
select to_char(sysdate,'q')from dual;
--通过系统函数获取周 每年有52周或53周
select to_char(sysdate,'iw')from dual;
--创建custormes表
create table customers

cust_id int not null,
cust_name char(50) not null,
cust_address char(50) null,
cust_city char(50) null,
cust_state char(5) null,
cust_zip char(10) null,
cust_country char(50) null,
cust_contact char(50) null,
cust_email char(255) null
);
--创建orders表
create table orders
(
order_num int not null,
order_date date not null,
cust_id int not null
);
--创建vendors表
create table vendors
(
vend_id int not null,
vend_name char(50) not null,
vend_address char(50) null,
vend_city char(50) null,
vend_state char(5) null,
vend_zip char(10)null,
vend_country char(50)null
);
--创建orderitems表
create table orderitems
(
order_num int not null,
order_item int not null,
prod_id char(10) not null,
quantity int default 1 not null,
item_price decimal(18,2)not null
);
select * from vendors;
--操作表 更新表
--向表中增加一个元素
alter table vendors add vendor_phone char(50);
alter table vendors drop column vendor_phone;
--主键操作
alter table customers add constraint pk_customers primary key (cust_id);
alter table orderitems add constraint pk_orderitems primary key (order_num,order_item);
alter table orders add constraint pk_orders primary key (order_num);
alter table vendors add constraint pk_vnedors primary key (vend_id);
--定义外键 Define Foreign key
alter table orderitems add constraint fk_orderitems_orders foreign key (order_num)references orders (order_num);
alter table orders add constraint fk_orders_customers foreign key(cust_id)references customers (cust_id);
--删除表
drop table tablename;
--重命名表名
alter table tablename1 rename to tablename2;
--删除数据表
select * from orders;
select * from vendors;
select * from customers;
select * from orderitems;
select * from products;
select * from productnotes;

--视图操作
create view vw_productscustomers as
select a.cust_name,a.cust_contact,c.PROD_ID
from customers a ,orders b ,orderitems c
where a.cust_id=b.cust_id
and c.order_num=b.order_num;

--用户表视图
select * from user_tables;
select table_name from user_tables;
select * from emp,dept;
select DEPTNO,ROWID
from dept;
--抛异常 报错
set serveroutput on;
declare
a int:=100;
b int:=100;
c number;
begin
c:=(a+b)/(a+b);
dbms_output.put_line('计算结果是'||c);
exception
when zero_divide then
dbms_output.put_line('除数是不能为零的!');
end;
select * from orders where CUST_ID='10001';
--cust_id 10001 order_num 20005
declare
var_cust_id char(20);
var_order_num char(20);
begin
select cust_id,order_num into var_cust_id,var_order_num
from orders where cust_id='10003';
dbms_output.put_line('cust_id是'||var_cust_id||'order_num是'||var_order_num);
end;

--3中特殊的数据类型
--1 %type 类型
declare
var_cust_id orders.cust_id%type;
var_order_num orders.order_num%type;
begin
select order_num,cust_id into var_order_num,var_cust_id
from orders where cust_id='10003';
dbms_output.put_line('var_cust_id'||var_cust_id||' var_order_num'||var_order_num);
end;

--record类型
select to_char(sysdate,'yyyy-mm-dd')from dual;
set serveroutput on
declare
type emp_type is record
(
var_ename varchar2(20),
var_job varchar2(20),
var_sal number
);
empinfo emp_type;
begin
select ENAME,job,sal
into empinfo
from emp
where empno='7369';
DBMS_OUTPUT.PUT_LINE('1'||empinfo.var_ename||'2'||empinfo.var_job||'3'||empinfo.var_sal);
end;
--%rowtype 类型 结合了%type和record的特点
declare
rowvar_emp emp%rowtype;
begin
select *
into rowvar_emp
from emp
where empno='7369';
/*输出检索到的信息*/
DBMS_OUTPUT.PUT_LINE('1 '||rowvar_emp.ename||' 2 '||rowvar_emp.empno||' 3 '||rowvar_emp.job);
end;
--2018-03-26 21:24:48 定义变量和常量
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
declare
var_countryname varchar2(50):='中国';--定义变量
con_day constant integer:=365;--定义常量

--流程控制语句
set SERVEROUTPUT ON
declare
var_name1 varchar2(50);
var_name2 varchar2(50);
begin
var_name1:='East';
var_name2:='xiaoke';
if length(var_name1)<length(var_name2)then
dbms_output.put_line('123');
end if;
end;
--if then else end if;
declare
age int:=55;
begin
if age>=56 then
dbms_output.put_line('1');
else
dbms_output.put_line('2');
end if;
end;
--if elsif ...else end if;
declare
month int:=20;
begin
if month>=0 and month<=3 then
dbms_output.put_line('1');
elsif month>=4 and month<=6 then
dbms_output.put_line('2');
elsif month>=7 and month<=9 then
dbms_output.put_line('3');
elsif month>=10 and month<=12 then
dbms_output.put_line('4');
else
dbms_output.put_line('数据不合法,请检查!!!');
end if;
end;
--cast 语句
declare
season int:=3;
aboutinfo varchar2(50);
begin
case season
when 1 then aboutinfo:=season||'季度';
when 2 then aboutinfo:=season||'季度';
when 3 then aboutinfo:=season||'季度';
when 4 then aboutinfo:=season||'季度';
else
aboutinfo:=season||'季节不合法!';
end case;
dbms_output.put_line(aboutinfo);
end;
--循环语句 主要包括3种循环语句 loop while for
--使用loop求前100的和 会先执行一次循环体,然后判断exit when 后面的条件表达式值是否是true或者false 如果true则跳出循环体,false继续循环
declare
sum_i int:=0;
i int:=0;
begin
loop
i:=i+1;
sum_i:=sum_i+i;
exit when i=100;
end loop;
dbms_output.put_line(sum_i);
end;
--while 语句 求得前100的和
declare
sum_i int:=100;
i int:=0;
begin
while i<=99 loop
i:=i+1;
sum_i:=sum_i+i;
end loop;
dbms_output.put_line(sum_i);
end;
--使用for 语句求得前100的和
declare
sum_i int:=0;
i int:=0;
begin
for i in reverse 1..100 loop
sum_i:=sum_i+i;
end loop;
dbms_output.put_line(sum_i);
end;
--游标操作
set serveroutput on
declare
/*声明游标 检索雇员信息*/
cursor cur_emp(var_job in varchar2:='salesman')
is select empno,ename,sal
from emp
where job=var_job;
type record_emp is record
(
/*定义当前的成员变量*/
var_empno emp.empno%type,
var_ename emp.ename%type,
var_sal emp.sal%type
);
emp_row record_emp;
begin
open cur_emp('MANAGER');
fetch cur_emp into emp_row;
while cur_emp%found
loop
dbms_output.put_line(emp_row.var_ename||'的编号是'||emp_row.var_empno||',工资是'||emp_row.var_sal);
fetch cur_emp into emp_row;
end loop;
close cur_emp;
end;
--2018-03-27 22:51:46
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')from dual;
set serveroutput on
declare
var_ename varchar2(20);
var_job varchar2(50);
cursor cur_emp
is select ename,job from emp where empno=7599;
begin
open cur_emp;
fetch cur_emp into var_ename,var_job;
if cur_emp%found then
dbms_output.put_line('编号是7499的雇员名称是'||var_ename||',职务是:'||var_job);
else
dbms_output.put_line('无数据记录');
end if;
end;
--隐式游标
begin
update emp
set sal=sal*(1+0.2)
where job='SALESMAN';
IF sql%notfound then
dbms_output.put_line('没有雇员工资需要上调');
else
dbms_output.put_line('有'||sql%rowcount||'个雇员工资上调20%');
end if;
end;
select * from emp;
--for 语句和游标结合使用
declare
cursor cur_emp is select *from emp where deptno=30;
begin
for emp_record in cur_emp loop
dbms_output.put('雇员编号:'||emp_record.empno);
dbms_output.put('雇员名称:'||emp_record.ename);
dbms_output.put_line('雇员职务:'||emp_record.job);
end loop;
end;

Oracle 练习的更多相关文章

  1. Oracle分析函数入门

    一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...

  2. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 5.安装Database软件 5. ...

  3. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 1.实施前准备工作 1.1 服务器安装操 ...

  4. Oracle 的基本操作符

    != 不等于 select empno,ename,job from scott.emp where job!='manager' ^= 不等于 select empno,ename,job from ...

  5. 使用Zabbix监控Oracle数据库

    Orabbix介绍 监控Oracle数据库我们需要安装第三方提供的Zabbix插件,我们先测试比较有名的Orabbix,http://www.smartmarmot.com/product/orabb ...

  6. 基于Oracle安装Zabbix

    软件版本 Oracle Enterprise Linux 7.1 64bit Oracle Enterprise Edition 12.1.0.2 64bit Zabbix 3.2.1 准备工作 上传 ...

  7. Oracle Database 12c Data Redaction介绍

    什么是Data Redaction Data Redaction是Oracle Database 12c的高级安全选项之中的一个新功能,Oracle中国在介绍这个功能的时候,翻译为“数据编纂”,在EM ...

  8. 使用Oracle官方巡检工具ORAchk巡检数据库

    ORAchk概述 ORAchk是Oracle官方出品的Oracle产品健康检查工具,可以从MOS(My Oracle Support)网站上下载,免费使用.这个工具可以检查Oracle数据库,Gold ...

  9. 利用Oracle RUEI+EM12c进行应用的“端到端”性能诊断

    概述 我们知道,影响一个B/S应用性能的因素,粗略地说,有以下几个大的环节: 1. 客户端环节 2. 网络环节(可能包括WAN和LAN) 3. 应用及中间层环节 4. 数据库层环节 能够对各个环节的问 ...

  10. 使用技术手段限制DBA的危险操作—Oracle Database Vault

    概述 众所周知,在业务高峰期,某些针对Oracle数据库的操作具有很高的风险,比如修改表结构.修改实例参数等等,如果没有充分评估和了解这些操作所带来的影响,这些操作很可能会导致故障,轻则导致应用错误, ...

随机推荐

  1. pcre 不支持 utf 的问题

    问题: Error 500  preg_match(): Compilation failed: this version of PCRE is compiled without UTF suppor ...

  2. 阿里云slb上传证书错误

    阿里云上传证书错误 今天在阿里云给slb上传新买的证书,传的过程中报错了,如下: 网上找了半天没找到,鼠标放在错误哪行行首,会报一个错 大意就是一行最多64个字符,我检查了下,报错这行是68个字符,于 ...

  3. bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  4. shell判断变量是字符还是数字

    ok,以后最好是每天一个shell小脚本吧,这样以后工作时还可以直接套用,嗯,比较不错,顺便还可以带给刚入门shell的朋友一些帮助,好了,废话不多说,下面是我两种判断的实现方式: 1.通过grep去 ...

  5. 使用PXE+NFS EFI引导安装RHEL6/7以及Kickstart安装

    PXE引导的步骤: 1.开机后选择网络启动,client端向server端的dhcpd发起获取IP地址的dhcp请求. 2.server端分配IP后,dhcpd会同时根据其配置文件,通过TFTP协议发 ...

  6. mesg---设置当前终端的写权限

    mesg命令用于设置当前终端的写权限,即是否让其他用户向本终端发信息.将mesg设置y时,其他用户可利用write命令将信息直接显示在您的屏幕上. 语法 mesg(参数) 参数 y/n:y表示运行向当 ...

  7. shiro整合thymeleaf

    1.引入依赖 <!--thymeleaf中使用shiro--> <dependency> <groupId>com.github.theborakompanioni ...

  8. CSS浏览器兼容问题集(一)

    CSS对浏览器的兼容性有时让人非常头疼,也许当你了解其中的技巧跟原理,就会认为也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理方法并整理了一下.对于web2.0的过度,请尽量用xhtml ...

  9. 赵雅智_运用Bitmap和Canvas实现图片显示,缩小,旋转,水印

    上一篇已经介绍了Android种Bitmap和Canvas的使用,以下我们来写一个详细实例 http://blog.csdn.net/zhaoyazhi2129/article/details/321 ...

  10. OpenCv 人脸检測的学习

    近期公司要组织开发分享,可是自己还是新手真的不知道分享啥了,然后看了看前段时间研究过OpenCv,那么就分享他把. openCv就不介绍了,说下人脸检測.事实上是通过openCv里边已经训练好的xml ...