2016-06-11 152 views
0

我試圖做簡單的數據庫,因爲昨天與AUTO_INCREMENT選項主要ID爲關鍵,但不斷收到此錯誤:MySQL的AUTO_INCREMENT列創建

Executing: 
CREATE TABLE `spring`.`samochod` (
`idsamochod` INT NOT NULL DEFAULT AUTO_INCREMENT, 
PRIMARY KEY (`idsamochod`)); 

Operation failed: There was an error while applying the SQL script to the database. 
ERROR 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 'AUTO_INCREMENT, 
PRIMARY KEY (`idsamochod`))' at line 2 
SQL Statement: 
CREATE TABLE `spring`.`samochod` (
    `idsamochod` INT NOT NULL DEFAULT AUTO_INCREMENT, 
    PRIMARY KEY (`idsamochod`)) 

ERROR 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 'AUTO_INCREMENT, 
PRIMARY KEY (`idsamochod`))' at line 2 

SQL語句:

CREATE TABLE `spring`.`samochod` (
    `idsamochod` INT NOT NULL DEFAULT AUTO_INCREMENT, 
    PRIMARY KEY (`idsamochod`)) 

我得到甚至對於正常的整數列也是如此。我已經閱讀過很多文章,據我所知,它應該以這種方式工作,沒有任何問題。

有人能告訴我我做錯了什麼嗎?

+0

你爲什麼標籤mysql的**和**的SQL服務器上,當你的問題是關於MySQL的? –

+0

你爲什麼認爲這可能是有效的語法 – Strawberry

回答

3

問題不在於AUTO_INCREMENT,而是在它之前的DEFAULT。沒有爲DEFAULT指定值。只需刪除這裏不需要的DEFAULT關鍵字。

又見CREATE TABLE syntax MySQL的文檔中,具體如下:

column_definition: 
    data_type [NOT NULL | NULL] [DEFAULT default_value] 
     [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] 
     [COMMENT 'string'] 
     [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}] 
     [STORAGE {DISK|MEMORY|DEFAULT}] 
     [reference_definition]