数据库内置视图或者表结构在开发中的使用场景

1.查询用户的所有表

2.查询表关系,外键,主键,约束条件

=========================================================

oracle数据库:

String reSql = "SELECT A.TABLE_NAME AS P_TABLE_NAME,B.COLUMN_NAME AS P_COLUMN_NAME,C.TABLE_NAME AS F_TABLE_NAME,"+
   "D.COLUMN_NAME AS F_COLUMN_NAME FROM USER_CONSTRAINTS A LEFT JOIN USER_CONS_COLUMNS B ON A.CONSTRAINT_NAME=B.CONSTRAINT_NAME "+
   "LEFT JOIN USER_CONSTRAINTS C ON C.R_CONSTRAINT_NAME = A.CONSTRAINT_NAME LEFT JOIN USER_CONS_COLUMNS D ON C.CONSTRAINT_NAME= "+
   "D.CONSTRAINT_NAME WHERE A.CONSTRAINT_TYPE=‘P‘ AND NVL(C.TABLE_NAME,‘ ‘)!=‘ ‘ AND NVL(D.COLUMN_NAME,‘ ‘)!=‘ ‘ ORDER BY A.TABLE_NAME";


String rsSql = "SELECT COL.COLUMN_NAME FROM USER_CONSTRAINTS CON,USER_CONS_COLUMNS COL "
   + "WHERE CON.CONSTRAINT_NAME = COL.CONSTRAINT_NAME AND CON.CONSTRAINT_TYPE=‘P‘ AND COL.TABLE_NAME=‘"
   + tableName + "‘ ORDER BY COLUMN_NAME";

String sql = "(select TABLE_NAME AS name,‘U‘ as xtype from ALL_TABLES where OWNER IN (‘DBO‘,‘VHBIYF‘) "+
  "union all "+
  "select VIEW_NAME AS name,‘V‘ as xtype from all_views where owner IN (‘DBO‘,‘VHBIYF‘) ) result";
  return sql;

=========================================================

sqlserver:

String rsSql = "SELECT C.NAME AS COLUMN_NAME FROM SYSINDEXES I JOIN SYSINDEXKEYS K ON I.ID=K.ID AND I.INDID=K.INDID "
    + "JOIN SYSOBJECTS O ON I.ID = O.ID JOIN SYSCOLUMNS C ON I.ID=C.ID AND K.COLID=C.COLID "
    + "WHERE O.XTYPE=‘U‘ AND EXISTS(SELECT 1 FROM SYSOBJECTS WHERE XTYPE=‘PK‘ AND NAME=I.NAME) "
    + "AND O.NAME=‘"+tableName+"‘ ORDER BY O.NAME,K.COLID";

 String reSql="select oMain.name  AS  P_TABLE_NAME,MainCol.name AS P_COLUMN_NAME,oSub.name  AS  F_TABLE_NAME,SubCol.name AS F_COLUMN_NAME "+
    "from sys.foreign_keys fk JOIN sys.all_objects oSub ON (fk.parent_object_id = oSub.object_id) JOIN sys.all_objects oMain "+ 
    "ON (fk.referenced_object_id = oMain.object_id) JOIN sys.foreign_key_columns fkCols ON (fk.object_id = fkCols.constraint_object_id) "+
    "JOIN sys.columns SubCol ON (oSub.object_id = SubCol.object_id AND fkCols.parent_column_id = SubCol.column_id) JOIN sys.columns MainCol "+ 
    "ON (oMain.object_id = MainCol.object_id AND fkCols.referenced_column_id = MainCol.column_id)";

=========================================================

数据库内置视图或者表结构在开发中的使用场景

上一篇:【oracle11g,17】存储结构: 段的类型,数据块(行连接、行迁移,块头),段的管理方式,高水位线


下一篇:mysql增加自定义函数功能