NULL value in multi-column primary key

前端 未结 5 1377
执笔经年
执笔经年 2020-12-29 17:54

I\'ve got a table with several columns making up the primary key. The nature of the data stored allows some of these fields to have NULL values. I have designed

5条回答
  •  清酒与你
    2020-12-29 18:28

    You can use unique key like this:

    mysql> CREATE TABLE `test` (
        ->     `Field1` SMALLINT(5) UNSIGNED NOT NULL,
        ->     `Field2` DECIMAL(5,2) UNSIGNED NULL DEFAULT NULL,
        ->     UNIQUE KEY (`Field1`, `Field2`)
        -> )
        -> COLLATE='latin1_swedish_ci'
        -> ENGINE=InnoDB;
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> 
    mysql> desc test
        -> ;
    +--------+-----------------------+------+-----+---------+-------+
    | Field  | Type                  | Null | Key | Default | Extra |
    +--------+-----------------------+------+-----+---------+-------+
    | Field1 | smallint(5) unsigned  | NO   | MUL | NULL    |       |
    | Field2 | decimal(5,2) unsigned | YES  |     | NULL    |       |
    +--------+-----------------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)
    

提交回复
热议问题