取消

达梦数据库常用语句范例(转)

原文地址达梦数据库常用语句范例 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;

  1. 删除表空间

drop tablespace tbs1;

  1. 创建用户 –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;

  1. 查看当前用户 select user;
  2. 表字段注释 COMMENT ON column TEST1.STU.ADDRESS IS ‘学员地址’;
  3. 查看所有用户 select from dba_users; select from all_users;

  4. 创建表 –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);
  5. 插入数据 –INSERT ADDRESS EXAMPLE INSERT INTO PERSON.ADDRESS(ADDRESS1,ADDRESS2,CITY,POSTALCODE) VALUES(‘洪山 区 369 号金地太阳城 56-1-202’,’’,’武汉市洪山区’,’430073’);

  6. 查看表的表空间、所属者等信息; select * from dba_tables where table_name = ‘STU’; dba_tables表结构见管理员手册ORACLE兼容视图13节;
  7. 查询创建的表结构; select dbms_metadata.get_ddl(‘TABLE’,’STU’,’TEST2’); 或 sp_tabledef(‘TEST’,’STU’);

  8. 重命名表 alter table test1.STU rename to STUDENT;
  9. 增加或删除列 alter table test1.student add sfid varchar(18) ; alter table test1.student drop sfid;
  10. 表注释 COMMENT ON TABLE “TEST1”.”STU” IS ‘xue yuan’;
  11. 删除表 drop table test1.student;
  12. 查看用户所属的角色;

select from dba_role_privs where grantee=’test’;

  1. 查询角色的权限 select from dba_sys_privs where grantee=’PUBLIC’;

  2. 权限授予和收回

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;

  1. 查看表的约束信息; select * from dba_constraints where table_name=’TEST3’; dba_constraints 表结构见管理员手册ORACLE兼容视图12节;
  2. 创建约束 –检查约束 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));

  3. 禁用、启用约束 alter table test.TEST3 disable constraint CONS134218845; alter table test.TEST3 enable constraint CONS134218845;

  4. 创建或更新、修改视图 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;

  1. 自增列; 创建 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;
  2. 同义词 全局同义词 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;

  1. 索引 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;

  1. 归档设置 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;
  2. 备份和还原 –全量备份 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

作业管理 –SYSJOBS:作业信息 select from SYSJOB.SYSJOBS; –SYSJOBSCHEDULES:作业调度信息 select from SYSJOB.SYSJOBSCHEDULES; –SYSJOBHISTORIES:作业历史信息 select * from SYSJOB.SYSJOBHISTORIES;


参考资料

本文会经常更新,请阅读原文: https://dashenxian.github.io/post/%E8%BE%BE%E6%A2%A6%E6%95%B0%E6%8D%AE%E5%BA%93%E5%B8%B8%E7%94%A8%E8%AF%AD%E5%8F%A5%E8%8C%83%E4%BE%8B ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 小神仙 (包含链接: https://dashenxian.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (125880321@qq.com)

登录 GitHub 账号进行评论