Query basic table field information, primary key, and foreign key in Oracle

Source: Internet
Author: User

Because the data structure design of some modules of the project is not strictly in accordance with certain specifications, the data structure can only be queried from the database. The information to be queried is as follows: field name, data type, whether it is null, default value, primary key, foreign key, and so on.

I searched for the above information on the Internet and summarized it as follows:

I. query basic table information

Select
Utc. column_name, utc. data_type, utc. data_length, utc. data_precision,
Utc. data_Scale, utc. nullable, utc. data_default, ucc. comments
From
User_tab_columns utc, user_col_comments ucc
Where
Utc. table_name = ucc. table_name
And utc. column_name = ucc. column_name
And utc. table_name = 'onlinexls'
Order
Column_id

Note: order by column_id indicates that the result is displayed in the order of data structure design.

Ii. query the table's primary key

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 = 'onlinexls'
Iii. query table foreign keys

Select
Distinct (ucc. column_name) column_name, rela. table_name, rela. column_name column_name1
From
User_constraints uc, user_cons_columns ucc,
(Select t2.table _ name, t2.column _ name, t1.r _ constraint_name from Region t1, user_cons_columns t2 where t1.r _ constraint_name = t2.constraint _ name and t1.table _ name = 'onlinexls') rela
Where
Uc. constraint_name = ucc. constraint_name
And uc. r_constraint_name = rela. r_constraint_name
And uc. table_name = 'onlinexls'

With the preceding SQL statements, combined with the program (such as java + jxl), you can output the Excel report as shown in.

Related Article

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.