<div class="htmledit_views" id="content_views"> <p>说明:</p>
<p> UNIQUE 约束唯一标识数据库表中的每条记录。</p>
<p> UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。</p>
<p> PRIMARY KEY 拥有自动定义的 UNIQUE 约束。</p>
<p> 请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。</p>
<p><br> 1、创建一个测试表:</p>
<p>create table test(<br> id int unsigned auto_increment,<br> name varchar(50) not null,<br> age int unsigned,<br> unique(name),<br> primary key(id)<br> )engine=innodb charset=utf8</p>
<p>2、插入数据测试</p>
<p> insert into test(name,age) values('张三',21)<br> 查询<br> <img alt="" class="has" height="93" src="https://img-blog.csdnimg.cn/20190828111417214.png" width="342"></p>
<p>再添加相同name名称的数据会报错添加不上</p>
<p><img alt="" class="has" height="132" src="https://img-blog.csdnimg.cn/20190828111517493.png" width="395"></p> 原文地址:https://blog.csdn.net/weirdo_world/article/details/100115290 </div>
来源:oschina
链接:https://my.oschina.net/u/4261744/blog/3316161