[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

hat's the tsql code for creating primary key out of the 2 columns

Posted on 2011-03-22
4
Medium Priority
?
437 Views
Last Modified: 2012-05-11
Hi, I'm altering an existing table to a manyTomany table.  
1.  Does manyTomany need Pk?
2.  What's the tsql code for creating primary key out of the 2 columns, personID, Position_ID?
Thanks.  I know I have to drop the current primary key first, PersonIs_ID
Below is the table definition.  
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[personis](
	[PersonIs_ID] [int] IDENTITY(4221,1) NOT NULL,
	[personID] [int] NOT NULL DEFAULT ((0)),
	[Position_ID] [int] NOT NULL DEFAULT ((0)),
	[Position_Title] [nvarchar](max) NULL,
	[PrimarySecondary] [nvarchar](20) NULL DEFAULT (N'Primary'),
	[Position_Bio] [nvarchar](max) NULL,
	[Position_Narrative] [nvarchar](max) NULL,
	[Position_Narrative_short] [nvarchar](200) NULL DEFAULT (NULL),
	[web_visible] [smallint] NULL DEFAULT ((1)),
	[picture] [nvarchar](100) NULL DEFAULT (NULL),
 CONSTRAINT [PK_personis_PersonIs_ID] PRIMARY KEY CLUSTERED 
(
	[PersonIs_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[personis]  WITH CHECK ADD  CONSTRAINT [FK_personis_person] FOREIGN KEY([personID])
REFERENCES [dbo].[person] ([personID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[personis] CHECK CONSTRAINT [FK_personis_person]

Open in new window

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
4 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 35192696
CREATE TABLE [dbo].[personis](
      [PersonIs_ID] [int] IDENTITY(4221,1) NOT NULL,
      [personID] [int] NOT NULL DEFAULT ((0)),
      [Position_ID] [int] NOT NULL DEFAULT ((0)),
      [Position_Title] [nvarchar](max) NULL,
      [PrimarySecondary] [nvarchar](20) NULL DEFAULT (N'Primary'),
      [Position_Bio] [nvarchar](max) NULL,
      [Position_Narrative] [nvarchar](max) NULL,
      [Position_Narrative_short] [nvarchar](200) NULL DEFAULT (NULL),
      [web_visible] [smallint] NULL DEFAULT ((1)),
      [picture] [nvarchar](100) NULL DEFAULT (NULL),
 CONSTRAINT [PK_personis_PersonIs_ID] PRIMARY KEY CLUSTERED
(
      [PersonIs_ID] ASC, Position_ID ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[personis]  WITH CHECK ADD  CONSTRAINT [FK_personis_person] FOREIGN KEY([personID])
REFERENCES [dbo].[person] ([personID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[personis] CHECK CONSTRAINT [FK_personis_person]
0
 

Author Comment

by:lapucca
ID: 35193236
The create table code is just to show you the table information.  I'm looking for tsql code to create a PK using those 2 columns. Thank you.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 35193264
I already have it in there...but, a separate statement would be:

alter table personis
add CONSTRAINT [PK_personis_PersonIs_ID] PRIMARY KEY CLUSTERED
(
      [PersonIs_ID] ASC, Position_ID ASC
)

0
 

Author Closing Comment

by:lapucca
ID: 35193277
Thank you.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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