简体   繁体   中英

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. I was able to successfully get foreign keys working that did not require NOT NULL but I can't get both working.

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. In the second line, the id is identified. 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. 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).

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:

Option 1 (minimally changing the SQL from the question):

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

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.

Key Point

You must define the columns before you define the foreign keys based on those columns.

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