SQL statement used to query the foreign key information of a table's primary key

Source: Internet
Author: User
Tags join
In my BSOOC, I need an SQL statement to query the foreign key information of the table's primary key. Last night I studied it at AM and finally achieved this goal:
Oracle:
Select o. obj # as objectId, o. name AS tableName, oc. name AS constraintName,
Decode (c. type #, 1, 'C', 2, 'P', 3, 'u ',
4, 'R', 5, 'V', 6, 'O', 7, 'C ','? ') As constraintType,
Col. name AS columnName

From sys. con $ oc, sys. con $ rc,
Sys. obj $ ro, sys. obj $ o, sys. obj $ oi,
Sys. cdef $ c,
Sys. col $ col, sys. ccol $ cc, sys. attrcol $ ac
Where oc. con # = c. con #
And c. obj # = o. obj #
And c. rcon # = rc. con #()
And c. enabled = oi. obj #()
And c. robj # = ro. obj #()
And c. type #! = 8
And c. type #! = 12/* don't include log groups */
And c. con # = cc. con #
And cc. obj # = col. obj #
And cc. intcol # = col. intcol #
And cc. obj # = o. obj #
And col. obj # = ac. obj #()
And col. intcol # = ac. intcol #()
And o. name = 'Your table'

SQL Server:
SELECT sysobjects. id objectId,
OBJECT_NAME (sysobjects. parent_obj) tableName,
Sysobjects. name constraintName,
Sysobjects. xtype AS constraintType,
Syscolumns. name AS columnName
FROM sysobjects inner join sysconstraints
ON sysobjects. xtype in ('C', 'F', 'PK', 'uq', 'D ')
AND sysobjects. id = sysconstraints. constid
Left outer join syscolumns ON sysconstraints. id = syscolumns. id
WHERE OBJECT_NAME (sysobjects. parent_obj) = 'Your table'

Other databases have no time to implement.


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.