Solved

How to set a primary key field when I select from several joins into a new table?

Posted on 2008-10-22
9
285 Views
Last Modified: 2010-03-20
Hi, I have the following selects.   How can I set the Id field to be a Primary key but not auto-increment?  Thank you.

SELECT a.Id
      ,a.MembershipLevel
      ,a.ProfessionalOrganizations
      ,a.Department
      ,a.Gender
      ,a.Ethnicity
      ,a.Race
      ,a.YearOfBirth
      ,a.Title
      ,a.EmailAddress
      ,a.LastName
      ,a.Firstname
      ,a.PhoneNumber
      ,a.Institution
      ,a.MailingAddress
      ,a.OfficeAddress
      ,a.TitleOther
      ,a.PhdTraineeCount
      ,a.MscTraineeCount
      ,a.InstitutionOther
      ,a.MembershipReasonOther
      ,a.RaceOther
      ,a.MailingAddressLine1
      ,a.MailingAddressLine2
      ,a.MailingAddressCity
      ,a.MailingAddressState
      ,a.MailingAddressZip
      ,a.CommunityOrganization
      ,C.[Degree 1]
      ,D.[Membership Reason 1]
      ,E.[Membership Service Requirement 1]
INTO Application
      FROM dbo.TempApplication2 a
            FULL JOIN dbo.Degree C on a.Id = C.ApplicationId
            FULL JOIN dbo.Reasons D on a.Id = D.ApplicationId
            FULL JOIN dbo.Requirement E on a.Id = E.ApplicationId
ORDER BY a.Id
0
Comment
Question by:lapucca
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22781142
Try this:
ALTER TABLE Application ADD PRIMARY KEY(Id);

Open in new window

0
 
LVL 22

Expert Comment

by:dportas
ID: 22781150
SQL doesn't allow explicit keys in SELECT statements or views. You can create a key on your table like this:

ALTER TABLE dbo.TempApplication2
  ADD CONSTRAINT TempApplication2_PK
  PRIMARY KEY (ID);
0
 

Expert Comment

by:Tejal06
ID: 22781180
Actually your ID is in TempApplication2 table
therefore the query should be
ALTER TABLE TempApplication2 ADD PRIMARY KEY (ID); 

Open in new window

0
 

Author Comment

by:lapucca
ID: 22781195
Hi Cxr,
Got the following error when I run it.  Please advise how to correct this.  Thank you.
Msg 8111, Level 16, State 1, Line 2
Cannot define PRIMARY KEY constraint on nullable column in table 'Application'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Expert Comment

by:Tejal06
ID: 22781230
Your table Application has records where ID is NULL. You need to delete these records.

DELETE Application where ID IS NULL
0
 

Author Comment

by:lapucca
ID: 22781263
I delete the null rows but still getting the same error message.  I think after deleting the Null rows I would need to set Id column not to accept null.  How to code this constraint?  thank you.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 22781285
A primary key column must be defined as "not null". Which table do you want this primary key on? And what datatype is the Id column. If the table is "Application" and the type is INT, use something like this to make the column "not null":

ALTER TABLE Application ALTER COLUMN Id INT NOT NULL;

Open in new window

0
 

Author Comment

by:lapucca
ID: 22781351
Hi Cxr, Yes, that did it. Thank you.
0
 
LVL 22

Expert Comment

by:dportas
ID: 22781374
Note that Cxr's ALTER TABLE statement adds a primary key with a default system-assigned name. This is not good practice in my opinion - in fact people who support SQL Server systems regularly have cause to curse people who do this! Always give your constraints explicit names. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
another query question 7 37
Insert from Stored Procedure where some field/s > 0 7 43
sql query help 2 46
Updating a table from a temp table 4 30
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

864 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

23 Experts available now in Live!

Get 1:1 Help Now