SQL:使用主键和外键引用创建表( [英] SQL: creating tables with primary keys and foreign key refering (

查看:136
本文介绍了SQL:使用主键和外键引用创建表(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,因为这对我来说完全是业余时间.

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

这是我的小组被分配在SQL开发中创建的数据库架构.

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

我在引用外键时遇到困难

I'm having difficulties referring to FOREIGN KEYs

等 我正在创建表Author,一切都很好.我不确定aName是否应该是唯一标识符或主键.

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

但是,当我尝试制作BOOK表时,由于我在SQL方面的能力不足而遇到麻烦(我真的在尝试).

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.

我无法理解的是我如何将DISCIPLINES表中的主键书引用到BOOK表的year列中.

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.

BOOK表中的Publisher和PUBLISHER表中的URL列之间的箭头方向相同.当它们都不是主键时,如何相互引用?

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.

推荐答案

对于内联外键,不能使用foreign key关键字.最后还有一个悬空的,:

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

或者:

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

这篇关于SQL:使用主键和外键引用创建表(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