mysql中key 、primary key 、unique key 与index区别

断了今生、忘了曾经 提交于 2020-12-16 07:15:28

<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post" style="overflow: hidden;">

<p><span style="font-family:Microsoft YaHei"><span style="font-size:18px; color:#ff0000"><span style="line-height:25.2px"></span></span></span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 索引被用来快速找出在一个列上用一特定值的行。没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行。</p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 表越大,花费时间越多。如果表对于查询的列有一个索引,MySQL能快速到达一个位置去搜寻到数据文件的中间,没有必要考虑所有数据。</p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 如果一个表有1000行,这比顺序读取至少快100倍。注意你需要存取几乎所有1000行,它较快的顺序读取,因为此时我们避免磁盘寻道。&nbsp;<br> <br> 所有的MySQL<span style="color:rgb(255,0,0)">索引</span>(PRIMARY、UNIQUE和INDEX)在<span style="color:rgb(255,0,0)">B树中存储</span>。字符串是自动地压缩前缀和结尾空间。<br> <br> <span style="color:rgb(0,0,153)"><span style="color:rgb(255,0,0)">索引用于:</span>&nbsp;</span><br> <br> 快速找出匹配一个WHERE子句的行;&nbsp;<br> 当执行联结时,从其他表检索行;&nbsp;<br> 对特定的索引列找出MAX()或MIN()值;&nbsp;<br> 如果排序或分组在一个可用键的最左面前缀上进行(例如,ORDER&nbsp;BY&nbsp;key_part_1,key_part_2),排序或分组一个表。</p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 如果所有键值部分跟随DESC,键以倒序被读取。&nbsp;<br> 在一些情况中,一个查询能被优化来检索值,不用咨询数据文件。</p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 如果对某些表的所有使用的列是数字型的并且构成某些键的最左面前缀,为了更快,值可以从索引树被检索出来。&nbsp;</p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> —————————————————————————————————————————————————————————————————————————————</p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 下面是建表的语句:<br> </p><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 706px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;`phpcolor_ad`&nbsp;(&nbsp;&nbsp;</span></span></li><li class=""><span>`id`&nbsp;mediumint(8)&nbsp;<span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>&nbsp;AUTO_INCREMENT,&nbsp;&nbsp;</span></span></li><li class="alt"><span>`<span class="keyword">name</span><span>`&nbsp;</span><span class="keyword">varchar</span><span>(30)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;</span></span></li><li class=""><span>`type`&nbsp;mediumint(1)&nbsp;<span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;</span></span></li><li class="alt"><span>`code`&nbsp;text,&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">PRIMARY</span><span>&nbsp;</span><span class="keyword">KEY</span><span>&nbsp;(`id`),&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">KEY</span><span>&nbsp;`type`&nbsp;(`type`)&nbsp;&nbsp;</span></span></li><li class=""><span>);&nbsp;&nbsp;</span></li></ol></div><pre name="code" class="sql" style="display: none;">CREATE TABLE `phpcolor_ad` ( `id` mediumint(8) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `type` mediumint(1) NOT NULL, `code` text, PRIMARY KEY (`id`), KEY `type` (`type`) );</pre><br> <br> <p></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">最后一句的KEY `type` (`type`)是什么意思?</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><strong><span style="color:rgb(255,0,0)">如果只是key的话,就是普通索引</span></strong>。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mysql的key和index多少有点令人迷惑,<span style="color:rgb(255,0,0)">单独的key和其它关键词结合的key(primary key)实际表示的意义是不同</span>,这实际上考察对<a target="_blank" href="http://lib.csdn.net/base/14" class="replace_word" title="MySQL知识库" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold">数据库</a>体系结构的了解的。<br> <span style="font-size:18px">1 :</span></span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">key 是数据库的物理结构,它包含两层意义和作用,</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">一是约束(偏重于约束和规范数据库的结构完整性),</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">二是索引(辅助查询用的)。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">包括primary key, unique key, foreign key 等。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><span style="color:rgb(255,0,0)"><strong>primary key</strong></span>&nbsp;有两个作用,一是约束作用(constraint),用来规范一个存储主键和唯一性,但同时也在此key上建立了一个主键索引;&nbsp;&nbsp;&nbsp;&nbsp;</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PRIMARY KEY 约束:唯一标识数据库表中的每条记录;</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; 主键必须包含唯一的值;</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 主键列<span style="color:rgb(0,0,153)">不能包含 NULL 值</span>;<br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 每个表都应该有一个主键,并且每个表只能有一个主键。(PRIMARY KEY 拥有自动定义的 UNIQUE 约束)</span></p> <span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:26px"><span style="color:rgb(255,0,0)">unique key</span></span><span style="font-size:14px; line-height:26px">&nbsp;也有两个作用,一是约束作用(constraint),规范数据的唯一性,但同时也在这个key上建立了一个唯一索引;</span><br style="font-size:14px; line-height:26px"> </span> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">UNIQUE 约束:唯一标识数据库表中的每条记录。<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束)<br> </span></p> <span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:26px"><span style="color:rgb(255,0,0)">foreign key</span></span><span style="font-size:14px; line-height:26px">也有两个作用,一是约束作用(constraint),规范数据的引用完整性,但同时也在这个key上建立了一个index;</span></span> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">可见,<span style="color:rgb(255,0,0)">mysql的key是同时具有constraint和index的意义</span>,这点和其他数据库表现的可能有区别。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">(至少在oracle上建立外键,不会自动建立index),因此创建key也有如下几种方式:<br> (1)在<span style="color:rgb(255,0,0)">字段级</span>以key方式建立, 如 create table t (id int not null primary key);<br> (2)在<span style="color:rgb(255,0,0)">表级</span>以<span style="color:rgb(255,0,0)">constraint方式</span>建立,如create table t(id int, CONSTRAINT pk_t_id PRIMARY key (id));<br> (3)在<span style="color:rgb(255,0,0)">表级</span>以<span style="color:rgb(255,0,0)">key方式</span>建立,如create table t(id int, primary key (id));<br> <br> 其它key创建类似,但不管那种方式,既建立了constraint,又建立了index,只不过index使用的就是这个constraint或key。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><br> <span style="font-size:18px">2:</span>&nbsp;index是数据库的物理结构,它只是辅助查询的,<span style="color:rgb(255,0,0)">它创建时会在另外的表空间(mysql中的innodb表空间)以一个类似目录的结构存储</span>。索引要分类的话,分为前缀索引、全文本索引等;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因此,索引只是索引,<strong><span style="color:rgb(255,0,0)">它不会去约束索引的字段的行为</span></strong>(那是key要做的事情)。如,create table t(id int,<span style="color:rgb(0,0,153)"><strong>index inx_tx_id (id)</strong>)</span>;<br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><br> 3 总结,最后的释疑:<br> (1)我们说<strong><span style="color:rgb(255,0,0)">索引分类</span></strong>,分为</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">主键索引(必须指定为“<span style="color:rgb(51,51,255)">PRIMARY KEY</span>”,没有PRIMARY Index)、</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">唯一索引(<span style="color:rgb(51,51,255)">unique index,一般写成unique key</span>)、</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><span style="color:rgb(255,0,0)">普通索引(index,只有这一种才是纯粹的index)</span>等,<span style="color:rgb(255,0,0)"><strong>也是基于是不是把index看作了key</strong></span>。<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 比如 create table t(id int,&nbsp;<strong><span style="color:rgb(0,0,153)">unique index</span></strong>inx_tx_id (id));<span style="color:rgb(0,0,153)"><strong>--index当作了key使用</strong></span><br> <br> (2)最重要的也就是,不管如何描述,需要理解index是纯粹的index(普通的key,或者普通索引index),还是被当作key(如:unique index、unique key和primary key),若当作key时则会有两种意义或起两种作用。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">—————————————————————————————————————————————————————————————————————————————</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><span style="font-size:18px">MySQL Key值(PRI, UNI, MUL)的含义</span>:</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">PRI主键约束;</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">UNI唯一约束;</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">MUL可以重复。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei">注:若是普通的key或者普通的index(实际上,普通的key与普通的index同义)。</span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><span style="background-color:rgb(255,255,0)">当我们在desc 表名; 的时候,有一个Key值,表示该列是否含有索引</span><br> 假设表结构如下所示<br> mysql&gt; desc aa;<br> +-------+---------+------+-----+---------+-------+<br> | Field | Type&nbsp; &nbsp; | Null | Key | Default | Extra |<br> +-------+---------+------+-----+---------+-------+<br> | id&nbsp; &nbsp; | int(11) | YES&nbsp;&nbsp;|&nbsp; &nbsp;&nbsp;&nbsp;| NULL&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp; |<br> +-------+---------+------+-----+---------+-------+<br> | xx&nbsp; &nbsp; | int(11) | YES&nbsp;&nbsp;| PRI | NULL&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp; |<br> +-------+---------+------+-----+---------+-------+<br> | yy&nbsp; &nbsp; | int(11) | YES&nbsp;&nbsp;| UNI | NULL&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp; |<br> +-------+---------+------+-----+---------+-------+<br> | zz&nbsp; &nbsp; | int(11) | YES&nbsp;&nbsp;| MUL | NULL&nbsp; &nbsp; |&nbsp; &nbsp;&nbsp; &nbsp; |<br> +-------+---------+------+-----+---------+-------+<br> 1 row in set (0.00 sec)<br> <br> 我们看到Key那一栏,可能会有4种值,即'啥也没有','PRI','UNI','MUL'<br> 1. 如果Key是空的, 那么该列值的可以重复,表示<span style="color:rgb(255,0,0)">该列没有索引</span>, 或者是一个<span style="color:rgb(255,0,0)"><strong>非唯一</strong>的复合索引的<strong>非</strong>前导列</span><br> 2. 如果Key是PRI,&nbsp;&nbsp;那么该列是主键的组成部分<br> 3. 如果Key是UNI,&nbsp;&nbsp;那么该列是一个<span style="color:rgb(255,0,0)">唯一值索引的第一列(前导列)</span>,且<span style="color:rgb(0,0,153)">不能含有空值(NULL)</span><br> 4. 如果Key是MUL,&nbsp;&nbsp;那么该列的值可以重复, 该列是一个<span style="color:rgb(255,0,0)"><strong>非唯一</strong>索引的前导列(第一列)</span>或者是一个<span style="color:rgb(255,0,0)">唯一性索引的组成部分</span>但是<span style="color:rgb(0,0,153)">可以含有空值NULL</span><br> </span></p> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-size:14px; line-height:26px"> <span style="font-family:Microsoft YaHei"><strong><span style="font-size:18px">注:</span></strong><br> 1、如果对于一个列的定义,同时满足上述4种情况的多种,比如一个列既是PRI,又是UNI(如果是PRI,则一定是UNI)<br> 那么"desc 表名"; 的时候,显示的Key值按照优先级来显示<span style="color:rgb(255,0,0)">&nbsp;PRI-&gt;UNI-&gt;MUL</span><br> 那么此时,显示PRI。<br> <br> <span style="color:rgb(255,0,0)"><span style="color:rgb(0,0,0)">2、</span>如果某列不能含有空值,同时该表没有主键,则一个唯一性索引列可以显示为PRI,</span><br> <br> 3、如果多列构成了一个唯一性复合索引,那么一个唯一性索引列可以显示为MUL。(因为虽然索引的多列组合是唯一的,比如ID+NAME是唯一的,但是每一个单独的列依然可以有重复的值,因为只要ID+NAME是唯一的即可)</span><br> </p> <p><span style="font-family:Microsoft YaHei"><span style="line-height:25.2px"><span style="font-size:14px"></span><br> </span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:18px; color:#ff0000"><span style="line-height:25.2px"><br> </span></span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:18px; color:#ff0000"><span style="line-height:25.2px">一、key与primary key区别</span><span style="line-height:25.2px">&nbsp;</span></span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="color:#ff0000"><span style="font-size:14px; line-height:25.2px"><br> </span></span></span></p> <span style="font-family:Microsoft YaHei"></span><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 3309px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;wh_logrecord&nbsp;(&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>logrecord_id&nbsp;<span class="keyword">int</span><span>(11)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>&nbsp;auto_increment,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>user_name&nbsp;<span class="keyword">varchar</span><span>(100)&nbsp;</span><span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>operation_time&nbsp;datetime&nbsp;<span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>logrecord_operation&nbsp;<span class="keyword">varchar</span><span>(100)&nbsp;</span><span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">PRIMARY</span><span>&nbsp;</span><span class="keyword">KEY</span><span>&nbsp;(logrecord_id),&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">KEY</span><span>&nbsp;wh_logrecord_user_name&nbsp;(user_name)&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>)&nbsp;&nbsp;&nbsp;</span></li></ol></div><pre name="code" class="sql" style="display: none;">CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_name varchar(100) default NULL, operation_time datetime default NULL, logrecord_operation varchar(100) default NULL, PRIMARY KEY (logrecord_id), KEY wh_logrecord_user_name (user_name) ) </pre><br> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">解析:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">KEY wh_logrecord_user_name (user_name)&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">本表的user_name字段与wh_logrecord_user_name表user_name字段建立外键&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">括号外是建立外键的对应表,括号内是对应字段&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">类似还有 KEY user(userid)&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">当然,<span style="background-color:rgb(255,255,0)">key未必都是外键&nbsp;</span></span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">总结:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">Key是索引约束,对表中字段进行约束索引的,都是通过primary foreign unique等创建的。常见有foreign key,外键关联用的。&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">KEY forum (status,type,displayorder)&nbsp; # 是多列索引(键)&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">KEY tid (tid)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # 是单列索引(键)。&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">如建表时: KEY forum (status,type,displayorder)&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">select * from table group by status,type,displayorder 是否就自动用上了此索引,&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">而当 select * from table group by status 此索引有用吗?&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px; color:red">key的用途:主要是用来加快查询速度的。</span><span style="font-size:14px; line-height:25.2px">&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <p><span style="font-family:Microsoft YaHei"></span></p> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 4019px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;`admin_role`&nbsp;(&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`adminSet_id`&nbsp;<span class="keyword">varchar</span><span>(32)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`roleSet_id`&nbsp;<span class="keyword">varchar</span><span>(32)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;<span class="keyword">PRIMARY</span><span>&nbsp;</span><span class="keyword">KEY</span><span>&nbsp;(`adminSet_id`,`roleSet_id`),&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;<span class="keyword">KEY</span><span>&nbsp;`FK9FC63FA6DAED032`&nbsp;(`adminSet_id`),&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;<span class="keyword">KEY</span><span>&nbsp;`FK9FC63FA6C7B24C48`&nbsp;(`roleSet_id`)&nbsp;&nbsp;</span></span></li><li class="alt"><span>)&nbsp;ENGINE=InnoDB&nbsp;<span class="keyword">DEFAULT</span><span>&nbsp;CHARSET=utf8;&nbsp;&nbsp;</span></span></li></ol></div><pre name="code" class="sql" style="display: none;">CREATE TABLE `admin_role` ( `adminSet_id` varchar(32) NOT NULL, `roleSet_id` varchar(32) NOT NULL, PRIMARY KEY (`adminSet_id`,`roleSet_id`), KEY `FK9FC63FA6DAED032` (`adminSet_id`), KEY `FK9FC63FA6C7B24C48` (`roleSet_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; </pre><br> 主键,两个列组合在一起,是唯一的,内建唯一性索引,并且不能为NULL<br> 另外,两个Key定义,相当于分别对这两列建立索引。<br style="font-size:14px; line-height:25.2px"> <p></p> <p><span style="font-family:Microsoft YaHei"><br> </span></p> <p><span style="font-family:Microsoft YaHei">innodb<br> primary key &nbsp; 主键聚集索引<br> key 普通索引<br> </span></p> <p><span style="font-family:Microsoft YaHei"><br> </span></p> <span style="font-family:Microsoft YaHei"><span style="font-size:18px; color:#ff0000"><span style="line-height:25.2px">二、KEY与INDEX区别</span><span style="line-height:25.2px">&nbsp;</span></span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> </span> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">KEY通常是INDEX同义词。如果关键字属性PRIMARY KEY在列定义中已给定,则PRIMARY KEY也可以只指定为KEY。</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">这么做的目的是与其它数据库系统兼容。</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">PRIMARY KEY是一个唯一KEY,此时,所有的关键字列必须定义为NOT NULL。</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">如果这些列没有被明确地定义为NOT NULL,MySQL应隐含地定义这些列。一个表只有一个PRIMARY KEY。&nbsp;</span></span></p> <span style="font-family:Microsoft YaHei"><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="line-height:25.2px"><span style="font-size:18px; color:#ff0000">MySQL 中Index 与Key 的区别&nbsp;</span></span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> </span> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">Key即键值,是关系模型理论中的一部份,比如有主键(Primary Key),外键(Foreign Key)等,用于数据完整性检否与唯一性约束等。</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">而Index则处于实现层面,比如可以对表的任意列建立索引,那么当建立索引的列处于SQL语句中的Where条件中时,就可以得到快速的数据定位,从而快速检索。</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">至于Unique Index,则只是属于Index中的一种而已,建立了Unique Index表示此列数据不可重复,猜想MySQL对Unique Index类型的索引可以做进一步特殊优化吧。&nbsp;</span></span></p> <span style="font-family:Microsoft YaHei"><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">于是乎,在设计表的时候,Key只是要处于模型层面的,<span style="color:#ff0000">而当需要进行查询优化,则对相关列建立索引即可。&nbsp;</span></span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">另外,在MySQL中,对于一个Primary Key的列,MySQL已经自动对其建立了Unique Index,无需重复再在上面建立索引了。&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">搜索到的一段解释:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">&nbsp;&nbsp;&nbsp; Note that “primary” is called PRIMARY KEY not INDEX.&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">&nbsp;&nbsp;&nbsp; <span style="color:#ff0000"> KEY is something on the logical level</span>, describes your table and database design (i.e. enforces referential integrity …)&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">&nbsp;&nbsp;&nbsp;<span style="color:#ff0000"> INDEX is something on the physical level</span>, helps improve access time for table operations.&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">&nbsp;&nbsp;&nbsp; Behind every PK there is (usually) unique index created (automatically).&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:18px"><span style="color:#ff0000"><span style="line-height:25.2px">三、mysql中UNIQUE KEY和PRIMARY KEY有什么区别</span><span style="line-height:25.2px">&nbsp;</span></span></span><br style="font-size:14px; line-height:25.2px"> </span> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">1,Primary key的1个或多个列必须为NOT NULL,如果列为NULL,在增加PRIMARY KEY时,列自动更改为NOT NULL。</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">而UNIQUE KEY 对列没有此要求&nbsp;</span></span></p> <span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">2,一个表只能有一个PRIMARY KEY,但可以有多个UNIQUE KEY&nbsp;</span><br style="font-size:14px; line-height:25.2px"> </span> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">3,主键和唯一键约束是通过参考索引实施的,如果插入的值均为NULL,</span></span></p> <p><span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">则<span style="background-color:rgb(255,255,0)">根据索引的原理,全NULL值不被记录在索引上</span>,所以插入全NULL值时,可以有重复的,而其他的则不能插入重复值。&nbsp;</span></span></p> <span style="font-family:Microsoft YaHei"><span style="font-size:14px; line-height:25.2px">alter table t add constraint uk_t_1 unique (a,b);&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">insert into t (a ,b ) values (null,1);&nbsp;&nbsp;&nbsp; # 不能重复&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">insert into t (a ,b ) values (null,null);#可以重复&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:18px; color:#ff0000"><span style="line-height:25.2px">四、使用UNIQUE KEY</span><span style="line-height:25.2px">&nbsp;</span></span><br style="font-size:14px; line-height:25.2px"> </span><div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 5658px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;`secure_vulnerability_warning`&nbsp;(&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`id`&nbsp;<span class="keyword">int</span><span>(10)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>&nbsp;auto_increment,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`<span class="keyword">date</span><span>`&nbsp;</span><span class="keyword">date</span><span>&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`type`&nbsp;<span class="keyword">varchar</span><span>(100)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`sub_type`&nbsp;<span class="keyword">varchar</span><span>(100)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`domain_name`&nbsp;<span class="keyword">varchar</span><span>(128)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`url`&nbsp;text&nbsp;<span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`parameters`&nbsp;text&nbsp;<span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`hash`&nbsp;<span class="keyword">varchar</span><span>(100)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`deal`&nbsp;<span class="keyword">int</span><span>(1)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`deal_date`&nbsp;<span class="keyword">date</span><span>&nbsp;</span><span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`remark`&nbsp;text,&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;`last_push_time`&nbsp;datetime&nbsp;<span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`push_times`&nbsp;<span class="keyword">int</span><span>(11)&nbsp;</span><span class="keyword">default</span><span>&nbsp;</span><span class="string">'1'</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;`first_set_ok_time`&nbsp;datetime&nbsp;<span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;`last_set_ok_time`&nbsp;datetime&nbsp;<span class="keyword">default</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;<span class="keyword">PRIMARY</span><span>&nbsp;</span><span class="keyword">KEY</span><span>&nbsp;&nbsp;(`id`),&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;<span class="keyword">UNIQUE</span><span>&nbsp;</span><span class="keyword">KEY</span><span>&nbsp;`</span><span class="keyword">date</span><span>`&nbsp;(`</span><span class="keyword">date</span><span>`,`hash`)&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>)&nbsp;ENGINE=InnoDB&nbsp;&nbsp;<span class="keyword">DEFAULT</span><span>&nbsp;CHARSET=utf8&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><pre name="code" class="sql" style="display: none;">CREATE TABLE `secure_vulnerability_warning` ( `id` int(10) NOT NULL auto_increment, `date` date NOT NULL, `type` varchar(100) NOT NULL, `sub_type` varchar(100) NOT NULL, `domain_name` varchar(128) NOT NULL, `url` text NOT NULL, `parameters` text NOT NULL, `hash` varchar(100) NOT NULL, `deal` int(1) NOT NULL, `deal_date` date default NULL, `remark` text, `last_push_time` datetime default NULL, `push_times` int(11) default '1', `first_set_ok_time` datetime default NULL, `last_set_ok_time` datetime default NULL, PRIMARY KEY (`id`), UNIQUE KEY `date` (`date`,`hash`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 </pre><br> <span style="font-size:14px; line-height:25.2px; color:red">UNIQUE KEY的用途:主要是用来防止数据插入的时候重复的。</span><span style="font-size:14px; line-height:25.2px">&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px; background-color:rgb(255,255,0)">1,创建表时&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 6166px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;Persons&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>(&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>Id_P&nbsp;<span class="keyword">int</span><span>&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>LastName&nbsp;<span class="keyword">varchar</span><span>(255)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>FirstName&nbsp;<span class="keyword">varchar</span><span>(255),&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>Address&nbsp;<span class="keyword">varchar</span><span>(255),&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>City&nbsp;<span class="keyword">varchar</span><span>(255),&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">UNIQUE</span><span>&nbsp;(Id_P)&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>)&nbsp;&nbsp;&nbsp;</span></li></ol></div><pre name="code" class="sql" style="display: none;">CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), UNIQUE (Id_P) ) </pre><br> <span style="font-size:14px; line-height:25.2px; background-color:rgb(255,255,0)">如果需要命名 UNIQUE 约束,以及为多个列定义 UNIQUE 约束,请使用下面的 SQL 语法:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 6444px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">CREATE</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;Persons&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>(&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>Id_P&nbsp;<span class="keyword">int</span><span>&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>LastName&nbsp;<span class="keyword">varchar</span><span>(255)&nbsp;</span><span class="op">NOT</span><span>&nbsp;</span><span class="op">NULL</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>FirstName&nbsp;<span class="keyword">varchar</span><span>(255),&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>Address&nbsp;<span class="keyword">varchar</span><span>(255),&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>City&nbsp;<span class="keyword">varchar</span><span>(255),&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">CONSTRAINT</span><span>&nbsp;uc_PersonID&nbsp;</span><span class="keyword">UNIQUE</span><span>&nbsp;(Id_P,LastName)&nbsp;&nbsp;&nbsp;</span></span></li><li class="alt"><span>)&nbsp;&nbsp;&nbsp;</span></li></ol></div><pre name="code" class="sql" style="display: none;">CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName) ) </pre><br> <span style="font-size:14px; line-height:25.2px; background-color:rgb(255,255,0)">2,当表已被创建时,如需在 "Id_P" 列创建 UNIQUE 约束,请使用下列 SQL:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 6747px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">ALTER</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;Persons&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">ADD</span><span>&nbsp;</span><span class="keyword">UNIQUE</span><span>&nbsp;(Id_P)&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><pre name="code" class="sql" style="display: none;">ALTER TABLE Persons ADD UNIQUE (Id_P) </pre><br> <span style="font-size:14px; line-height:25.2px; background-color:rgb(255,255,0)">如需命名 UNIQUE 约束,并定义多个列的 UNIQUE 约束,请使用下面的 SQL 语法:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 6924px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">ALTER</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;Persons&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">ADD</span><span>&nbsp;</span><span class="keyword">CONSTRAINT</span><span>&nbsp;uc_PersonID&nbsp;</span><span class="keyword">UNIQUE</span><span>&nbsp;(Id_P,LastName)&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><pre name="code" class="sql" style="display: none;">ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName) </pre><br> <span style="font-size:14px; line-height:25.2px; background-color:rgb(255,255,0)">3,撤销 UNIQUE 约束&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <span style="font-size:14px; line-height:25.2px">如需撤销 UNIQUE 约束,请使用下面的 SQL:&nbsp;</span><br style="font-size:14px; line-height:25.2px"> <br style="font-size:14px; line-height:25.2px"> <div class="dp-highlighter bg_sql"><div class="bar"><div class="tools"><b>[sql]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 487px; top: 7151px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_9" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_9" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=9&amp;width=16&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-sql"><li class="alt"><span><span class="keyword">ALTER</span><span>&nbsp;</span><span class="keyword">TABLE</span><span>&nbsp;Persons&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">DROP</span><span>&nbsp;</span><span class="keyword">INDEX</span><span>&nbsp;uc_PersonID&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><pre name="code" class="sql" style="display: none;">ALTER TABLE Persons DROP INDEX uc_PersonID </pre><br> <br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.2px"> </div>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!