类别:数据库

日期:2021-07-05 浏览:1747 评论:0

-- 用户和模式 

-- 用户(user):用来连接数据库访问数据库 

-- 模式(schema):模式对象是数据库数据的逻辑结构 

-- 关系:模式跟用户在oracle 是一对一的关系,db2 一个用户可以对应多个模式,dm也是一对多(mysql中没有模式概念)

--模式规则

-- 在同一模式下不能存在同名对象,但在不同模式中的对象名称可以相同

-- 新建表时候,可以指定表空间。如果不指定,则默认保存到MAIN

-- 用户可以直接访问其他模式对象,但如果要要访问其他模式对象,则必须具有对象权限

-- 当用户要访问其他模式对象时,必须附加模式名作后缀(schema.table)

--------------------------------------------------------------------------------------------------------------------------------

-- 数据大小

-- 数据库占用空间(kb为单位)

SELECT SUM(bytes) from dba_data_files; 

-- 数据库读写占用

SELECT * from v$datafile; 

-- 表大小,大小写敏感

SELECT TABLE_USED_SPACE('SYSDBA', 'D') * 1024; 

--------------------------------------------------------------------------------------------------------------------------------

-- 归档模式

-- 归档模式是否打开

select arch_mode from v$database; 

-- 归档信息

select * from V$DM_ARCH_INI;

--------------------------------------------------------------------------------------------------------------------------------

-- 执行SQL错误码

select * from V$ERR_INFO where code = '-6111';

select count(*) from V$ERR_INFO

--------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------    表空间相关                 -------------------------------------------------------

-- 查询所有表空间

select * from V$TABLESPACE;

-- 查询模式

SELECT * FROM dba_segments;

-- 通过系统视图模式查找

SELECT object_name from all_objects where object_type = 'SCH';

-- 查询系统视图

select * from SYSTABLECOMMENTS; 

-- 查询dba总表数量

select count(*) from dba_tables;

-- 查询用户所有表

select * from user_tables;

-- 查询dba表空间

select * from dba_tablespaces;

-- 查询用户表空间

select * from user_tablespaces;

-- 查询所有对象

SELECT * from all_objects;

-- 查询dba对象

select * from dba_objects;

-- 查询用户对象

select * from user_objects;

-- 查询dba用户

select * from dba_users;

-- 实例名

select distinct value from v$parameter; 

--------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------    表属性相关操作          ------------------------------------------------------

-- 建表语句

create table tests ( id char not null) 


-- 获取表字段

select * from user_tab_columns where Table_Name='test';

select * from user_tab_columns where Table_Name='test_table';


-- 表重命名

alter table test rename to test_table


-- 查询表注释

select comments from user_tab_comments where table_name = 'test_table'


-- 修改表注释

comment on table test_table is '测试';

--------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------    列属性相关操作          -------------------------------------------------------

-- 增加列

alter table test_table add ids int


-- 增加一列不为空

alter table test_table add id_notnull int not null


-- 类型int 小于9999 主键 

alter table test_table add id_int int not null primary key check(id_int < 9999)


-- 类型 varchar2 长度 2

alter table test_table add id_varchar2 varchar(2)


-- 删除列,列名:ids 

alter table test_table drop ids


-- 修改列名

ALTER TABLE 表名 RENAME COLUMN 以后列名 TO 新列名;

alter table test_table rename column idss to ids


-- 查询列注释

select * from user_col_comments where owner = 'zfgxpt' and table_name = 'test_table' and column_name = 'id'


-- 修改列注释

comment on column test_table.id is '主键'; 


-----------------------------------------------------    列数据相关操作          -------------------------------------------------------

-- 查询数据

select * from test_table;

select * from test_table where id like 'id%';

-- 插入一条数据

insert into test_table (id,id_notnull,id_int,id_varchar2,ids) values ('id1',1,2,'idvarchar2',3);

-- 更新

update test_table set id = '123';

--------------------------------------------------------------------------------------------------------------------------------


本文标题:达梦数据库常用SQL整理
本文链接:https://vtzw.com/post/687.html
作者授权:除特别说明外,本文由 零一 原创编译并授权 零一的世界 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
 您阅读本篇文章共花了: 

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。