1、安装11g数据库
安装
2、client 客户端
3、plsql 64位
首先,安装完后配置客户端目录
其次,配置环境变量:
NLS_LANG
AMERICAN_AMERICA.AL32UTF8
或 SIMPLIFIED CHINESE_CHINA.ZHS16GBK
最后,配置系统变量:
TNS_ADMIN
D:\instantclient_11\network\ADMIN
准备登陆plsql:
解除scott用户锁定
->进入system用户输入
alter user scott account unlock;
注:oracle错误信息提示中英文显示切换
alter session set NLS_LANGUAGE='SIMPLIFIED CHINESE'; --英文切换为中文
alter session set nls_language='american'; --中文切换为英文
①新创建用户
第一步,用下面的语句查询是否有要创建的表空间"DBTEST_TAB"和"DBTEST_IDX",如果没有,则把D:\database\zyhip改为对应的路径,有的话直接创建用户
select tablespace_name
,file_name
,round(bytes/(1024*1024),0)"SIZE(MB)"
,AUTOEXTENSIBLE
,increment_by
,round(maxbytes/(1024*1024),0) max_size
from dba_data_files
order by tablespace_name;
定义块的大小
alter system set db_16K_cache_size=20m; --定义块的大小
创建表空间
--创建表空间
CREATE TABLESPACE "DBTEST_TAB"
blocksize 16k
DATAFILE 'F:\DBtest_Tab\DBTEST_TAB01.ora' SIZE 100M
AUTOEXTEND ON NEXT 50M MAXSIZE 32000M,
'F:\DBtest_Tab\DBTEST_TAB02.ora' SIZE 100M
AUTOEXTEND ON NEXT 50M MAXSIZE 32000M
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO ;
--创建索引表空间
CREATE TABLESPACE "HOSPDW_IDX"
blocksize 16k
DATAFILE 'F:\DBtest_Tab\DBTEST_IDX01.ora' SIZE 50M
AUTOEXTEND ON NEXT 50M MAXSIZE 32000M,
'F:\DBtest_Tab\DBTEST_IDX02.ora' SIZE 50M
AUTOEXTEND ON NEXT 50M MAXSIZE 32000M
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO ;
创建用户
create user Ruan --用户名
IDENTIFIED BY dbtest --密码
default tablespace DBTEST_TAB
temporary tablespace TEMP
profile DEFAULT;
-- Grant/Revoke role privileges
grant connect to Ruan with admin option;
grant dba to Ruan with admin option;
grant resource to Ruan with admin option;
-- Grant/Revoke system privileges
grant create procedure to Ruan;
grant create public synonym to Ruan;
grant create table to Ruan;
grant create view to Ruan;
grant debug any procedure to Ruan;
grant delete any table to Ruan;
grant drop public synonym to Ruan;
grant insert any table to Ruan;
grant select any dictionary to Ruan;
grant select any sequence to Ruan;
grant select any table to Ruan with admin option;
grant unlimited tablespace to Ruan with admin option;
grant update any table to Ruan;
②建表
--新建表
create table temp
(
id number(6) primary key, --主键约束:标识整条记录,唯一标识(非空+唯一)
name varchar2(20) constraint temp_name_nn not null, --②非空约束起名:temp_name_nn,否则系统默认起名
sex number(1),
age number(3),
dim_date date,
grade number(2) default 1, --③默认值1
class number(4),
email varchar2(50),
addr varchar2(20) unique, --①唯一约束
constraint temp_email_name_uq unique(email,name) --④表级约束
--constraint temp_id_pk primary key(email,name) --⑤主键约束
)
--修改已有约束
alter table table_name add constraint constraint_name unique (column_name) --列名
--添加外键约束(级联删除)
--foreign key (groupid) references t_group(id) on delete cascade on update cascade
alter table temp add constraint constraint_tu_id foreign key (id) references temp_1 (id) on delete cascade; --删除级联
添加外键约束(级联更新):Oracle的外键默认是非延迟约束,修改学生的外键为延迟约束。
--例子
-- 班级表
create table tb_class
(
id number not null, --班级主键
name varchar2(50), --班级名称
constraint pk_tb_class primary key (id)
);
-- 学生表
create table tb_student
(
id number not null, --学生主键
name varchar2(50), --学生姓名
class_id number, --学生所属班级,外键
--主键约束
constraint pk_tb_student primary key (id),
--外键约束
--设置级联删除为no action
constraint fk_tb_student_class_id foreign key (class_id) references tb_class (id)
);
-- 添加班级数据
insert into tb_class (id, name) values (1, '一班');
insert into tb_class (id, name) values (2, '二班');
insert into tb_class (id, name) values (3, '三班');
-- 添加学生数据
insert into tb_student (id, name, class_id) values (1, '小明', 1);
insert into tb_student (id, name, class_id) values (2, '小刚', 1);
insert into tb_student (id, name, class_id) values (3, '小王', 1);
insert into tb_student (id, name, class_id) values (4, '二明', 2);
insert into tb_student (id, name, class_id) values (5, '二刚', 2);
insert into tb_student (id, name, class_id) values (6, '二王', 2);
insert into tb_student (id, name, class_id) values (7, '大明', 3);
insert into tb_student (id, name, class_id) values (8, '大刚', 3);
insert into tb_student (id, name, class_id) values (9, '大王', 3);
---------------------------开始-------------------
--删除学生表(tb_student)上的已有外键
alter table tb_student drop constraint fk_tb_student_class_id;
--添加延迟约束外键
alter table tb_student add constraint fk_tb_student_class_id foreign key (class_id) references tb_class (id) on delete cascade deferrable;
--触发器
create or replace trigger tgr_tb_class_update
after update of id on tb_class
for each row
begin
if :old.id<>:new.id then
update tb_student set class_id=:new.id where class_id=:old.id;
end if;
end;