Auto Increment in Temporary Table

徘徊边缘 提交于 2019-12-08 12:30:53

问题


I'm trying to create a Temp Table that has a key field that auto increments. But I keep running into a syntax error.

Here's what I'm trying:

CREATE TEMPORARY TABLE
    RETURN_ARTISTS
        (KEY INT(11) NOT NULL AUTO_INCREMENT,
        ARTIST_1_KEY INT(11),
        ARTIST_2_KEY INT(11));

INSERT INTO RETURN_ARTISTS (1,1);

DELETE TABLE RETURN_ARTISTS;

And here's the error I keep getting:

Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT(11) NOT NULL AUTO_INCREMENT,
        ARTIST_1_KEY INT(11),
        ARTIST_2_KEY INT(' at line 3

回答1:


First of all, key is a reserved word, escape it with `

Secondly, when using auto_increment column, that column must be defined as a key.

CREATE TEMPORARY TABLE
`RETURN_ARTISTS`
    (`KEY` INT(11) NOT NULL AUTO_INCREMENT,
    ARTIST_1_KEY INT(11),
    ARTIST_2_KEY INT(11),
    KEY (`KEY`));



回答2:


key is a reserved word, so you'll have to escape it:

    (`KEY` INT(11) NOT NULL AUTO_INCREMENT,
     ^---^--- escapes

or, preferably, use a different field name.



来源:https://stackoverflow.com/questions/10184375/auto-increment-in-temporary-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!