Multi-Column Primary Key in MySQL 5

后端 未结 3 495
清歌不尽
清歌不尽 2020-12-03 10:28

I\'m trying to learn how to use keys and to break the habit of necessarily having SERIAL type IDs for all rows in all my tables. At the same time, I\'m also do

3条回答
  •  爱一瞬间的悲伤
    2020-12-03 10:37

    Quoted from the CREATE TABLE Syntax page:

    A PRIMARY KEY can be a multiple-column index. However, you cannot create a multiple-column index using the PRIMARY KEY key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate PRIMARY KEY(index_col_name, ...) clause.

    Something like this can be used for multi-column primary keys:

    CREATE TABLE
        product (
            category INT NOT NULL,
            id INT NOT NULL,
            price DECIMAL,
            PRIMARY KEY(category, id)
        );
    

    From 13.1.20.6 FOREIGN KEY Constraints

提交回复
热议问题