Get Oracle Schema

我的未来我决定 提交于 2019-12-24 03:48:50

问题


Is there any way to retrieve oracle schema in c#? These are what I need: Tables_Name, Table Columns_Name, Primary Keys, Unique Keys, Foreign Keys


回答1:


Table Names:

select * from user_tables

Table Column Names:

select * from user_tab_columns

Primary Keys, Unique Keys, Foreign Keys:

select * from user_constraints

More generally:

select * from dictionary

to see all the possible system views you can use.

If you want the actual DDL used to create the tables etc you can use dbms_metadata.get_ddl, which returns a CLOB.

For instance:

select dbms_metadata.get_ddl('TABLE','MY_TABLE') from dual;



回答2:


You can select from the data dictionary tables in Oracle just like you would any other tables. Here is a discussion of the data dictionary tables - Data Dictionary




回答3:


Use the DBMS_METADATA package. For example, to get the CREATE scripts for all the tables, you'd do:

SELECT DBMS_METADATA.GET_DDL('TABLE', u.table_name)
  FROM USER_ALL_TABLES u

This way, with a little effort, you can get DDL scripts for the whole schema. And there are more examples on the DBMS_METADATA documetation page.




回答4:


I use the OracleConnection.GetSchema method. I wrote myself a helper method to retrieve per table.

private class SchemaInfo
{
    public bool Mandatory { get; set; }
    public int MaxLength { get; set; }
}

private Dictionary<string, SchemaInfo> _getSchemaInfo(OracleConnection _cn, string owner, string tableName)
{
    DataTable _dt = _cn.GetSchema("Columns", new string[3] { owner, tableName, null });
    Dictionary<string, SchemaInfo> _dict = new Dictionary<string, SchemaInfo>();
    for (int x = 0; x < _dt.Rows.Count; x++)
    {
        DataRow _r = _dt.Rows[x];
        SchemaInfo _si = new SchemaInfo();
        object maxl = _r.ItemArray[10];
        if (maxl == DBNull.Value) maxl = -1;
        _si.Mandatory = (_r.ItemArray[8].ToString().Equals("N")) ? true : false;
        _si.MaxLength = Convert.ToInt32((maxl ?? 0));
        _dict.Add(_r.ItemArray[2].ToString(), _si);
    }
    return _dict;
}

You will have to look up the other elements that are returned to get keys, etc. I am pretty sure that is available (but could be wrong).

If you want to get Tables, use Connection.GetSchema("Tables", new string[3] { owner, null, null });



来源:https://stackoverflow.com/questions/12270274/get-oracle-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!