 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
Olegon
Joined: 07 Sep 2009 Posts: 40
|
|
Wrong key column displayed |
|
When primary key defined in CREATE TABLE instruction.
Example:

|
|
Thu Apr 19, 2012 6:10 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
You've got a syntax error, 2 actually, there is a comma missing before PRIMARY KEY and constraint name.
PRIMARY KEY keyword can appear either immediately after the key column or as a separate syntax element. In the last case constraint name is required.
|
|
Thu Apr 19, 2012 7:29 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
There is a confusion caused by CREATE TABLE definition not being strict enough (in my opinion). The one SysOp marked as a syntax error is a grave result of it. It's syntactically correct but very confusing and does not do what you would think it should. There are many ways to define the primary key, therefore it can be quite complicated to retrieve it correctly. Those below are all valid (and it does not cover all possibilities) and since SA does not show them correctly it might not cover all of them:
 |
 |
CREATE TABLE #ltSomeTable
(
|
0.
 |
 |
id INT NOT NULL PRIMARY KEY
,sub_id INT NOT NULL
,comment NVARCHAR(20) NULL
|
or 1.
 |
 |
id INT NOT NULL PRIMARY KEY (id, sub_id)
,sub_id INT NOT NULL
,comment NVARCHAR(20) NULL
|
or 3.
 |
 |
id INT NOT NULL
,sub_id INT NOT NULL PRIMARY KEY (id, sub_id)
,comment NVARCHAR(20) NULL
|
or 4.
 |
 |
id INT NOT NULL
,sub_id INT NOT NULL
,comment NVARCHAR(20) NULL
,CONSTRAINT pk_ltsometable PRIMARY KEY (id, sub_id)
|
or 5.
 |
 |
id INT NOT NULL
,sub_id INT NOT NULL
,comment NVARCHAR(20) NULL
,PRIMARY KEY(id, sub_id)
|
or 6. (see the lack of comma)
 |
 |
id INT NOT NULL
,sub_id INT NOT NULL
,comment NVARCHAR(20) NULL
PRIMARY KEY(id, sub_id)
|
 |
 |
)
|
#6 (without the comma) is primarily used to mark the single column it is place after as a primary key (see #0).
All of these (except #0.) seem to yield the same result (that is, id and sub_id being the primary key) and SA is not very consistent what to mark as primary key except for the missing comma case where it definitely show the wrong colum. But even refreshing the cache does stir things sometimes removing or adding the key icon to arbitrary columns.
Referring to the temp table with its full name
 |
 |
SELECT
*
FROM tempdb.dbo.#ltSomeTable
|
will show the correct primary key, though it has to be created first for that to work.
EDIT: all the above is for SQL Server 2k8, it might not apply to other DBMSes.
|
|
Thu Apr 19, 2012 7:44 am |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
Thu Apr 19, 2012 8:59 am |
|
 |
gemisigo
Joined: 11 Mar 2010 Posts: 2165
|
|
|
|
Alas, my knowledge is insufficient there. I only use #0 and #4 + ALTER TABLE ADD CONTSTRAINT clause, that's for sure. I know no reason for the presence of the others.
While #0 is identified correctly by SA, #4 is not (or so it seems).
|
|
Thu Apr 19, 2012 10:55 am |
|
 |
judahr
Joined: 09 Mar 2007 Posts: 319 Country: United States |
|
|
|
I use #5 quite a bit for temp tables. SA picks it up as a column.
|
|
Thu May 10, 2012 6:05 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7948
|
|
|
|
Case #5 will be supported in version 6.2. Please wait for a few more days.
|
|
Thu May 10, 2012 6:32 pm |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|