Solved

SQL Server Management Studio - Create Primary Key From 2 Columns

Posted on 2014-11-09
3
135 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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CASE Statement for unknown # of columns 10 66
SQL syntax for max(date) 3 35
SQL Percentage Formula 7 30
sql server string_split 4 23
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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

739 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