Solved

SQL Server Management Studio - Create Primary Key From 2 Columns

Posted on 2014-11-09
3
127 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
  • 2
3 Comments
 
LVL 65

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 65

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now