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 3428308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:56:36+00:00 2026-05-18T06:56:36+00:00

This is the problematic query (with the intended meaning: pull all entities paired with

  • 0

This is the problematic query (with the intended meaning: pull all entities paired with entity 530 into a new table, with the count of the pairs):

CREATE TEMPORARY TABLE paired (
  entity_id INTEGER PRIMARY KEY,
  numrels INTEGER
)
SELECT I.entity2_id, COUNT(I.relation_id) AS numrels
FROM pairs I
WHERE I.entity1_id = 530 AND I.entity2_id IS NOT NULL
GROUP BY I.entity2_id
;

I interpret the error message:

ERROR 1062 (23000): Duplicate entry '0' for key 1

as the complaint that I am violating primary key’s uniqueness. However, I am grouping by that value, which should ensure the uniqueness, right? Then I thought to try this:

CREATE TEMPORARY TABLE paired (
  entity_id INTEGER PRIMARY KEY,
  numrels INTEGER
)
;
INSERT INTO paired
SELECT I.entity2_id, COUNT(I.relation_id) AS numrels
FROM pairs I
WHERE I.entity1_id = 530 AND I.entity2_id IS NOT NULL
GROUP BY I.entity2_id
;

Surprisingly enough, this works without any problems, even though, according to my understanding, the two should be equivalent.

What gives?!?

For reference:

mysql  Ver 14.12 Distrib 5.0.82sp1, for redhat-linux-gnu (x86_64) using readline 5.1
  • 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-05-18T06:56:37+00:00Added an answer on May 18, 2026 at 6:56 am

    Your statements are not equivalent. CREATE ... SELECT creates the columns you mention in the CREATE part of the statement (that is, entity_id and numrels), and in addition creates columns for each column of the SELECT part of the statement. You end up with four columns in your new table. The results of SELECT are inserted into the last two columns. The other columns are filled with their default values, which results in violating the uniqueness of your primary key.

    See also http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This error just started popping up all over our site. Permission denied to call
This is a self-explanatory question: Why does this thing bubble into my try catch's
Client compiled with OCI: 10.2.0.4.0 Server: Oracle9i Enterprise Edition Release 9.2.0.4.0 The problematic query
Morning y'all! Basically, I'm using a table to store my main data - called
I have found this example on StackOverflow: var people = new List<Person> { new
I have the following query: ;WITH valRules AS ( SELECT vr.valRuleID, Count(*) AS totalRows,
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small

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.