简体   繁体   English

如何在MySQL中使列不为空并成为外键

[英]How do I make a column not null and be a foreign key in MySQL

I am trying to create a table that is a foreign key of another table and make it not null, but I am running into trouble making both happen. 我试图创建一个表,该表是另一个表的外键,并且使其不为null,但是我遇到了使两者同时发生的麻烦。 I was able to successfully get foreign keys working that did not require NOT NULL but I can't get both working. 我能够成功使不需要NOT NULL的外键正常工作,但是我却无法使两者都正常工作。

Here is the line giving me trouble 这是给我麻烦的线

CONSTRAINT instructor FOREIGN KEY (id) REFERENCES Instructor(id) NOT NULL

then I get the error: 然后我得到错误:

CONSTRAINT instructor FOREIGN KEY (id) REFERENCES Instructor(id) NOT NULL,
                                                                 *
ERROR at line 5:
ORA-00907: missing right parenthesis

Also, I am getting a weird error when trying to create a table (note, this table is created after creating the table that contains the above error) where it fails at a very trivial part: 另外,在尝试创建表时会出现一个奇怪的错误(请注意,此表是在创建包含上述错误的表之后创建的),它在非常琐碎的部分失败了:

CREATE TABLE Enrollment (
       CONSTRAINT class_id FOREIGN KEY (id) REFERENCES Class(id) NOT NULL,
       CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id) NOT NULL,
       cost int NOT NULL
);

Then for that command I get this error: 然后对于该命令,我得到此错误:

CREATE TABLE Enrollment (
                    *
ERROR at line 1:
ORA-00904: : invalid identifier

How can I fix these two errors? 如何解决这两个错误?

You need to create the column before you try creating constraints on the column. 在尝试在列上创建约束之前,需要创建列。

You have: 你有:

CREATE TABLE Enrollment (
       CONSTRAINT class_id  FOREIGN KEY (id) REFERENCES Class(id) NOT NULL,
       CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id) NOT NULL,
       cost int NOT NULL
);

You need: 你需要:

CREATE TABLE Enrollment (
       id INT NOT NULL CONSTRAINT class_id REFERENCES Class(id),
       CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id),
       cost INT NOT NULL
);

Note that in the first line, the FOREIGN KEY isn't necessary because the column is implied. 请注意,在第一行中,因为该列是隐含的,所以不需要FOREIGN KEY。 In the second line, the id is identified. 在第二行中,标识了id You could also write: 您还可以编写:

CREATE TABLE Enrollment
(
       id INT NOT NULL,
       CONSTRAINT class_id  FOREIGN KEY (id) REFERENCES Class(id),
       CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id),
       cost INT NOT NULL
);

It is unusual, though not automatically wrong, to make a single column ( id ) be a foreign key of two tables simultaneously. 使一个单独的列( id )同时成为两个表的外键是很平常的,尽管这不是自动的错误。 It isn't clear if you actually want three columns in your table — and if you do, which column names are in your table. 目前尚不清楚您是否真正想要表中的三列,如果要,那么表中将包含哪些列名。

You could also use the appropriate notation for an automatically allocated type in MySQL syntax ( SERIAL instead of INT NOT NULL , or add AUTO_INCREMENT , etc). 您还可以在MySQL语法中为自动分配的类型使用适当的符号( SERIAL而不是INT NOT NULL ,或者添加AUTO_INCREMENT等)。

Maybe you're really after: 也许您确实在追求:

CREATE TABLE Enrollment
(
       id          SERIAL,
       class_id    INT NOT NULL CONSTRAINT class_id  REFERENCES Class(id),
       member_id   INT NOT NULL CONSTRAINT member_id REFERENCES RecCenterMember(id),
       cost        INT NOT NULL
);

This makes more sense in general. 总的来说,这更有意义。 You're creating a new enrollment record for a pre-existing class, and for a pre-existing recreation centre member, and recording its cost. 您正在为现有班级和现有娱乐中心成员创建新的入学记录,并记录其成本。


Syntax diagrams vs actual behaviour 语法图与实际行为

If, as Michael - sqlbot suggests — and I've no reason whatsoever to disbelieve him — MySQL recognizes but does not respond to the REFERENCES clause in a column definition in a CREATE TABLE statement but only acts on full FOREIGN KEY clauses, then you have to adjust my suggested answers from their syntactically correct but semantically ignored form to something like: 如果按照Michael-sqlbot的 建议 -并且我没有任何理由不相信他-MySQL识别但不响应CREATE TABLE语句中列定义中的REFERENCES子句,但仅对完整的FOREIGN KEY子句起作用,那么您就有了将我建议的答案从其语法正确但在语义上被忽略的形式调整为以下形式:

Option 1 (minimally changing the SQL from the question): 选项1(从问题中最少更改SQL):

CREATE TABLE Enrollment (
       id INT NOT NULL,
       CONSTRAINT class_id  FOREIGN KEY (id) REFERENCES Class(id),
       CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id),
       cost int NOT NULL
);

Option 2 (what I consider the most plausible version): 选项2(我认为最合理的版本):

CREATE TABLE Enrollment
(
       id          SERIAL,
       class_id    INT NOT NULL,
       CONSTRAINT  fk_class_id  FOREIGN KEY (class_id)  REFERENCES Class(id),
       member_id   INT NOT NULL,
       CONSTRAINT  fk_member_id FOREIGN KEY (member_id) REFERENCES RecCenterMember(id),
       cost        INT NOT NULL
);

Or some other variant of this syntax based on the desired table schema ignoring the FK constraints, then adding the constraints along the lines shown. 或基于所需表模式的此语法的其他变体,忽略FK约束,然后沿所示的行添加约束。

Key Point 关键点

You must define the columns before you define the foreign keys based on those columns. 您必须先定义列,然后才能基于这些列定义外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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