If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > No unique or primary key error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-19-11, 23:53
Shynash21 Shynash21 is offline
Registered User
 
Join Date: Nov 2011
Posts: 2
No unique or primary key error

Please help me in this problem i've been trying but unable to solve

Code:
create table student(regno varchar(10) primary key,name varchar(15),major varchar(6),bdate date);
Code:
create table course(courseno int primary key,cname varchar(10),dept varchar(7));
Code:
create table enroll(regno varchar(10),courseno int,sem int,primary key(regno,courseno,sem),mar
ks int,foreign key (regno) references student(regno),foreign key (courseno) references course(course
no));
Code:
create table text(book_isbn int,book_title varchar(10),publisher varchar(10),author varchar(10
));
Code:
create table book_adoption(courseno int,sem int,book_isbn int,primary key(courseno,sem,book_is
bn),foreign key (courseno) references course(courseno),foreign key (sem) references enroll(sem),foreign key (book_isbn) references book_adopt
ion(book_isbn));
When i try to create the book_adoption table i get the error.
Reply With Quote
  #2 (permalink)  
Old 11-20-11, 01:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
create table book_adoption
(courseno int
,sem int
,book_isbn int
,primary key(courseno,sem,book_isbn)
,foreign key (courseno) references course(courseno)
,foreign key (sem) references enroll(sem)
,foreign key (book_isbn) references book_adoption(book_isbn));
the part marked in red is invalid because enroll(sem) is not a primary or unique key

similarly, the part marked in blue is invalid because book_adoption(book_isbn) is not a primary or unique key
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-20-11, 01:48
Shynash21 Shynash21 is offline
Registered User
 
Join Date: Nov 2011
Posts: 2
Quote:
the part marked in red is invalid because enroll(sem) is not a primary or unique key

similarly, the part marked in blue is invalid because book_adoption(book_isbn) is not a primary or unique key
I have actually done that.If you see the enroll table i have mentioned sem as primary key.
Reply With Quote
  #4 (permalink)  
Old 11-20-11, 01:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Shynash21 View Post
If you see the enroll table i have mentioned sem as a primary key
you have mentioned sem as one of the columns of the primary key

there is only one primary key, and it is a composite key consisting of three columns

a foreign key reference must match the entire key -- either a primary key or a unique key -- and not just part of it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On