mysql 创建唯一约束表

给你一囗甜甜゛ 提交于 2020-11-26 02:54:30

<div class="htmledit_views" id="content_views"> <p>说明:</p>

<p>&nbsp; &nbsp; UNIQUE 约束唯一标识数据库表中的每条记录。</p>

<p>&nbsp; &nbsp; UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。</p>

<p>&nbsp; &nbsp; PRIMARY KEY 拥有自动定义的 UNIQUE 约束。</p>

<p>&nbsp;请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。</p>

<p><br> 1、创建一个测试表:</p>

<p>create table test(<br> &nbsp; &nbsp; id int unsigned auto_increment,<br> &nbsp; &nbsp; name varchar(50) not null,<br> &nbsp; &nbsp; age int unsigned,<br> &nbsp; &nbsp; unique(name),<br> &nbsp; &nbsp; primary key(id)<br> )engine=innodb charset=utf8</p>

<p>2、插入数据测试</p>

<p>&nbsp; &nbsp; insert into test(name,age) values('张三',21)<br> 查询<br> &nbsp; &nbsp;&nbsp;<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>

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