oracle基本笔记整理及案例分析2
生活随笔
收集整理的這篇文章主要介紹了
oracle基本笔记整理及案例分析2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*
======================================================================================================================================
*//*
tp_orders表空間,大小10M,文件大小可自動增長,允許文件擴展,最大限度為無限制
創建A_oe用戶的默認表空間為tp_orders
密碼 bdqn
授予connect,resource權限黨文a_hr用戶的employee
*/
--創建表空間和用戶,并授予訪問數據庫的權限
create tablespace tp_orders
datafile 'E:\E盤\tp_orders01.dbf'
size 10M
autoextend on;--創建用戶
create user A_oe
identified by bdqn
default tablespace tp_orders--賦予權限
grant connect,resource to A_oe;
grant select on test.employee to A_oe;
grant select on test.bumen to A_oe;select * from test.employee;/*使用序列生成部門編號的值
*/
/*
從60開始,間隔是10,最大值是10000的序列的對象dept_seq
*/
select * from bumen;
--創建一個序列
create sequence dept_seq
start with 60
increment by 10
maxvalue 10000
--插入數據
insert into bumen values(dept_seq.nextval,'學術部');
insert into bumen values(dept_seq.nextval,'學術部1');
--數據遷移前的工作
drop sequence dept_seq;create sequence dept_seq
start with 80
increment by 10
maxvalue 10000;
create table deptBak as
select * from bumen;select * from deptBak;
--測試插入數據
insert into deptBak values(dept_seq.nextval,'人事部');/*創建A_oe模式下dept表的公有同義詞,可以允許任何能夠連接上數據庫的用戶訪問
*/--創建一個測試dept表
create table dept
as select * from test.bumen;select * from dept;--創建同義詞
create public synonym p_sy_dept for a_oe.dept;
--賦予權限
grant select on test.customers to A_oe;
grant create public synonym to A_oe;
select * from p_sy_dept;/*
切換用戶,操作使用test用戶
*/
--查看并且操作employee表select * from customers;
--為客戶編號創建反向建索引
create index index_reverse_customer_id on customers (customer_id) reverse;
--為地域列創建位圖索引
create bitmap index index_nls_territory on customers (nls_territory);
--為名和姓氏列創建組合索引
create index index_cus on customers(cust_fiest_name,cust_last_name);/*
根據訂單表創建范圍分區表
*/
--(1)已完成
--(2)創建分區
create table rangeOrders
(
order_id number(12) primary key, --訂單編號
order_date date not null, --訂貨日期
order_mode varchar2(8) not null , --訂貨模式
customer_id number(6) not null, --客戶編號
order_status number(2), --訂單狀態
order_total number(8,2), --總定價
sales_rep_id number(6), --銷售代表id
promotion_id number(6) --推廣員id
)
partition by range (order_date)
(
partition part1 values less than (to_date('2013-01-01' , 'yyyy-mm-dd')),
partition part2 values less than (to_date('2014-01-01' , 'yyyy-mm-dd')),
partition part3 values less than (to_date('2015-01-01' , 'yyyy-mm-dd')),
partition part4 values less than (to_date('2016-01-01' , 'yyyy-mm-dd')),
partition part5 values less than (to_date('2017-01-01' , 'yyyy-mm-dd')),
partition part6 values less than (maxvalue)
)--插入測試數據
insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(1,'2017-02-09','網上下單',2,1,323.23,1,2);insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(2,'2016-11-09','上門購買',1,2,56.00,2,1);
insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(3,'2017-12-20','熟人推薦',3,1,6000,1,2);
insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(4,'2015-12-02','網上下單',5,2,365,2,2);
insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(5,'2017-12-09','上門購買',3,1,3210,1,2);
insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(6,'2014-11-11','網上下單',3,1,630,2,2);
insert into rangeOrders(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(7,'2017-01-01','上門購買',2,1,6300,1,2);--查看表中的數據
select * from rangeOrders
--查詢每一個分區中的數據
select * from rangeOrders partition (part1);
select * from rangeOrders partition (part2);
select * from rangeOrders partition (part3);
select * from rangeOrders partition (part4);
select * from rangeOrders partition (part5);
select * from rangeOrders partition (part6);--查看分區情況
select table_name,partition_name from user_tab_partitions;--把已存在的表改為分區表
create table rangeOrder
partition by range (order_date)
(
partition part1 values less than (to_date('2013-01-01' , 'yyyy-mm-dd')),
partition part2 values less than (to_date('2014-01-01' , 'yyyy-mm-dd')),
partition part3 values less than (to_date('2015-01-01' , 'yyyy-mm-dd')),
partition part4 values less than (to_date('2016-01-01' , 'yyyy-mm-dd')),
partition part5 values less than (to_date('2017-01-01' , 'yyyy-mm-dd')),
partition part6 values less than (maxvalue)
)
as select * from orders;/*
間隔分區(自動化)
*/
--創建分區表(按照一年分一個表)
create table sales_interval1
partition by range (order_date)
interval (numtoyminterval(1,'year')) --按照一年分區一個表
(partition part1 values less than (to_date('2017/01/01','yyyy/mm/dd')))
as select * from orders;--查看分區情況
select table_name,partition_name,tablespace_name from user_tab_partitions
where table_name=upper('sales_interval1');
--插入一條測試數據
insert into sales_interval1(order_id,order_date,order_mode,customer_id,order_status,order_total,sales_rep_id,promotion_id)
values(98,'2018/01/03','熟人推薦',3,1,9658,2,2);/*注意:剛剛加的那條記錄現在肯定沒有,所以
1.先執行
select table_name,partition_name,tablespace_name from user_tab_partitions
where table_name=upper('sales_interval1');
查看下一個分區是多少
2.復制剛剛查詢出來的分區,eg:SYS_P21
3.執行select * from sales_interval1 partition (SYS_P21);
*/
select * from sales_interval1 partition (SYS_P21);--添加分區
alter table rangeOrder
add partition part7 values less tahn(to_date('2018-01-01','yyyy-mm-dd'));
--刪除分區
alter table rangeOrder
drop partition part3;
--移動分區
alter table rangeOrder
move partition part1 tablespace works01; --works01是表空間名稱、/*
1.創建一個單獨的表空間
2.把分區的數據移動到這個表空間里面去
3.讓這個表空間作為只讀
*/--以system的身份登陸上
create tablespace tb_name
datafile 'e:\oracle\tbdb.dbf'
size 10M;
--授權
alter user test quota unlimited on tb_name;
--移動
alter table rangeOrder
move partition part1 tablespace tb_name;
--設置為只讀
alter tablespace tp_name read only;
--設置為讀寫
alter tablespace tp_name read write;/*
課后簡答題
*/
--(1)在test用戶下創建一個表Stock_Received
create table Stock_Received
(
Stock_ID number,
Stock_Date date,
Cost varchar2(50)
)--插入數據
insert into Stock_Received values (myseq.nextval,'2017/03/05','描述一');
insert into Stock_Received values(myseq.nextval,'2017/01/05','描述二');
insert into Stock_Received values (myseq.nextval,'2017/02/05','描述三');
insert into Stock_Received values(myseq.nextval,'2017/04/05','描述四');
insert into Stock_Received values(myseq.nextval,'2017/05/05','描述五');
insert into Stock_Received values(myseq.nextval,'2017/06/05','描述六');
insert into Stock_Received values(myseq.nextval,'2017/05/05','描述七');
insert into Stock_Received values(myseq.nextval,'2017/04/05','描述八');
insert into Stock_Received values(myseq.nextval,'2017/02/05','描述九');
insert into Stock_Received values(myseq.nextval,'2017/01/05','描述十');
insert into Stock_Received values(myseq.nextval,'2017/08/05','描述十一');
--創建一個名為myseq的序列
create sequence myseq
start with 1000
increment by 10
maxvalue 1100
cycle--(2)創建一個公有的同義詞
create public synonym p_Stock_received for Stock_Received
--給a_oe賦予一個可以查看Stock_Received的權限
grant select on p_Stock_received to a_oe;--用a_oe登陸,測試能不能查看Stock_Received表
select * from p_Stock_received;
--↑測試成功--(3)在Stock_Received中根據Stock_Date列創建3個范圍分區
create table range_Stock_Received
partition by range(Stock_Date)
(
partition p1 values less than(to_date('2017/01/01','yyyy-mm-dd')),
partition p2 values less than(to_date('2017/03/01','yyyy-mm-dd')),
partition p3 values less than(to_date('2017/05/01','yyyy-mm-dd')),
partition p4 values less than(maxvalue)
)
as select * from Stock_Received--查看每個分區里面的數據
select * from range_Stock_Received partition (p1);
select * from range_Stock_Received partition (p2);
select * from range_Stock_Received partition (p3);--(4)在表的id上創建一個逐漸索引列
create index index_Stock_ID on Stock_Received (Stock_ID);
總結
以上是生活随笔為你收集整理的oracle基本笔记整理及案例分析2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕vivobook电脑如何查看详情?
- 下一篇: DreamHost vs JustHos