Computing.Net > Forums > Database > Beginner SQL Student, Question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Beginner SQL Student, Question

Reply to Message Icon

Name: Atlasprime
Date: October 1, 2007 at 19:44:57 Pacific
OS: Vista
CPU/Ram: 2.4
Product: Custom
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 1, 2007 at 20:02:32 Pacific
Reply:

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


0

Response Number 2
Name: Atlasprime
Date: October 1, 2007 at 20:18:04 Pacific
Reply:

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?


0

Response Number 3
Name: Don Arnett
Date: November 6, 2007 at 11:48:09 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Database Forum Home


Sponsored links

Ads by Google


Results for: Beginner SQL Student, Question

very simple SQL design question www.computing.net/answers/dbase/very-simple-sql-design-question/351.html

SQL 2005 UNION question www.computing.net/answers/dbase/sql-2005-union-question/300.html

oracle 9i sql question www.computing.net/answers/dbase/oracle-9i-sql-question/649.html