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
Solved

SQL Server Management Studio - Create Primary Key From 2 Columns

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

860 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