八个约束条件

只有唯一约束才有约束名

1.非空约束:NOT NULL

2.主键约束:PRIMARY KEY

3.多字段联合主键(复合主键)

4.唯一约束:UNIQUE

5.默认约束:DEFAULT

6.外键约束:FOREIGN KEY

7.自增约束: auto_increment

8.检查约束: auto_increment

1.非空约束NOT NULL

  • 非空约束___创建表时添加约束
create table 表名
(
a int not null,
b int not null,
);
  • 非空约束___修改表时添加约束
alter table 表名
modify 字段名 类型 not null;
  • 非空约束___删除约束
alter table 表名
modify 字段名 类型;

2.主键约束PRIMARY KEY

  • 主键约束___创建表时添加约束.
create table 表名
(
a int,
b int,
c int,
primary key(字段1,字段2)
);
create table 表名
(
a int,
b int,
c int,
constraint 约束名 primary key(字段1,...)
);
create table 表名
(
a int primary key,
b int,
c int
);
  • 主键约束___修改表时添加约束
alter tbale 表名
modify 字段名 类型 primary key;
alter table 表名
primary key(字段1,字段2);
alter table 表名
add constraint 约束名 primary key(字段1,字段2);
  • 主键约束___删除约束
alter table 表名
drop primary key;

3.多字段联合主键(复合主键)

  • 多字段联合约束___创建多字段联合主键
create table 表名
(
a int,
b int,
primary key(字段1,字段2)
);

4.唯一约束UNIQUE

  • 唯一约束___创建表时添加约束
create table 表名
(
a int unqiue,
b int,
c int
);
create table 表名
(
a int,
b int,
c int,
constraint usfz unique(字段名,...)
);
create table 表名
(
a int,
b int,
c int,
unique(字段名,...)
);
  • 唯一约束___修改表时添加约束
alter tbale 表名
modify 字段名 类型 unique;
alter table 表名
add unique(字段1,字段2);
alter table 表名
add constraint 约束名 unique(字段1,字段2);
  • 唯一约束___删除约束
alter table 表名
drop index 约束名;
alter table 表名
drop key 约束名称;

5.默认约束DEFAULT

  • 默认约束___创建表时添加约束
create table 表名
(
a int primary key,
b int default '123456'
);
  • 默认约束___修改表时添加约束
alter table 表名
modify 字段名 类型 default '1234';
alter table 表名
alter column 字段名 set default '1234';
  • 默认约束___删除约束
alter table 表名
modify 字段名 类型;
alter table 表名
alter column 字段 drop default;

6.外键约束FOREIGN KEY

有外键的表称之为从表或者子表,相关联的表称之为主表或者父表

  • 外键约束___创建表时添加约束
create table 从表名
(
a int,
b int,
constraint 外键约束名 foreign key (从表字段名)
references 主表名 (主表主键)
);
  • 外键约束___修改表时添加约束
alter table 从表名
add foreign key(从表字段名)
references 主表名(主表字段);
  • 外键约束___删除约束
alter table 从表名
drop foreign key 从表字段名;
  • 从父表删除或更新且自动删除或更新子表中匹配的行
create able 从表名
(
a int,
b int,
consteraint fk_cid
foreign key(从表字段名)
references 主表表名(主表字段名) on delete cascade
);

7.自增约束 auto_increment

https://www.cnblogs.com/iforever/p/10071733.html

  • 自增列___创建表时添加约束

如果想一个自增列,则该字段必须为主键

create tbale 表名
(
a int primary key auto_increment,
b int
);
create table 表名
(
a int primary key auto_increment,
b int
)auto_increment=9;
  • 自增列___修改表时添加约束
alter table 表名
modify 字段名 字段类型 auto_increment;
alter table 表名
auto_increment=9;
  • 自增列___删除约束
alter table 表名
modify 字段名 字段类型;

就算是把自增列给删除了也会给原自增列一个值,这个值为0

8.检查约束

经过上网查看发现,MySQL只是可以使用check约束,但不会强制的遵循check约束!

官方推荐使用枚举类型(ENUM)来替代以上的使用check约束的情况原文

  • 检查约束___创建表时添加约束
create table 表名
(
a int,
b int check(字段名>0 and 字段名<100)
);
create table 表名
(
a int,
check(字段名>100)
);
  • 检查约束___修改表时添加约束
alter table 表名
add constraint 约束名 check(字段名>100);
  • 检查约束___删除约束
alter table 表名
drop constraint 检查约束名;

