Solved

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

Posted on 2011-03-22
4
428 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server: SNAPSHOT replication to include a newly added table. 2 28
SQL Server Update Query Streamline 4 28
Running Total in Access 4 47
Help Required 2 29
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

16 Experts available now in Live!

Get 1:1 Help Now