Database built-in view or table structure usage scenarios in development

Source: Internet
Author: User

1. Querying all tables of the user

2. Query table relation, foreign key, primary key, constraint condition

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

Oracle Database:

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 joins User_cons_columns B on A.constraint_name=b.constraint_n AME "+
"Left joins user_constraints C on c.r_constraint_name = A.constraint_name left joins User_cons_columns D on C.constraint_nam E= "+
"D.constraint_name WHERE a.constraint_type= ' P ' and NVL (C.table_name, ')! =" and NVL (D.column_name, ')! = "ORDER by A.T Able_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 the owner in (' DBO ', ' VHBIYF ')" result ";
return SQL;

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

Sql server:

String rssql = "Select C.name as column_name from sysindexes I joins Sysindexkeys K on I.id=k.id and I.indid=k.indid"     + "Join SYSOBJECTS O i.id = o.id join syscolumns C on i.id=c.id and K.colid=c.colid"
&nbs p;   + "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 joins 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) joins 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.co Lumns Maincol "+
"On (omain.object_id = maincol.object_id and fkcols.referenced_column_id = maincol.column_id)";

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

Database built-in view or table structure usage scenarios in development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.