简体   繁体   中英

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

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

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

数据库架构

I'm having difficulties referring to 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 could help me or give some kind of direction in any way it would be much appreciated.

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) 
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM