1.创建表空间
--创建初始大小为32M的表空间;
create tablespace test datafile ‘/dm7/data/DAMENG/TEST1_01.DBF‘ SIZE 32;
--创建初始大小为50M的表空间,自动扩展,最大扩展到100M;
create tablespace tbs1 datafile ‘/dm7/data/DAMENG/tbs1_02.dbf‘ size 50 autoextend on maxsize 100 ;
--创建初始大小为50M的表空间,自动扩展,最大扩展到100M,每次增加1M;
create tablespace tbs3 datafile ‘/dm7/data/DAMENG/tbs3_02.dbf‘ size 50 autoextend on next 1 maxsize 100 ;
2.查看表空间
select * from dba_tablespaces;
?SYSTEM :数据字典和全局的系统数据。
?ROLL:存放了数据库运行过程中产生的回滚记录。
?UNDO_RETENTION:单位秒
?TEMP:临时表空间
?MAIN:数据库默认的表空间,创建数据对象时,如果不指定存储位置,默认存放在该表空间。
?HMAIN:HUGE表空间
3.增加表空间文件
--一个表空间可以关联多个表空间文件;
alter tablespace tbs2 add datafile ‘dm7/data/DAMNENG/tbs2_02.dbf’ size 50 autoextend on maxsize 100;
4.表空间更换存储位置或更换文件名
--首先将表空间下线;
alter tablespace tbs4 offline;
--更改表空间存储位置或更换表空间名字;
alter tablespace tbs4 rename datafile ‘/dm7/data/DAMENG/tbs4_02.dbf’ to ‘/dm7/data/tbs4_02.dbf’;
--修改完成后将表空间上线;
alter tablespace tbs4 online;
5.删除表空间
drop tablespace tbs1;
6.创建用户
--identifiled by 指定鉴定密码
--limit password_life_time 指定最大生存时间(天)
--failed_login_attemps 指定最大登陆失败次数
--password_lock_time 指定锁定时间(分);
create user test1 identified by dameng123 limit password_life_time 60, failed_login_attemps 2, password_lock_time 5;
7.查看当前用户
select user;
8.表字段注释
COMMENT ON column TEST1.STU.ADDRESS IS ‘学员地址‘;
9.查看所有用户
select from dba_users;
select from all_users;
10.创建表
--CREATE TABLE SALESORDER_DETAIL EXAMPLE
CREATE TABLE SALES.SALESORDER_DETAIL
(SALESORDERID INT NOT NULL REFERENCES
SALES.SALESORDER_HEADER(SALESORDERID),
SALESORDER_DETAILID INT NOT NULL,
CARRIERNO VARCHAR(25) NOT NULL,
PRODUCTID INT NOT NULL REFERENCES PRODUCTION.PRODUCT(PRODUCTID),
ORDERQTY INT NOT NULL,
LINETOTAL DEC(19,4) NOT NULL,
PRIMARY KEY(SALESORDERID,SALESORDER_DETAILID)) STORAGE (ON BOOKSHOP);
11.插入数据
--INSERT ADDRESS EXAMPLE
INSERT INTO PERSON.ADDRESS(ADDRESS1,ADDRESS2,CITY,POSTALCODE) VALUES(‘洪山
区 369 号金地太阳城 56-1-202‘,‘‘,‘武汉市洪山区‘,‘430073‘);
12.查看表的表空间、所属者等信息;
select * from dba_tables where table_name = ‘STU‘;
dba_tables表结构见管理员手册ORACLE兼容视图13节;
13.查询创建的表结构;
select dbms_metadata.get_ddl(‘TABLE‘,‘STU‘,‘TEST2‘);
或
sp_tabledef(‘TEST‘,‘STU‘);
14.重命名表
alter table test1.STU rename to STUDENT;
15.增加或删除列
alter table test1.student add sfid varchar(18) ;
alter table test1.student drop sfid;
16.表注释
COMMENT ON TABLE "TEST1"."STU" IS ‘xue yuan‘;
17.删除表
drop table test1.student;
18.查看用户所属的角色;
select from dba_role_privs where grantee=’test’;
19.查询角色的权限
select from dba_sys_privs where grantee=‘PUBLIC‘;
20.权限授予和收回
grant select on test.test_table to test1;
grant select(city_id,city_name) on dmhr.city to test;
REVOKE grant select on test.test_table to test1;
21.查看表的约束信息;
select * from dba_constraints where table_name=‘TEST3‘;
dba_constraints 表结构见管理员手册ORACLE兼容视图12节;
22.创建约束
--检查约束
create table test2.t8(id int check(id>=5));
--唯一约束
create table test2.T3 (id int unique);
--主键约束
create table test2.T5(id int primary key);
--外键约束
create table test2.t10(sid int primary key, pid int);
create table test2.t11(id int primary key, sid int foreign key references test2.t10 (sid));
23.禁用、启用约束
alter table test.TEST3 disable constraint CONS134218845;
alter table test.TEST3 enable constraint CONS134218845;
24.创建或更新、修改视图
create view dmhr.employee_column_controlled as
select employee_name,salary from dmhr.employee;
create view or replace dmhr.employee_column_controll
as select employee_name,salary from dmhr.employee;
25.自增列;
创建
create sequence test.s1 ---test为模式名
start with 1 ---序列的起始
increment by 1 ---自增多少
maxvalue 5 ---最大值
nocache ----是否缓存
nocycle; ---是否循环
应用:
create table test.t12(id int primary key);
insert into test.t12 values(test.s1.nextval);
select test.s1.nextval;
insert into test.t12 values(test.s1.nextval);
insert into test.t12 values(test.s1.nextval);
insert into test.t12 values(test.s1.nextval);
select test.s1.nextval; --此语句会执行失败,序列溢出。表的数据是1,3,4,5.查询也会使用掉序列数据.
修改
alter sequence test.s1 maxvalue 10;
查询
select * from dba_sequences where sequence_name = ‘S1‘;
删除
drop sequence test.s1;
26.同义词
全局同义词
create public synonym SY1 for dmhr.employee;
SELECT * from dba_synonyms;
select * from dba_synonyms where SYNONYM_NAME = ‘SY1‘;
drop public synonym SY1;
局部同义词
create synonym SY1 for dmhr.employee;
SELECT * from dba_synonyms;
select * from dba_synonyms where SYNONYM_NAME = ‘SY1‘;
drop synonym SY1;
27.索引
select from dba_indexes;
select from dba_indexes where table_name = ‘CITY‘;
create table test.emp as select * from dmhr.employee;
create tablespace index1 datafile ‘/dm7/data/DAMENG/index01.dbf‘ size 32;
create index ind_emp on test.emp(employee_id) tablespace index1;
select table_name,index_name from dba_indexes where table_name=‘EMP‘;
EXPLAIN SELECT * FROM TEST.EMP WHERE EMPLOYEE_ID < 20;
sp_create_system_packages(1);
begin
dbms_stats.gather_table_stats(‘TEST‘,‘EMP‘);
end;
alter index test.ind_emp rebuild;
alter index test.ind_emp rebuild online;
drop index test.ind_emp;
28.归档设置
select * from v$database;
--ARCH_MODE=N表示未开启归档,Y表示已开启归档
alter database mount;
alter database add archivelog ‘type=local,dest=/dm7/arch,file_size=64,space_limit=0‘;
alter database archivelog;
alter database open;
29.备份和还原
--全量备份
backup database full backupset ‘/dm7/backup/full‘;
select checkpoint(0);
--增量备份
--with backupdir指定基目录
backup database increment with backupdir ‘/dm7/backup/full‘ backupset ‘/dm7/backup/incr‘;
--表空间还原
restore tablespace tbs2 from backupset ‘/dm7/backup/full‘;
--逻辑备份
./dexp sysdba/[email protected]:5236 file=dexp01.dmp log=dexp01.log directory=/dm7/backup/dexp full=y
--逻辑还原
./dimp sysdba/[email protected]:5236 file=/dm7/backup/dexp/dexp01.dmp log=dimp02.log directory=/dm7/backup/dexp
- 作业管理
--SYSJOBS:作业信息
select from SYSJOB.SYSJOBS;
--SYSJOBSCHEDULES:作业调度信息
select from SYSJOB.SYSJOBSCHEDULES;
--SYSJOBHISTORIES:作业历史信息
select * from SYSJOB.SYSJOBHISTORIES;