MySQL: Name primary key in CREATE TABLE statement

前端 未结 4 2033
醉梦人生
醉梦人生 2020-12-20 17:40

How do I set the name of a primary key when creating a table?

For example here I\'m trying to create a primary key with the name \'id\', but this is invalid SQL. Can

4条回答
  •  难免孤独
    2020-12-20 18:20

    You don't have to specify the column name again, because you already specified it as part of the current field definition - just say PRIMARY KEY.

    CREATE TABLE IF NOT EXISTS `default_test` (
        `id` SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        `name` LONGTEXT NOT NULL
    )
    

    Alternatively, you can specify it separately later:

    CREATE TABLE IF NOT EXISTS `default_test` (
        `id` SMALLINT NOT NULL AUTO_INCREMENT,
        `name` LONGTEXT NOT NULL,
        PRIMARY KEY(`id`)
    )
    

提交回复
热议问题