问题描述
我在我的数据库中创建了一个表并添加了一些约束.当我在我的表上使用 DESC
关键字时,根据我放置的约束,Key 列会更改其记录.
这是表定义:
创建表 t(sif INT,sif2 INT 非空,sif3 INT唯一,sif4 INT NOT NULL UNIQUE);
这里是 DESC
结果:
为什么我的列 sif4 得到了键值 PRI?我从未提及主键定义,但 DESC
关键字显示该列已设置为主键.
DESC t
或 DESCRIBE t
等价于 SHOW COLUMNS FROM t
.在 SHOW COLUMNS
命令的输出中,如果唯一索引不能包含空值并且表中没有主键,则它可能会显示为 PRI
.
在您的情况下,sif4
列符合条件,因为它声明为
sif4 INT NOT NULL UNIQUE
来自文档:
UNIQUE 索引如果不能包含 NULL 可能会显示为 PRI值并且表中没有 PRIMARY KEY.UNIQUE 索引可能如果多个列形成一个复合 UNIQUE 索引,则显示为 MUL;虽然列的组合是唯一的,但每一列都可以仍然保持给定值的多次出现.
I have created a table in my database and added a few constraints. When i use the DESC
keyword on my table, depending on what constraints I placed, the Key column changes its record.
Here's the table definition:
CREATE TABLE t(
sif INT,
sif2 INT NOT NULL,
sif3 INT UNIQUE,
sif4 INT NOT NULL UNIQUE
);
and Here's the DESC
result:
mysql> desc t; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | sif | int(11) | YES | | NULL | | | sif2 | int(11) | NO | | NULL | | | sif3 | int(11) | YES | UNI | NULL | | | sif4 | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
Why did my column sif4 get the key value PRI? I never mentioned a primary key definition and yet the DESC
keyword shows that the column is set up as a primary key.
DESC t
or DESCRIBE t
is equivalent to SHOW COLUMNS FROM t
. In the output from the SHOW COLUMNS
command, a unique index may be displayed as PRI
if it cannot contain null values and there's no primary key in the table.
In your case, the sif4
column qualifies, as it's declared as
sif4 INT NOT NULL UNIQUE
From the documentation:
这篇关于DESC 命令和其中的键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!