How Does Oracle obtain the primary key column name of a table and all column names of a table?

Source: Internet
Author: User

Obtain the table's primary key column name SQL

Select * From user_cons_columns
Where constraint_name = (select constraint_name from user_constraints
Where table_name = 'bst _ favorite 'and constraint_type = 'P ');

Remember: The table name must be in uppercase.
Run the following command to check all the primary keys in the table. The primary key column is in the fourth column.

Obtain all column names of a table.

Select * From table_name where rownum = 1

I believe everyone knows what this SQL statement means.

You can use the first statement above to query the primary key column name through JDBC programming.
Another method is to query the table's primary key column name by using JDBC programming. The Code is as follows:

Databasemetadata dbmd = conn. getmetadata ();
Rs = dbmd. getprimarykeys (null, null, tablename. touppercase (); // You must convert the table name to uppercase to correctly retrieve the primary key.

While (Rs. Next ())...{
Columnname = Rs. getstring (4 );
System. Out. println (columnname );
}

You can use the second SQL statement to obtain all the column names of a table using JDBC. The Code is as follows:

String SQL = "select * From table_name where rownum = 1 ";
STM = conn. createstatement ();
Rs1_cmd.exe cutequery (SQL );
Rs. Next ();

Resultsetmetadata metadata = Rs. getmetadata ();
For (INT I = 1; I <= metadata. getcolumncount (); I ++ )...{
System. Out. println (metadata. getcolumnname (I ));}

 

 

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.