達夢數據庫經常使用語句範例 by Crystane Dec 27,2019

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 指定鎖定時間(分);ide

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;ui

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');spa

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'); rest

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.查看用戶所屬的角色;code

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;ci

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/SYSDBA@localhost:5236 file=dexp01.dmp log=dexp01.log directory=/dm7/backup/dexp full=y
--邏輯還原

./dimp sysdba/SYSDBA@localhost:5236 file=/dm7/backup/dexp/dexp01.dmp log=dimp02.log directory=/dm7/backup/dexp

  1. 做業管理
    --SYSJOBS:做業信息
    select from SYSJOB.SYSJOBS;--SYSJOBSCHEDULES:做業調度信息select from SYSJOB.SYSJOBSCHEDULES;--SYSJOBHISTORIES:做業歷史信息select * from SYSJOB.SYSJOBHISTORIES;
相關文章
相關標籤/搜索