mysql 常用约束

来源:互联网 发布:移动adhoc网络的特点 编辑:程序博客网 时间:2024/06/06 19:17

## mysql 常用约束

目录

  1. 主键约束
  2. 默认约束
  3. 非空约束
  4. 外键约束

内容

  1. 主键约束:

     字段后加 primary key 或者使用 primary key (字段) 来设定
  2. 默认约束:

     字段后加 default 'string' 或 default number (也可默认设置时间戳 default current_timestamp())
  3. 非空约束:

     字段后加 not null 
  4. 外键约束:

    外键约束需要满足三个条件 : 引擎 innodb , 外键字段必须为索引类型外键字段与关联字段类型必须一致
    constraint 外键名 foreign key (外键字段) preferences 表名(关联字段)
    constraint 外键名 foreign key (外键字段) preferences 表名(关联字段)+ on delete cascade
    constraint 外键名 foreign key (外键字段) preferences 表名(关联字段)+ on delete set null
    注:delete 处也可使用 update 实现更新时的效果

原创粉丝点击