Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:19:13+00:00 2026-06-04T20:19:13+00:00

If you have a table with two primary keys: CREATE TABLE [AttributeMap]( [intParentAttributeId] [bigint]

  • 0

If you have a table with two primary keys:

CREATE TABLE [AttributeMap](
[intParentAttributeId] [bigint] NOT NULL,
[intChildAttributeId] [bigint] NOT NULL,
    [datCreatedDate] [datetime] NOT NULL
  CONSTRAINT [PK_AttributeMap] PRIMARY KEY CLUSTERED 
 ( 
   [intParentAttributeId] ASC,
    [intChildAttributeId] ASC
 )

If you want to do an insert into/select statement to add data to the table, how can you restrict the data to make sure it doesn’t violate both keys?

So if you insert this into the table above:

INSERT INTO [AttributeMap] VALUES (1, 1, getdate())
INSERT INTO [AttributeMap] VALUES (1, 2, getdate())
INSERT INTO [AttributeMap] VALUES (1, 3, getdate())
INSERT INTO [AttributeMap] VALUES (2, 1, getdate())

How could you run this query without violating the keys?

declare table @temp (intParent int, intChild int)
insert into @temp (1, 1)
insert into @temp (1, 2)
insert into @temp (4, 4)
insert into @temp (5, 5)

 insert into AttributeMap (intParentAttributeId, intChildAttributeId, datCreatedDate)
  select intParent, intChild, getDate()
 from @temp

So AttributeMap should end up with two new rows, values 4, 4, “date” and 5, 5 “date”. Make sense?

Cheers,
Matt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:19:14+00:00Added an answer on June 4, 2026 at 8:19 pm

    EXCEPT

    Returns any distinct values from the query to the left of the EXCEPT operand that are not also returned from the right query.

    Try this:

    insert into AttributeMap (intParentAttributeId, intChildAttributeId, datCreatedDate)
    Select temp.intParent, temp.intChild, getDate()
    FROM 
    (select intParent, intChild
    from @temp 
    EXCEPT 
    select intParentAttributeId, intChildAttributeId
    from AttributeMap) as temp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two SQL statements: CREATE TABLE legs(legid INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
I have two tables: CREATE TABLE InmarsatZenith.dbo.ClientJob (JobRef int PRIMARY KEY NOT NULL, IntRef
i have two tables: CREATE TABLE public.auctions ( id VARCHAR(255) NOT NULL, auction_value_key VARCHAR(255)
I have two tables: CREATE TABLE dbo.country ( cntry_id VARCHAR(2) NOT NULL, name VARCHAR(50)
I have two tables: CREATE TABLE IF NOT EXISTS treaties( id INT NOT NULL
I have a table with two primary keys,how can i map the same in
In A Table I want to have two columns with primary keys(that avoid insert
I have two tables (Table1 and Table2) with the same primary keys, lets say
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key
i have a table where termid,termversion are two primary key. column present in table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.