MySQL_约束条件的更多相关文章

  1. oracle数据库出现“批处理中出现错误: ORA-00001: 违反唯一约束条件”解决方法

    最近使用oraclede impdp工具全库导入数据库时,在数据库里面使用出现如下情况. SQL state : 违反唯一约束条件 (GDXAORCL.SYS_C0055359) ; nested e ...

  2. oracle违反完整约束条件

    oracle违反完整约束条件 Oracle ORA-02292: 违反完整约束条件 (UNITELE.TA_SUB_REFERENCE3) - 已找到子记录 A表被B表引用,删除A表的时候提示ORA- ...

  3. SQL server 的约束条件【转】

    SQLServer - 约束 一.约束的分类 在SQLServer中,有3种不同类型的约束. 1.实体约束 实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键. 2.域约束 域约束是 ...

  4. 纯代码添加约束条件(Auto Layout)

    Auto Layout 是一种基于约束的.描述性的布局系统.也就是使用约束条件来描述布局,View的Frame会根据这些描述来进行计算. 在iOS6.0以后加入了一个新类: NSLayoutConst ...

  5. 1 - SQL Server 2008 之 使用SQL语句创建具有约束条件的表

    约束条件分为以下几种: 1)非空约束,使用NOT NULL关键字: 2)默认值约束,使用DEFAULT关键字: 3)检查约束,使用CHECK关键字: 4)唯一约束,使用UNIQUE关键字: 5)主键约 ...

  6. 查看Oracle当前用户下的信息(用户,表视图,索引,表空间,同义词,存储过程函数,约束条件)

    0.表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user ...

  7. 一些不熟悉的SQL脚本--约束条件

    1.根据表名查询主键的SQL语句 SELECT D.COLUMN_NAME AS COLNAME FROM USER_CONS_COLUMNS D, USER_CONSTRAINTS M WHERE ...

  8. MySQL中的完整性约束条件(主键、外键、唯一、非空)

    数据库的完整性约束用来防止对数据的意外破坏,来保证数据的安全性和一致性. 主键 1.创建表时候指定主键 创建表user(id, username, age),并且id字段非空自增. CREATE TA ...

  9. ORACLE之莫名---ORA-02290: 违反检查约束条件

    最近碰到一个十分棘手的问题,Java程序插入空数据到oracle时报ORA-02290: 违反检查约束条件(XXXX.×××××),这明显是在设置不可为空的字段上插入为空内容导致,但是检查数据库表后发 ...

随机推荐

  1. 转:JSON Assertion 适用于json格式的响应断言

    当响应结果是json格式时,用JSON Assertion更方便判断. 1 在请求上右键添加json断言 2  编辑json Assertion 判断方式: 如果响应结果不是json格式的,fail ...

  2. tcp_wraper&xinetd 和telnet

    一.xinetd简介 1.什么是xinetd xinetd:eXtended InterNET Daemon  扩展的互联网守护程序   xinetd是新一代的网络守护进程服务程序,又叫超级守护进程, ...

  3. mysql 1045 - Access denied for user 'root'@'*.*.*.*' (using password YES)

    远程无法连接mysql 解决方法: 1.在服务器登录数据 mysql -uroot -hlocalhost -P3306 -p Enter password: Welcome to the MySQL ...

  4. golang 文件导入数据追加sheet

    func ReadXlsx(c []CmdbTest, SheetName string) error {     //打开文件,如果文件不存在创建,存在就打开     path := ". ...

  5. Java通过过滤器修改header

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  6. Nginx-HTTP之ngx_http_top_header_filter

    1. ngx_http_top_header_filter 该链表主要是用于构造响应消息的消息报头. ngx_http_top_header_filter 单链表有如下模块插入了操作: ngx_htt ...

  7. Arcengine获得arcgis安装的版本

    ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Version  //gsioracle MessageBox.Show(ESRI.ArcGIS.RuntimeMan ...

  8. mysql表的模糊查询

    查询库下所有的表名 SELECT table_name FROM information_schema.tables WHERE table_schema='库名' 模糊表名查询 SELECT tab ...

  9. spark-submit 提交任务及参数说明

    spark-submit 可以提交任务到 spark 集群执行,也可以提交到 hadoop 的 yarn 集群执行. 1. 例子 一个最简单的例子,部署 spark standalone 模式后,提交 ...

  10. CentOS7 源码安装 PostgreSQL 12

    PostgreSQL 12 源码安装 Table of Contents 1. 下载 2. 准备环境 3. 编译安装 4. 设置环境变量 5. 初始化数据库 6. 配置参数文件 6.1. postgr ...