ORACLE使用全套脚本

select * from help;
----------------1.创建表空间(可以省略,企业中就必须实现)
CREATE TABLESPACE epet_tablespace
DATAFILE 'F:\word\Epet.dbf' --前提必须目录存在
SIZE 10M
AUTOEXTEND ON NEXT 5M 
MAXSIZE UNLIMITED

alter database datafile 'F:\word\Epet.dbf' offline drop;
drop tablespace Epet

---------------2.创建用户------------------------
CREATE USER epet IDENTIFIED BY accp
DEFAULT TABLESPACE epet_tablespace  --可以省略
TEMPORARY TABLESPACE temp;          --可以省略

---------------3.授权和撤销------------------------
GRANT CONNECT,RESOURCE TO epet;
--授予查询权限给epet
--GRANT SELECT ON emp TO epet;
--select * from system.emp

---------------4.使用epet登录数据库,之后创建表------------------------
--创建master,pet_type,pet表
CREATE TABLE  pet_master(
       m_id number(11) PRIMARY KEY,
       loginid NVARCHAR2(50) NOT NULL,
       m_password NVARCHAR2(50) NOT NULL,
       status CHAR(1) DEFAULT 1 NOT NULL
);

CREATE TABLE pet_type(
       t_id NUMBER(11) PRIMARY KEY,
       t_name NVARCHAR2(50) NOT NULL,
       status CHAR(1) DEFAULT 1 NOT NULL
);

CREATE TABLE pet(
       p_id NUMBER(11),
       m_id NUMBER(11),
       p_name NVARCHAR2(50),
       t_id NUMBER(11),
       health NUMBER(11) DEFAULT 100 NOT NULL,
       love NUMBER(11) DEFAULT 100 NOT NULL,
       prop1 NVARCHAR2(100),
       prop2 NVARCHAR2(100),
       prop3 NVARCHAR2(100),
       adopt_time DATE NOT NULL,
       status CHAR(1) DEFAULT 1 NOT NULL,
       CONSTRAINT pet_pk PRIMARY KEY (p_id),
       CONSTRAINT p_m_fk FOREIGN KEY (m_id) REFERENCES pet_master(m_id),
       CONSTRAINT p_t_fk FOREIGN KEY (t_id) REFERENCES pet_type(t_id)
);

---------------5.序列------------------------
create sequence SEQ_MASTER_ID
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 10;

create sequence SEQ_PET_ID
minvalue 1
maxvalue 999999999999999999999999999
start with 4
increment by 1
cache 10;
--查询序列的值
select SEQ_MASTER_ID.NEXTVAL from dual;
--查询遍编辑数据
select * from pet for update
select * from pet_type for update
select * from pet_master for update
--插入测试数据
insert into pet_type values(1,'狗狗',default);
insert into pet_type values(2,'猫猫',default);
insert into pet_type values(3,'鸡鸡',default);

insert into pet_master values(SEQ_MASTER_ID.nextval,'大叔','123',default);
insert into pet_master values(SEQ_MASTER_ID.nextval,'大妈','111',default);

insert into pet values(SEQ_PET_ID.nextval,1,'阿呆',1,80,80,null,null,null,sysdate,0);
insert into pet values(SEQ_PET_ID.nextval,1,null,1,80,80,null,null,null,sysdate,1);
commit;
---------------6.常用函数------------------------
-----Extract,获取指定日期部分,||连接字符串
--领养时间年月日,不推荐
select extract(year from adopt_time) || '年' ||
       extract(month from adopt_time) || '月' ||
       extract(day from adopt_time) || '日' adopt_time
  from pet;
-----to_char是将非字符类型转换字符类型,
-----常用于日期转换为指定格式的字符串
--to_char(date)
select To_char(adopt_time) from pet;
--to_char(date,format)
select to_char(adopt_time, 'yyyy-mm-DD') adopt_time from pet;
select to_char(adopt_time, 'yyyy"年"MM"月"dd"日"') adopt_time from pet;
select to_char(
sysdate, 'yyyy"年"MM"月"dd"日"hh24"时"mi"分"ss"秒"'
) adopt_time from dual;

-----Decode,如果条件1,结果1,条件2,结果2...,else结果
select decode(pet.status,0,'正常',1,'禁用','无效') status from pet;

select decode(pet.name,null,'无名',pet.name) from pet;
-----nvl,如果值不为null就正常显示,否则显示为指定的值
select nvl(pet.name,'无名') from pet;
-----其他函数
--日期比较
--第一种,使用默认日期格式比较
select *
  from pet
 where pet.master_id = 1
   and pet.adopt_time > '01-1月-10'--OK
--第二种,使用to_char
select *
  from pet
 where pet.master_id = 1
   and to_char(pet.adopt_time,'yyyy-mm-dd') > '2010-01-01'
--第三种,使用to_date
select *
  from pet
 where pet.master_id = 1
   and pet.adopt_time >to_date( '2010-01-01','yyyy-mm-dd');
select to_char(pet.adopt_time) from pet;
--去空格
select rtrim(ltrim('      abc         ')) a from dual;
--replace,替换,位置从1开始
select replace ('jack and jue', 'j', 'bl') from dual;
--instr,查询字符的位置
select instr ('worldwide', 'd') from dual;
--substr,截取字符串
select substr ('abcdefg',3,2) from dual;
--mod,取余数
select mod(10,3)  from dual;
--round,四舍五入取小数位数
select round(100.256,2) from dual;
--trunk,不进行四舍五入取小数位数
select trunc(100.256,2)  from dual;
--to_number,转换成数字
select to_number('1234.5') from dual;
--聚合函数
select count(*) from master where status = 1;
select avg(health) from pet where master_id=1;
--案例
select pet.id,
       nvl(pet.name, '无名') name,
       pet.health,
       pet.love,
       decode(pet.status, 0, '正常', 1, '禁用', '无效') status,
       to_char(pet.adopt_time, 'yyyy"年"mm"月"dd"日"') adopt_time
  from pet
---------------7.索引------------------------
CREATE UNIQUE INDEX adopt_time_index ON pet(adopt_time DESC);
CREATE BITMAP INDEX type_id_bitmap_index ON pet(type_id);
CREATE INDEX health_love_index ON pet(health,love);
CREATE INDEX to_char_index ON pet(TO_CHAR(adopt_time,'YYYY'));
CREATE INDEX master_id_reverse_index ON pet(master_id) REVERSE;
DROP INDEX master_id_reverse_index;

---------------8.导出与导入------------------------
--1.表方式的导出
exp epet/accp@服务名 tables=(master,pet_type,pet) file=d:\tables_epet.dmp
--2.用户方式的导出(建议),如果数据大的时候,建议使用buffer=8000000
exp epet/accp file=d:\epet.dmp buffer=8000000
--3.全数据库方式导出(不建议)
exp system/accp file=d:\system_complete.dmp inctype=complete buffer=8000000
---------------9.导入------------------------
imp epet3/accp file=d:\epet.dmp full=y buffer=8000000

imp epet/accp file=F:\word\ch12\epetdb.dmp full=y buffer=8000000

--注意
--导出与导入用户的时候,必须保证导出的用户与导入的用户具有相同角色

 

你可能感兴趣的:(oracle)