--作业
create tablespace tp_hr
datafile 'E:orcl_datas\A_hr.dbf'--这个文件夹要存在
size 10m autoextend on
next 32m maxsize 2048m;
create tablespace tp_orders
datafile 'E:orcl_datas\A_oe.dbf'
size 10m autoextend on
next 32m maxsize 2048m;
alter user A_oe
default tablespace tp_orders;
create sequence dept_seq
start with 60
increment by 10
maxvalue 10000;
drop sequence dept_seq;
select * from dept;
insert into dept values(dept_seq.nextval,'zz','guangdong');
insert into dept values(dept_seq.nextval,'bb','hunan');
drop sequence dept_seq;
create table deptBak as select * from dept;
drop table deptBak;
select * from deptBak;
insert into deptBak values(dept_seq.nextval,'cc','hunan');
insert into deptBak values(dept_seq.nextval,'dd','hunan');
--为客户编号创建反向键索引
create index index_reverse_customer_id on customers(customer_id) reverse;
--为地域创建位图索引
create bitmap index index_bitmap_nls_territory on customers(nls_territory);
-- 为名和姓氏创建组合索引
create index indx_t on customers(customer_last_name,customer_first_name);
--根据订单表创建范围分区表
create table rangeOrder
(
ORDER_ID NUMBER(12) not null,
ORDER_DATE DATE not null,
ORDER_MODE VARCHAR2(8),
CUSTOMER_ID NUMBER(6) not null,
ORDER_STATUS NUMBER(2),
ORDER_TOTAL NUMBER(8,2),
SALE_REP_ID NUMBER(6),
PROMOTION_ID NUMBER(6)
)
partition by range(order_date)
(
partition p1 values less than (to_date('2005/01/01','yyyy/mm/dd')),
partition p2 values less than (to_date('2006/01/01','yyyy/mm/dd')),
partition p3 values less than (to_date('2007/01/01','yyyy/mm/dd')),
partition p4 values less than (to_date('2008/01/01','yyyy/mm/dd')),
partition p5 values less than (to_date('2009/01/01','yyyy/mm/dd')),
partition p6 values less than (maxvalue)
);
select * from rangeOrder partition (p6);
delete from rangeOrder partition (p6);
oracle数据库出现“批处理中出现错误: ORA-00001: 违反唯一约束条件”解决方法
最近使用oraclede impdp工具全库导入数据库时,在数据库里面使用出现如下情况。
SQL state [null]; error code [17081]; 批处理中出现错误: ORA-00001: 违反唯一约束条件 (GDXAORCL.SYS_C0055359); nested exception is java.sql.BatchUpdateException: 批处理中出现错误: ORA-00001: 违反唯一约束条件 (GDXAORCL.SYS_C0055359) -(:155)
由于表的ID是唯一的,所以用 select max(id) from test 查的该ID大于Sequences里面的开始ID,所以出现了该情况
为此,我们只要更改Sequences里面的”开始于“的ID大于max(ID)即可。
问题解决
----------------------------------------------------------------------------
--练习
--1.请将这些表建立在tb_bank表空间,由A_bank用户管理
--使用system 用户登录
create tablespace tb_bank
datafile 'E:orcl_datas\A_bank.dbf'
size 10m autoextend on
next 32m maxsize 2048m;
create user A_bank
identified by A_bank
default tablespace tb_bank
temporary tablespace temp;
grant connect ,resource ,create synonym,create public synonym to A_bank;
--以下使用A_bank用户登录
-- 2.为四个表建立主外键关系及各种约束
/*客户表*/
drop table Customers;
Create table Customers
(
cId number not null primary key ,
cName varchar2(20) check (length(cName)>=2) not null,--客户姓名(建立约束只能是2 个字以上)
cSex number(1) check (cSex in (0,1)),--性别(建立约束只能是0 ,1)
cBirthday date,--出生日期
cIdentity varchar2(20) unique--证件号码(unique);
);
insert into Customers values(SEQ_Customers.Nextval,'张三',1,to_date('1992-09-09','yyyy-mm-dd'),'1111');
insert into Customers values(SEQ_Customers.Nextval,'张莉莉',0,to_date('1999-08-09','yyyy-mm-dd'),'2222');
select * from Customers;
/*卡表*/
drop table Cards;
Create table Cards
(
cardid number not null primary key,
cardNo char(3) not null unique,--卡号(建立唯一约束)
cid number references Customers(cId) not null ,--客户ID
siteName varchar(20),--网点名称
balance number(10,2) check(balance >0),--余额(>=0 )
state number(1) default 1, check(state in(1,2,3))--位图索引,状态:1正常 2冻结 3已经销卡 (建立约束,只能是1,2,3,默认是1)
);
create bitmap index state on Cards (state);
insert into Cards values(SEQ_Cards.Nextval,'123',7,'东风路网点',800.00,default);--如果有默认值,插入数据时要写default;
insert into Cards values(SEQ_Cards.Nextval,'124',8,'北京路网点',10000.00,default);--如果有默认值,插入数据时要写default;
select * from Cards;
/*交易记录*/
Create table Records
(
Rid number not null primary key ,--主键
cardid number not null references Cards(cardid),--卡ID
amount number(10,2) not null check(amount>0),--金额(必须大于0)
inOrOut number(1) not null check(inOrOut in (0,1)),--收入或支出(只允许是0,1)
recordType number(1) not null check(recordType in (1,2,3)),--帐务类型 1.现金 2.转账 3.工资
recordTime date--时间
);
insert into Records values(SEQ_Records.Nextval,1,400.00,0,1,trunc(sysdate,'dd'));
insert into Records values(SEQ_Records.Nextval,2,800.00,0,2,trunc(sysdate-13,'dd'));
insert into Records values(SEQ_Records.Nextval,2,900.00,1,2,trunc(sysdate-10,'dd'));
insert into Records values(SEQ_Records.Nextval,2,900.00,1,2,trunc(add_months(sysdate,-6),'dd'));
insert into Records values(SEQ_Records.Nextval,2,1000.00,1,2,trunc(add_months(sysdate,-3),'dd'));
insert into Records values(SEQ_Records.Nextval,2,1000.00,1,2,trunc(add_months(sysdate,3),'dd'));
insert into Records values(SEQ_Records.Nextval,1,400.00,0,1,trunc(add_months(sysdate,3),'dd'));
insert into Records values(SEQ_Records.Nextval,1,400.00,1,1,trunc(add_months(sysdate,-3),'dd'));
select * from records;
/*历史记录表(开卡,销卡)*/
Create table Historys
(
Id number not null primary key ,--主键
cardid number not null references Cards(cardid),--卡id
oprType number(1) check(oprType in (0,1)),--操作类型:1-开卡 0-销卡(只允许是0,1)
oprTime date,--操作时间
oprName varchar2(20)--操作人
);
insert into Historys values(SEQ_Historys.Nextval,1,1,trunc(sysdate-20,'dd'),'刘全宽');
insert into Historys values(SEQ_Historys.Nextval,2,0,trunc(sysdate-25,'dd'),'林建辉');
select * from Historys;
-- 创建4个序列为4张表新增时候作主键自增长
create sequence SEQ_Customers
minvalue 1
maxvalue 10000
start with 1
increment by 1
cache 20;
create sequence SEQ_Cards
minvalue 1
maxvalue 10000
start with 1
increment by 1
cache 20;
create sequence SEQ_Records
minvalue 1
maxvalue 10000
start with 1
increment by 1
cache 20;
create sequence SEQ_Historys
minvalue 1
maxvalue 10000
start with 1
increment by 1
cache 20;
--6.先建立私有同义词Customers-> cus Cards->ca Records->rec Historys->ht 方便查询
-- grant create synonym to A_bank;
create synonym cus for Customers ;
create synonym ca for Cards ;
create synonym rec for Records ;
create synonym ht for Historys ;
--4.创建这些表和索引
--规划哪些表要做成分区表,告诉老师理由
/*
||交易记录表和历史记录表需要做成分区表
||因为交易记录表里有交易时间 recordTime ,需要建立范围分区,
交易类型recordType ,需要建立列表分区
||历史记录表oprTime 需要建立范围分区,
*/
--2)规划在哪些列应该建立索引,告诉老师理由,建立哪种类型的索引
/*
||除主键列之外,位图索引的优点是适合低基数列(如:卡表的state )
||B 树索引适用于变化大,高基数列(如:客户表的客户名称)
||反向键索引:(解决热点问题,大家要查询的集中在某一地方 ,并发的负担很高,磁盘负担很大)
*/
--1)查询本月内取款最多的用户有哪些
select sum(amount) ,c.cName ,r.cardid
from Records r ,customers c, cards cd
where r.cardid=cd.cardid and c.cid=cd.cid and
r.inOrOut =1 and r.recordTime between trunc(sysdate,'month') and trunc(last_day(sysdate))
group by c.cName, r.cardid ;
--2)查询本年发生交易笔数最多的前10 名是哪些用户,并把排名打印出来
select * from
(select rank() over( partition by r.cardid order by r.cardid desc) rk,c.*
from Records r ,customers c, cards cd
where r.cardid=cd.cardid and c.cid=cd.cid and to_char(r.recordTime,'yyyy')=to_char(sysdate,'yyyy')
)where rk<=10;
--3)根据卡号查询交易记录,按时间从高到低分页查询
select * from
(
select b.* ,rownum r from
(select rd.* from Cards cd,Records rd where rownum <=5 and cd.cardid=rd.cardid and cd.cardno='123' order by rd.recordtime desc) b
)
a where r>=0
--4)查询本年各季度取款金额
--方法一:
select '第一季度' as 季度, sum(amount) as 总金额
from Records r where inOrOut=1 and to_char(r.recordTime,'yyyy')=to_char(sysdate,'yyyy')
and r.recordTime between trunc(add_months(sysdate,-6),'q') and trunc(add_months(sysdate,-3),'q')
union
select '第二季度' as 季度, sum(amount) as 总金额
from Records r where inOrOut=1 and to_char(r.recordTime,'yyyy')=to_char(sysdate,'yyyy')
and r.recordTime between trunc(add_months(sysdate,-3),'q') and trunc(add_months(sysdate,0),'q')
union
select'第三季度' as 季度, sum(amount) as 总金额
from Records r where inOrOut=1 and to_char(r.recordTime,'yyyy')=to_char(sysdate,'yyyy')
and r.recordTime between trunc(add_months(sysdate,0),'q') and trunc(add_months(sysdate,3),'q')
union
select '第四季度' as 季度, sum(amount) as 总金额
from Records r where inOrOut=1 and to_char(r.recordTime,'yyyy')=to_char(sysdate,'yyyy')
and r.recordTime between trunc(add_months(sysdate,3),'q') and trunc(add_months(sysdate,+6),'q')
--方法二:
select to_char(r.recordTime,'q') as 季度,sum(amount) as 总金额
from Records r where inOrOut=1 and to_char(r.recordTime,'yyyy')=to_char(sysdate,'yyyy')
group by to_char(r.recordTime,'q')