How Oracle Gets the primary key column name of the table and how to get all the column names of the table

Source: Internet
Author: User

Gets the primary key column name of the table 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 : table names are capitalized
Perform the test, the table all the primary keys are detected, the primary key column name is in the fourth column

Get all column names for a table SQL

SELECT * FROM table_name where rownum=1


This SQL statement believes everyone knows what it means.

Using JDBC programming to check the primary key column name, can be queried by the first statement above.
There is also a way to query the table's primary key column names, using JDBC Programming, the code is as follows:

DatabaseMetaData dbmd= conn.getmetadata ();
rs = Dbmd.getprimarykeys (Null,null,tablename.touppercase ()); To capitalize the table name to properly remove the primary key

while (Rs.next ()) {
Columnname=rs.getstring (4);
System.out.println (ColumnName);
}

Use JDBC to get all the column names of the table, or through the second SQL statement above, the code is as follows:

String sql= "SELECT * FROM table_name where rownum=1";
Stm=conn.createstatement ();
Rs=stm.executequery (SQL);
Rs.next ();

ResultSetMetaData Metadata=rs.getmetadata ();
for (int i=1;i<=metadata.getcolumncount (); i++) {
System.out.println (Metadata.getcolumnname (i)); }

How Oracle Gets the primary key column name of the table and how to get all the column names of the table

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.