简体   繁体   中英

ENGINE=InnoDB' at line 10 in SQL

I'm trying to create a table with this sql:

CREATE TABLE angestellte (
 PersonalNr          int(11) NOT NULL AUTO_INCREMENT,
 Vorname             varchar(50) NOT NULL,
 Nachname            varchar(50) NOT NULL,
 Beruf               varchar(50) NOT NULL,
 Gehalt              int(11) NOT NULL,
 arbeitetInAbteilung int(11) NOT NULL,
 PRIMARY KEY (PersonalNr),
 FOREIGN KEY abteilung (AbteilungID)
)
ENGINE=InnoDB;

But I get only the message

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')
ENGINE=InnoDB' at line 10

I looked really can't find my mistake but think it's probably something obvious I just don't see.

Supposing you have a table where a field ( arbeitetInAbteilung ) references a row in another table (say arbeiten ), you need to define it like this:

CREATE TABLE `angestellte` (
    `PersonalNR` INT(11) NOT NULL AUTO_INCREMENT,
    ...other fields...
    `arbeitetInAbteilung` INT(11) NULL DEFAULT NULL,
    PRIMARY KEY (`PersonalNR`),
    INDEX `FK__arbeiten` (`arbeitetInAbteilung`),
    CONSTRAINT `FK__arbeiten` FOREIGN KEY (`arbeitetInAbteilung`) 
        REFERENCES `arbeiten` (`arbeitID`) 
        ON UPDATE CASCADE
        ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

This means that an index will be created to index the field you specify (arbeitetInAbteilung). Then a constraint will be set in place so that this index is linked to the values of another field in a different table, which could be defined like this:

CREATE TABLE `arbeiten` (
    `arbeitID` INT(11) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`arbeitID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

Note that the two fields must be absolutely identical ; if they are text fields, they need to have the same collation; they must be both either NULL or NOT NULL; et cetera. The slightest difference will yield a "Foreign key constraint is incorrectly formed" error, and you'll need to show the engine status for InnoDB and parse its output to understand why.

The ON UPDATE and ON DELETE define what happens when you change a value in the master (arbeiten) table, or you delete it. If the ID 123 becomes 456, CASCADE means that all the rows that referred 123 will now refer 456. Another possibility would be to prevent the operation (RESTRICT), or set the mismatched rows to NULL (SET NULL).

As already commented, your FK syntax is total wrong.

FOREIGN KEY abteilung (AbteilungID)

should be

FOREIGN KEY some_column REFERENCES abteilung (AbteilungID)

Here, some_column should be replaced by the column which you want to designate as the referencing key and the definition of this column should exactly match with column AbteilungID of table abteilung

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