当前位置:   article > 正文

oracle元数据获取_oracle 获取 创建表空间元数据

oracle 获取 创建表空间元数据
  1. -- 查询表名,表注释,字段注释
  2. select ATC.OWNER,
  3. atC.TABLE_NAME,
  4. ucc.comments,
  5. ATC.COLUMN_NAME,
  6. ATC.DATA_TYPE,
  7. ATC.DATA_LENGTH,
  8. ATC.NULLABLE,
  9. ucc.comments
  10. from (select ATC.OWNER,
  11. atC.TABLE_NAME,
  12. ATC.COLUMN_NAME,
  13. ATC.DATA_TYPE,
  14. ATC.DATA_LENGTH,
  15. ATC.NULLABLE
  16. from all_tab_columns ATC
  17. where ATC.owner in ('USER_1')) atc
  18. left outer join user_col_comments ucc on atc.table_name = ucc.table_name
  19. and atc.column_name =
  20. ucc.column_name
  21. left outer join user_tab_comments utc on atc.table_name = utc.table_name
  22. order by atc.table_name, atc.column_name;
  23. select * from user_tab_comments
  24. -- 表注释SQL
  25. select 'comment on table ' || atC.TABLE_NAME || ' is -' ||
  26. utc.comments || '-;'
  27. from (select ATC.OWNER,
  28. atC.TABLE_NAME,
  29. ATC.COLUMN_NAME,
  30. ATC.DATA_TYPE,
  31. ATC.DATA_LENGTH,
  32. ATC.NULLABLE
  33. from all_tab_columns ATC
  34. where ATC.owner in ('USER_1')) atc
  35. left outer join user_tab_comments utc on atc.table_name = utc.table_name
  36. order by atc.table_name, atc.column_name;
  37. -- 字段注释
  38. select 'comment on column '||atC.TABLE_NAME||'.'||ATC.COLUMN_NAME||' is -'||ucc.comments||'-;'
  39. from (select ATC.OWNER,
  40. atC.TABLE_NAME,
  41. ATC.COLUMN_NAME,
  42. ATC.DATA_TYPE,
  43. ATC.DATA_LENGTH,
  44. ATC.NULLABLE
  45. from all_tab_columns ATC
  46. where ATC.owner in ('USER_1')) atc
  47. left outer join user_col_comments ucc on atc.table_name = ucc.table_name
  48. and atc.column_name =
  49. ucc.column_name
  50. left outer join user_tab_comments utc on atc.table_name = utc.table_name
  51. order by atc.table_name, atc.column_name;

