mysql create table mul,SQL键,MUL与PRI对比UNI

继续讨论MySQL中的MUL,PRI和UNI是什么?

如果Key是PRI,则该列是PRIMARY KEY或者是多列PRIMARY KEY中的列之一 . 如果Key是UNI,则该列是UNIQUE索引的第一列 . (UNIQUE索引允许多个NULL值,但您可以通过检查Null字段来判断该列是否允许NULL . )如果Key为MUL,则该列是非唯一索引的第一列,其中允许多次出现给定值在列内 .

实例

Control group, this example has neither PRI, MUL, nor UNI:

mysql> create table penguins (foo INT);

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | YES | | NULL | |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

A table with one column and an index on the one column has a MUL:

mysql> create table penguins (foo INT, index(foo));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | YES | MUL | NULL | |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

A table with a column that is a primary key has PRI

mysql> create table penguins (foo INT primary key);

Query OK, 0 rows affected (0.02 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | NO | PRI | NULL | |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

A table with a column that is a unique key has UNI:

mysql> create table penguins (foo INT unique);

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | YES | UNI | NULL | |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

A table with an index covering foo and bar has MUL only on foo:

mysql> create table penguins (foo INT, bar INT, index(foo, bar));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | YES | MUL | NULL | |

| bar | int(11) | YES | | NULL | |

+-------+---------+------+-----+---------+-------+

2 rows in set (0.00 sec)

A table with two separate indexes on two columns has MUL for each one

mysql> create table penguins (foo INT, bar int, index(foo), index(bar));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | YES | MUL | NULL | |

| bar | int(11) | YES | MUL | NULL | |

+-------+---------+------+-----+---------+-------+

2 rows in set (0.00 sec)

A table with an Index spanning three columns has MUL on the first:

mysql> create table penguins (foo INT,

bar INT,

baz INT,

INDEX name (foo, bar, baz));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo | int(11) | YES | MUL | NULL | |

| bar | int(11) | YES | | NULL | |

| baz | int(11) | YES | | NULL | |

+-------+---------+------+-----+---------+-------+

3 rows in set (0.00 sec)

A table with a foreign key that references another table's primary key is MUL

mysql> create table penguins(id int primary key);

Query OK, 0 rows affected (0.01 sec)

mysql> create table skipper(id int, foreign key(id) references penguins(id));

Query OK, 0 rows affected (0.01 sec)

mysql> desc skipper;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| id | int(11) | YES | MUL | NULL | |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| id | int(11) | NO | PRI | NULL | |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

坚持在你的新皮层,并将表盘设置为“冰沙” .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值