sql主键与自增 primary/auto_increment配合使用

  • 1,223

primary key 此列不重复,能够区分每一行!!!

注意:

一张表,只能一列为 auto_increment,且此列必须加索引(index/key);

最后一列sql语句,id是主键   int型 且自增!!!

    mysql> create table t11(
        -> id int primary key,
        -> name char(2)
        -> );
    ERROR 1050 (42S01): Table 't11' already exists
    mysql> create table t12(
        -> id int primary key,
        -> name char(2)
        -> );
    Query OK, 0 rows affected (0.14 sec)
    mysql> select * from t12;
    Empty set (0.00 sec)
    mysql> desc t12;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | NO   | PRI | NULL    |       |
    | name  | char(2) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.01 sec)
    mysql> create table t13(
        -> id int,
        -> name char(2),
        -> primary key(id)
        -> );
    Query OK, 0 rows affected (0.12 sec)
    mysql> desc t13;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | NO   | PRI | 0       |       |
    | name  | char(2) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    mysql> create table t14(
        -> id int auto_increment,
        -> name char(2),
        -> key id(id)
        -> );
    Query OK, 0 rows affected (0.15 sec)
    mysql> select * from t14;
    Empty set (0.01 sec)
    mysql> desc t14;
    +-------+---------+------+-----+---------+----------------+
    | Field | Type    | Null | Key | Default | Extra          |
    +-------+---------+------+-----+---------+----------------+
    | id    | int(11) | NO   | MUL | NULL    | auto_increment |
    | name  | char(2) | YES  |     | NULL    |                |
    +-------+---------+------+-----+---------+----------------+
    2 rows in set (0.01 sec)
    mysql> create table t15(
        -> id int primary key auto_increment
        -> );
    Query OK, 0 rows affected (0.27 sec)
    mysql> desc t15;
    +-------+---------+------+-----+---------+----------------+
    | Field | Type    | Null | Key | Default | Extra          |
    +-------+---------+------+-----+---------+----------------+
    | id    | int(11) | NO   | PRI | NULL    | auto_increment |
    +-------+---------+------+-----+---------+----------------+
    1 row in set (0.02 sec)
    mysql>

 

weinxin
我的微信
这是我的微信扫一扫
开拓者博主
  • 本文由 发表于 2016年7月22日13:27:59
  • 转载请务必保留本文链接:https://www.150643.com/215.html
匿名

发表评论

匿名网友 填写信息