Solved

SQL Server Management Studio - Create Primary Key From 2 Columns

Posted on 2014-11-09
3
137 Views
Last Modified: 2014-11-09
Hi

I have a SQL Table that contains two nvarchar columns called "Article" and "Site".
I have to create a primary Key that contains both of these. How would I do this in SQL
Server Management Studio
0
Comment
Question by:murbro
[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
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40431415
When creating the table...
CREATE TABLE your_table_name (
  Article nvarchar(50), 
  Site nvarchar(50), 
  CONSTRAINT pk_your_table_name PRIMARY KEY (Article, Site) 
GO

Open in new window


.. and if the table already exists..
ALTER TABLE your_table_name
ADD CONSTRAINT pk_your_table_name PRIMARY KEY (Article, Site) 
GO

Open in new window


Side comments:

1.

Better to use numeric values instead of char's, as the size of the columns in a primary key equates to faster querying, and char values (50x2 in this example) will be much better then say integers (4x2)

2.

In both examples I included the name pk_your_table_name, which is a best practice.  

Say you have three environments such as DEV, TEST, PROD, and the pk wasn't named.  If you ever had to alter the PK, if a name is not provided the system generates a name, so you'd have three different names to deal with when promoting code.
0
 

Author Closing Comment

by:murbro
ID: 40431512
Great answer. Thanks very much
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40431573
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

729 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