MySQL创建另一个PRIMARY KEY

时间:2015-04-15 16:03:31

标签: php mysql

我尝试为文章材料创建一个最简单的表,但MySQL为我创建了另一个我不需要的主键。

$command = "CREATE TABLE IF NOT EXISTS `$this->table` (
            `id` int unsigned UNIQUE NOT NULL AUTO_INCREMENT PRIMARY KEY,
            `title` varchar(200),
            `body` text,
            `author` varchar(100),
            `last_moderator` varchar(100),
            `category` int(10),
            `cdate` datetime,
            `lmdate` datetime,
            `commentable` boolean,
            `active` boolean ) CHARACTER SET utf8 COLLATE utf8_bin
                ";

这是查询。正如您所看到的,我需要id作为主键,但此查询会产生两个主键:idbody。人们请帮助我 - 我冥想这个查询20分钟,但我没有看到任何错误。

1 个答案:

答案 0 :(得分:0)

呃,不,它没有...... body没有被定义为主键或其他键:

mysql> CREATE TABLE IF NOT EXISTS `myTable` (
    ->             `id` int unsigned UNIQUE NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->             `title` varchar(200),
    ->             `body` text,
    ->             `author` varchar(100),
    ->             `last_moderator` varchar(100),
    ->             `category` int(10),
    ->             `cdate` datetime,
    ->             `lmdate` datetime,
    ->             `commentable` boolean,
    ->             `active` boolean ) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 0 rows affected (0.02 sec)

mysql> show indexes from myTable;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| myTable |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| myTable |          0 | id       |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)