Mysql给表添加约束

前言

这个其实很常见吧,经常我们会使用主键作为唯一约束,如果是手机用户,或者是邮箱用户进行登录,那么这个登录的字段并不是主键。在高并发,注册的时候,如果不设置唯一约束,则可能会导入两个相同的数据。为了防止这种情况发生,我们要注意添加约束。

1、创建约束和问题引出

1.1、创建联合约束,我们发现,这里设置为唯一约束,建立唯一约束和唯一索引又什么区别?建立唯一约束的时候,也会自动的创建唯一索引。建立唯一索引可以说是唯一约束的一种手段。

DROP  TABLE  user_info ;
create table user_info(
  id BIGINT(20) not null auto_increment,
  fuWuBusinessNoId  BIGINT(20) default null,
  dingYueBusinessNoId  BIGINT(20) default null,
  openId varchar(20) DEFAULT  NULL  UNIQUE  ,
  iphone varchar(20) default null COMMENT '',
  status int(11) default null ,
  cdate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  udate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY unique_fuWuBusinessNoId_iphone (fuWuBusinessNoId,iphone) COMMENT '服务号和手机号唯一标识一个用户,可用于手机号登录判断',
  PRIMARY key (id));


添加普通索引
ALTER TABLE  user_info add name VARCHAR(20) DEFAULT  NULL  ;
CREATE INDEX  index_name  on user_info(name)  ;

添加唯一索引
ALTER TABLE  user_info add mail VARCHAR(20) DEFAULT  NULL  ;
CREATE UNIQUE INDEX  index_mail  on user_info(mail)  ;





1.2、查看索引 ,

发现其实unique直接建立之后就变成了索引

show INDEX  from  user_info ;

mysql> show INDEX  from  user_info ;
+-----------+------------+--------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+--------------------------------------------------------------------------------+
| Table     | Non_unique | Key_name                       | Seq_in_index | Column_name      | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment                                                                  |
+-----------+------------+--------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+--------------------------------------------------------------------------------+
| user_info |          0 | PRIMARY                        |            1 | id               | A         |           0 |     NULL | NULL   |      | BTREE      |         |                                                                                |
| user_info |          0 | openId                         |            1 | openId           | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |                                                                                |
| user_info |          0 | unique_fuWuBusinessNoId_iphone |            1 | fuWuBusinessNoId | A         |           0 |     NULL | NULL   | YES  | BTREE      |         | 服务号和手机号唯一标识一个用户,可用于手机号登录判断                           |
| user_info |          0 | unique_fuWuBusinessNoId_iphone |            2 | iphone           | A         |           0 |     NULL | NULL   | YES  | BTREE      |         | 服务号和手机号唯一标识一个用户,可用于手机号登录判断                           |
| user_info |          0 | index_mail                     |            1 | mail             | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |                                                                                |
| user_info |          1 | index_name                     |            1 | name             | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |                                                                                |
+-----------+------------+--------------------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+--------------------------------------------------------------------------------+
6 rows in set (0.00 sec)


WX20180925-152627

2、创建和删除约束


 
 alter table test_table add unique key `kk_uq` ( kk); 
 
 
 删除唯一约束(和删除索引一个意思)
 
 ALTER TABLE jw_role DROP INDEX resource_name;

 

3、springboog注解


@Table(name = "user_info",uniqueConstraints =
        {@UniqueConstraint(columnNames = "openId"),
        @UniqueConstraint(columnNames = {"fuId","iphone"})})
@Entity
@Accessors(chain = true)
@Data
@NoArgsConstructor
@ApiModel(description = "微信用户信息")
public class UserInfo {


ContactAuthor

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值