简体   繁体   English

ENGINE = InnoDB'在SQL的第10行

[英]ENGINE=InnoDB' at line 10 in SQL

I'm trying to create a table with this sql: 我正在尝试使用此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: 假设您有一个表,其中一个字段( arbeitetInAbteilung )引用另一个表中的行(例如arbeiten ),则需要这样定义它:

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). 这意味着将创建一个索引以索引您指定的字段(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; 它们必须为NULL或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. 最小的差异将产生“外键约束格式错误”错误,并且您需要显示InnoDB的引擎状态并解析其输出以了解原因。

The ON UPDATE and ON DELETE define what happens when you change a value in the master (arbeiten) table, or you delete it. ON UPDATE和ON DELETE定义在更改主表(arbeiten)中的值或删除它时会发生什么。 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). 如果ID 123变为456,则CASCADE表示引用123的所有行现在都将引用456。另一种可能性是阻止该操作(RESTRICT),或将不匹配的行设置为NULL(SET NULL)。

As already commented, your FK syntax is total wrong. 如前所述,您的FK语法完全错误。

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 在这里, some_column应该要指定为引用的键和此列的定义应该正好与列匹配的列被替换AbteilungIDabteilung

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

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