Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey everyone, im taking an SQL class and im working on my first assignment. It seems simple but i keep getting an Error 1062, duplicate entry '1' for key 1. I have no idea what im doing wrong here.
Here is what i have.
-- Birthdays.sql
--
-- Edited by Kenneth Waters for Computer Science 455.
--
-- This file illustrates SQL statements that create and
-- populate a simple MySQL database containing childrens'
-- names and birth dates.SET @@storage_engine = 'InnoDB';
DROP DATABASE IF EXISTS Birthdays;
CREATE DATABASE Birthdays;
USE Birthdays;CREATE TABLE Child (
Id int,
Fname varchar(16),
Lname varchar(16),
Birth date,
PRIMARY KEY (Id)
);
INSERT INTO Child VALUES
(1, 'John', 'Smith', '1995-05-21'),
(2, 'Mary', 'Davis', '2000-08-05'),
(3, 'Bill', 'Gomez', '1993-02-15'),
(4, 'Sara', 'Smith', '1997-10-04');
CREATE TABLE Interests (
Id int REFERENCES Child(Id),
Interest varchar(20),
PRIMARY KEY (Id)
);
INSERT INTO Interests VALUES
(1, 'Baseball'),
(1, 'Piano'),
(2, 'Dogs'),
(3, 'Football'),
(3, 'Science Fiction'),
(4, 'Baseball'),
(4, 'Soccer'),
(4, 'Dinosaurs');
Any suggestions would be much appreciated. Thank you!

Your problem, as I see it, is here:
CREATE TABLE Interests (
Id int REFERENCES Child(Id),
Interest varchar(20),
PRIMARY KEY (Id)
);

Hrm, you are right. removing that line fixes the problem. Its strange though because my professors examples show a similar setup and it loads just fine.
I guess in this case i simply dont need it eh?

A PRIMARY key has to be unique, meaning that there cannot be duplicates.
It doesn't make sense that the Interests table would have a unique key, which implies that each child would have only one interest.
Rather than removing PRIMARY KEY, you want to make it a normal key field, which will allow duplicates. I don't know SQL SERVER, so can't give the syntax.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |