Peter Peter - 1 month ago 8
SQL Question

SQL: creating tables with primary keys and foreign key refering (

Sorry in advance because this is totally amatuer hour on my part.

This is the database schema my group is assigned to create in SQL dev.

Database schema

I'm having difficulties refering FOREIGN KEYs

Etc.
I'm creating table Author and it's all good. I'm a little unsure if maybe aName should be an unique identifier or a primary key.

CREATE TABLE AUTHOR
(
aName varchar (60),
book varchar (60) PRIMARY KEY
);


However when I try to make the BOOK table I run into trouble because of my incompetence in SQL (I'm really trying tho).

CREATE TABLE BOOK
(
ISBN INTEGER PRIMARY KEY,
year integer CHECK (yearBETWEEN 1900 AND 2016),
title varchar (60) FOREIGN KEY REFERENCES FORFATTER (BOK),
publisher utgiver varchar (90),
);


When I try to execute the script I get an error "missing right parenthesis" on the line where I'm trying to reference the foreign key. My guessing is that is a syntax error, but I can't figure it out.

Something else I can't get my head around is the how i reference the primary key book from the table DISCIPLINES into the year column in the BOOK table.

Same goes for how arrow between Publisher in the BOOK table and the URL column in the PUBLISHER table. How can I refer them to one another when neither of them are primary keys?

It's possible that I'm reading the schema wrong or some parts which might have lead to some of my confusion. If you'd be able to help me or give some kind of direction in any way it's much appreciated.

EDIT: fixed some tags

Answer

For an inline foreign key, you can't use the foreign key keyword. You also have a dangling , at the end:

CREATE TABLE BOOK 
(
  ISBN INTEGER PRIMARY KEY,
  year integer CHECK (year BETWEEN 1900 AND 2016),
  title varchar (60) REFERENCES FORFATTER (BOK),
  publisher utgiver varchar (90) --<<< remove the comma here
);

Alternatively:

CREATE TABLE BOOK 
(
  ISBN INTEGER PRIMARY KEY,
  year integer CHECK (year BETWEEN 1900 AND 2016),
  title varchar (60),
  publisher utgiver varchar (90), --<< for this syntax you need the comma
  foreign key (title) REFERENCES FORFATTER (BOK) 
);