`SELECT F_LIMS_GET_SQL_FOR_MYSQL('T_LIMS_EQUIPMENT') FROM DUAL;

  1. CREATE OR REPLACE FUNCTION F_LIMS_GET_SQL_FOR_MYSQL(PI_TABLENAME IN VARCHAR2,
  2. PI_ISDROP IN INTEGER := 1)
  3. RETURN CLOB IS
  4. V_TABLENAME VARCHAR(100);
  5. V_RET_SQL VARCHAR2(4000);
  6. V_SQL_HEADER VARCHAR2(1000);
  7. V_SQL_BODY CLOB;
  8. V_SQL_INDEX VARCHAR2(4000);
  9. V_SQL_TABLE_COMMENT VARCHAR(400);
  10. V_SQL_PK_COL_LIST VARCHAR(1000);
  11. -- 根据当前数据库和表名,生成mysql建表语句
  12. BEGIN
  13. -- 表名统一为大写
  14. SELECT UPPER(PI_TABLENAME) INTO V_TABLENAME FROM DUAL;
  15. SELECT MAX(T.COMMENTS)
  16. INTO V_SQL_TABLE_COMMENT
  17. FROM USER_TAB_COMMENTS T
  18. WHERE T.TABLE_NAME = V_TABLENAME;
  19. SELECT MAX(WM_CONCAT('`' || A.COLUMN_NAME || '` '))
  20. INTO V_SQL_PK_COL_LIST
  21. FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
  22. WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
  23. AND B.CONSTRAINT_TYPE = 'P'
  24. AND A.TABLE_NAME = V_TABLENAME
  25. GROUP BY A.TABLE_NAME;
  26. V_SQL_HEADER := '-- ' || V_SQL_TABLE_COMMENT || ' 建表语句' || CHR(13);
  27. IF PI_ISDROP = 1 THEN
  28. V_SQL_HEADER := V_SQL_HEADER || 'DROP TABLE IF EXISTS `' || V_TABLENAME || '`;' ||
  29. CHR(13);
  30. END IF;
  31. V_SQL_HEADER := V_SQL_HEADER || 'CREATE TABLE `' || V_TABLENAME || '` (';
  32. -- 表列信息
  33. SELECT WM_CONCAT(CHR(13) || '`' || T.COLUMN_NAME || '` ' ||
  34. (CASE
  35. WHEN T.DATA_TYPE = 'VARCHAR2' OR T.DATA_TYPE = 'NVARCHAR2' THEN
  36. 'VARCHAR(' || T.DATA_LENGTH || ')'
  37. WHEN T.DATA_TYPE = 'DATE' THEN
  38. 'DATETIME'
  39. WHEN T.DATA_TYPE = 'CLOB' THEN
  40. 'MEDIUMTEXT'
  41. WHEN T.DATA_TYPE = 'NUMBER' THEN
  42. 'DECIMAL'
  43. WHEN T.COLUMN_NAME = 'ID' THEN
  44. 'VARCHAR(32)'
  45. ELSE
  46. T.DATA_TYPE
  47. END) || DECODE(T.NULLABLE, 'N', ' NOT NULL ', '') ||
  48. ' COMMENT ''' || TC.COMMENTS || '''')
  49. INTO V_SQL_BODY
  50. FROM USER_TAB_COLUMNS T
  51. LEFT JOIN USER_COL_COMMENTS TC
  52. ON T.TABLE_NAME = TC.TABLE_NAME
  53. AND T.COLUMN_NAME = TC.COLUMN_NAME
  54. WHERE T.TABLE_NAME = V_TABLENAME
  55. ORDER BY T.COLUMN_ID;
  56. IF V_SQL_PK_COL_LIST IS NOT NULL THEN
  57. V_SQL_BODY := V_SQL_BODY || ' ,' || CHR(13) || ' PRIMARY KEY (' ||
  58. V_SQL_PK_COL_LIST || ') ';
  59. END IF;
  60. -- 表备注
  61. V_SQL_BODY := V_SQL_BODY || CHR(13) || ') COMMENT = ''' ||
  62. V_SQL_TABLE_COMMENT || ''';' || CHR(13);
  63. -- 唯一索引
  64. SELECT (WM_CONCAT('@ALTER TABLE `' || A.TABLE_NAME ||
  65. '` ADD UNIQUE INDEX (' ||
  66. WM_CONCAT('`' || A.COLUMN_NAME || '`') || ');@'))
  67. INTO V_SQL_INDEX
  68. FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
  69. WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
  70. AND B.CONSTRAINT_TYPE = 'U'
  71. AND A.TABLE_NAME = V_TABLENAME
  72. GROUP BY A.TABLE_NAME, A.CONSTRAINT_NAME;
  73. IF V_SQL_INDEX IS NOT NULL THEN
  74. V_SQL_INDEX := REPLACE(REPLACE(V_SQL_INDEX, '@,@', CHR(13)), '@', '');
  75. V_SQL_INDEX := '-- 创建索引' || CHR(13) || V_SQL_INDEX;
  76. END IF;
  77. RETURN V_SQL_HEADER || V_SQL_BODY || V_SQL_INDEX;
  78. END;

  1. 获取表:
  2. select table_name from user_tables; //当前用户的表
  3. select table_name from all_tables; //所有用户的表
  4. select table_name from dba_tables; //包括系统表
  5. select table_name from dba_tables where owner='用户名'
  6. user_tables:
  7. table_name,tablespace_name,last_analyzed等
  8. dba_tables:
  9. ower,table_name,tablespace_name,last_analyzed等
  10. all_tables:
  11. ower,table_name,tablespace_name,last_analyzed等
  12. all_objects:
  13. ower,object_name,subobject_name,object_id,created,last_ddl_time,timestamp,status
  14. 获取表字段:
  15. select * from user_tab_columns where Table_Name='用户表';
  16. select * from all_tab_columns where Table_Name='用户表';
  17. select * from dba_tab_columns where Table_Name='用户表';
  18. user_tab_columns
  19. table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等
  20. all_tab_columns
  21. ower,table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等
  22. dba_tab_columns
  23. ower,table_name,column_name,data_type,data_length,data_precision,data_scale,nullable,column_id等
  24. 获取表注释:
  25. select * from user_tab_comments
  26. user_tab_comments:table_name,table_type,comments
  27. 相应的还有dba_tab_comments,all_tab_comments,这两个比user_tab_comments多了ower列。
  28. 获取字段注释:
  29. select * from user_col_comments
  30. user_col_comments:table_name,column_name,comments
  31. 相应的还有dba_col_comments,all_col_comments,这两个比user_col_comments多了ower列。
  32. 获取主键:
  33. select * from user_constraints
  34. where table_name = 'AAA'
  35. and constraint_type ='P'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/559823
推荐阅读
相关标签
  

闽ICP备14008679号