Solved

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

Posted on 2011-03-22
4
427 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
  • 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 500 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

15 Experts available now in Live!

Get 1:1 Help Now