--简单的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;

最新文章

  1. Java并发1——线程创建、启动、生命周期与线程控制
  2. EF 未应用自动迁移,因为自动迁移会导致数据丢失的解决办法
  3. 深入理解Java虚拟机(一)、Java内存区域与内存溢出异常
  4. 安装Visual Studio 2013 出现0x80070643错误
  5. Tween + 缓动函数
  6. 关于allow_url_fopen的设置与服务器的安全
  7. leetcode抽风的解决办法
  8. Web Project配置Hirbernate
  9. poj1222 EXTENDED LIGHTS OUT
  10. jetty服务器访问系统的域名
  11. delphi代码实现创建dump文件
  12. hdu3018欧拉回路题
  13. Spring MVC NoClassDefFoundError 问题的解决方法。
  14. Vxworks驱动程序的结构
  15. RX系列四 | RxAndroid | 加载图片 | 提交表单
  16. OpenCV imread读取jpg图像的一个大坑
  17. python基础之作业3----三级菜单小练习
  18. JVM内存管理概述
  19. javascript自定义简单map对象功能
  20. iOS-图像之CoreImage框架

热门文章

  1. [洛谷P3932]浮游大陆的68号岛
  2. Rman备份及不完全恢复操作
  3. 在ubuntu上编译rasbian kernel(for raspberry pi 1)
  4. vue+element-ui的简洁导入导出功能
  5. ECNUOJ 2575 Separate Connections
  6. 怎样使用纯CSS3创建一个简单的五角星图形
  7. Android——4.2.2 文件系统文件夹分析
  8. rgba
  9. 生成不重复的随机数对(C/C++)
  10. Day2代码