Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

After i add primary key, it doesn't show up in Design mode of the table

Posted on 2011-03-11
5
Medium Priority
?
322 Views
Last Modified: 2012-05-11
Hi, I'm using sql 2005.  I use the script below to add the primary key and it executed successfully.  However, the primary key doesn't show up in the design mode.  I can see the PK in the tree on the left under Columns and the Keys folder.  
I have to manually do it in design again for this to show up.  What's wrong here and how can I fix this script.  I need to create this script for several tables that needs this done  and repeat this a couple more times.
Another question, since I can ssis to work (sql 2005 and vs2008 don't work together), I'm just creating TSql SP to clean up and transform a imported database.  I'm just curious, does SSIS really would save much more work from scripting if I were to use that instead of coding the TSQL sp?
Thank you.
Here is my script,
alter table dbo.q_history_type add HistoryTypeId int identity(1,1) not null
constraint [PK_dbo.q_history_type_HistoryTypeId] primary key clustered (HistoryTypeId asc)
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
8080_Diver earned 1000 total points
ID: 35117683
Did you, by any chance, happen to have opened the table in Design mode before you executed the script in another query window?  If so, the query window with the table design init is a static copy of the design of the table before you modified it with the script.  In other words, when it was opened, there was no such Primary Key, so it has no idea one now exists.

You can also use the sp_foreachtable (system) stored procedure with a script that only adds the identity column if a PK doesn't exist on the table.  
0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 1000 total points
ID: 35128582
I just ran this script against a SQL 2005 test server I have and they key now shows up in design view with no issues, so your script is correct.

Do you see your key in this list of you run the second script in this link?

http://blogs.msdn.com/b/sqltips/archive/2005/09/16/469136.aspx

If so, then it's set up correctly. Try closing and re-opening your design view of the table, as it only refreshes the view when that screen loads. Opening the design view after your script has finished running should show the key - if it doesn't, can you post the version (full number, like 9.0.4053) of both your SQL Server and your SSMS?
0
 

Author Comment

by:lapucca
ID: 35131060
No, the table is not opened in any way when I executed the script.  However, I have database diagram that's opened at the time.  Would that affect it?  Thanks.

Ray, I don't know what you mean by "Do you see your key in this list of you run the second script in this link?"  I tried refresh the whole database and it still doesn't show up.  Thanks.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 35131423
lapucca,

Did you refresh the database with the Diagram still open?
If you try to create the PK again (without having the Diagram open), does the database engine object and tell you it already exists?

Did you run either of the scripts in the link taht Ray provided?  If not, try doing so and then let us know if the index shows up; if you did run one of the scripts, did the PK show up?
0
 

Author Closing Comment

by:lapucca
ID: 35143479
I think it;s cause by open diagram.  thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

647 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question