473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

search the primary key given the table name

Hi all,

How can get the primary key string from the given table name? i know
it should from system tables of "sysobjects, syscolumns, and
sysconstraints", but when i execute the statement like that:

select a.name from syscolumns a,sysobjects b,sysconstraints c
where a.id = b.id and b.name ='Agreement' and a.id = c.id and a.colid
= c.colid and c.status = 1

i can't get the primary key out, what the trick here? bye the sql help
file,
'status' in sysconstraints table:
1 = PRIMARY KEY constraint.
2 = UNIQUE KEY constraint.

what is exact value refers to PRIMARY KEY constraint?

thanks,

Robert
Jul 20 '05 #1
1 19830
Robert Song (ro*********@mda-software.com) writes:
How can get the primary key string from the given table name? i know
it should from system tables of "sysobjects, syscolumns, and
sysconstraints", but when i execute the statement like that:

select a.name from syscolumns a,sysobjects b,sysconstraints c
where a.id = b.id and b.name ='Agreement' and a.id = c.id and a.colid
= c.colid and c.status = 1

i can't get the primary key out, what the trick here? bye the sql help


The trick is that sysconstraints is a dead end. colid only applies
for column constraints, and a PK is a table constraint; at least a
multi-colummn constraint is.

This query should give you what you want:

use Northwind
go
select c.name
from sysindexes i
join sysobjects o ON i.id = o.id
join sysobjects pk ON i.name = pk.name
AND pk.parent_obj = i.id
AND pk.xtype = 'PK'
join sysindexkeys ik on i.id = ik.id
and i.indid = ik.indid
join syscolumns c ON ik.id = c.id
AND ik.colid = c.colid
where o.name = 'Order Details'
order by ik.keyno
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
9
by: Chris Bowlby | last post by:
Hi All, I've noticed that in the pg_type system table, there is a data type called "name", would that represent the definition of the table name space, including the max length a talbe name...
1
by: rshivaraman | last post by:
Hi All : A couple of tables have been identified to be deleted. My job is to find if it is at all used. On searching the web, i found a proc to search for a string within all databases in a...
3
by: ramakanth05 | last post by:
Hi, I have a problem pulling data from a table using the parameter as table name. I have three tables tbl1, tbl2, tbl3 and I want all the records of the table based on the parameter given. ...
1
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: How can I get the IP for web site when I have the Domain Name ? To make my question clear: For example when you go to www.netcraft.com and search for any web site by given the...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
1
by: jgentes | last post by:
I am in the process of normalizing and re-creating a database that was a total maintanence mess. Currently the database has 640 tables. each table is an assembly and contains the data of its sub...
2
by: nanabuch | last post by:
Hello, I posted yesterday a very complex question, but I did not receive a adequate answer so I decided to simplify things, and I will try to figure out the rest of the problem :). Ok, I need...
2
by: ketanm | last post by:
Hi all, I have to get all table and it's row and column entry. let say this is the url http://hg.sagemath.org and it has four table name,Description,Contact,Last change and each table has some...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.