Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:46:26+00:00 2026-05-26T02:46:26+00:00

I have a large SQL database where I need to verify the structure of

  • 0

I have a large SQL database where I need to verify the structure of the tables and columns (not the data itself). So I need to generate a list of all of the tables, then for each table, all of its columns, then for each column, its data type, length/precision, ordinal position, and whether it’s part of the primary key for that table.

I can get most of what I need with the following query:

SELECT TABLE_NAME, ORDINAL_POSITION, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS

However, I’m not sure how to check whether a column is part of a primary key. Additionally, for those tables where the PK consists of more than one column, I want to know the ordinal position of each column within the key. The information I’ve found so far relates to setting the key rather than reading it.

I’m interested in doing this in both SQL Server and Oracle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:46:27+00:00Added an answer on May 26, 2026 at 2:46 am

    In SQL Server you can do

    SELECT K.TABLE_CATALOG, 
           K.TABLE_NAME, 
           K.COLUMN_NAME, 
           K.ORDINAL_POSITION 
    FROM   INFORMATION_SCHEMA.KEY_COLUMN_USAGE K 
           INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC 
             ON K.TABLE_CATALOG = TC.TABLE_CATALOG 
                AND K.TABLE_SCHEMA = TC.TABLE_SCHEMA 
                AND K.CONSTRAINT_NAME = TC.CONSTRAINT_NAME 
    WHERE  TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 
    

    or

    SELECT Object_name(C.OBJECT_ID) TABLE_NAME, 
           C.NAME, 
           IC.INDEX_COLUMN_ID 
    FROM   SYS.KEY_CONSTRAINTS K 
           INNER JOIN SYS.INDEX_COLUMNS IC 
             ON K.PARENT_OBJECT_ID = IC.OBJECT_ID 
                AND K.UNIQUE_INDEX_ID = IC.INDEX_ID 
           INNER JOIN SYS.COLUMNS C 
             ON IC.OBJECT_ID = C.OBJECT_ID 
                AND IC.COLUMN_ID = C.COLUMN_ID 
    WHERE  K.TYPE = 'PK' 
    

    IN ORACLE

    SELECT K.OWNER, 
           K.TABLE_NAME, 
           K.INDEX_NAME, 
           C.COLUMN_NAME, 
           C.COLUMN_POSITION 
    FROM   ALL_CONSTRAINTS K 
           INNER JOIN ALL_IND_COLUMNS C 
             ON K.OWNER = C.INDEX_OWNER 
                AND K.TABLE_NAME = C.TABLE_NAME 
                AND K.INDEX_NAME = C.INDEX_NAME 
    WHERE  K.CONSTRAINT_TYPE = 'P' 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing SQL Server database, where I store data from large specific
I have a rather large database exposed to my application using LINQ-to-SQL. I need
I have a large SQL script that creates my database (multiple tables, triggers, and
I have a very large database I need to diagram. The database is SQL
I have a rather large (many gigabytes) table of data in SQL Server that
I need to transfer a large number of rows from a SQL Server database
We have built a large database model (SQL Server) for our client and an
All - I need some help designing a table for a Postgre SQL database.
We have a SQL server database. To manipulate the data non-programmatically, I can use
I have a SQL Server database running in Full recovery. I need to remove

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.