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

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)";

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。