mysql给表增加约束条件

常见的约束 非空约束,not null 唯一约束,unique 主键约束,primary key 外键约束,foreign key

自定义检查约束,check(不建议使用)(在mysql中现在还不支持)

非空约束,not null

非空约束,针对某个字段设置其值不为空,如:学生的姓名不能为空

drop table if exists t_student;

create table t_student(

student_id int(10),

student_name varchar(20) not null,

sexchar(2) default m,

birthdaydate,

emailvarchar(30),

classes_idint(3)

)

insert into t_student(student_id, birthday, email, classes_id)

values

(1002, 1988-01-01, qqq@163.com, 10)

以上错误为加入的学生姓名为空。

唯一约束,unique

唯一性约束,它可以使某个字段的值不能重复,如:email不能重复:

drop table if exists t_student;

create table t_student(

student_id int(10),

student_name varchar(20) not null,

sexchar(2) default m,

birthdaydate,

emailvarchar(30) unique,

classes_idint(3)

)

insert into t_student(student_id, student_name , sex, birthday, email, classes_id)

values

(1001,zhangsan,m, 1988-01-01, qqq@163.com, 10)

以上插入了重复的email,所以出现了“违反唯一约束错误”,所以unique起作用了同样可以为唯一约束起个约束名

我们可以查看一下约束

mysql> use information_schema;

mysql> select * from table_constraints where table_name = t_student;

关于约束名称可以到table_constraints中查询

以上约束的名称我们也可以自定义。

drop table if exists t_student;

create table t_student(

student_id int(10),

student_name varchar(20) not null,

sexchar(2) default m,

birthdaydate,

emailvarchar(30) ,

classes_idint(3),

constraint email_unique unique(email)/*表级约束*/

)

主键约束,primary key

每个表应该具有主键,主键可以标识记录的唯一性,主键分为单一主键和复合(联合)主键,单一主键是由一个字段构成的,复合(联合)主键是由多个字段构成的

drop table if exists t_student;

create table t_student()

student_id int(10) primary key,/*列级约束*/

student_name varchar(20) not null,

sexchar(2) default m,

birthdaydate,

emailvarchar(30) ,

classes_idint(3)

)

insert into t_student(student_id, student_name , sex, birthday, email, classes_id)

values

(1001,zhangsan,m, 1988-01-01, qqq@163.com, 10)

向以上表中加入学号为1001的两条记录,出现如下错误,因为加入了主键约束

我们也可以通过表级约束为约束起个名称:

drop table if exists t_student;

create table t_student(

student_id int(10),

student_name varchar(20) not null,

sexchar(2) default m,

birthdaydate,

emailvarchar(30) ,

classes_idint(3),

CONSTRAINT p_id PRIMARY key (student_id)

)

insert into t_student(student_id, student_name , sex, birthday, email, classes_id)

values

(1001,zhangsan,m, 1988-01-01, qqq@163.com, 10)

外键约束,foreign key

外键主要是维护表之间的关系的,主要是为了保证参照完整性,如果表中的某个字段为外键字段,那么该字段的值必须来源于参照的表的主键,如:emp中的deptno值必须来源于dept表中的deptno字段值。

建立学生和班级表之间的连接

首先建立班级表t_classes

drop table if exists t_classes;

create table t_classes(

classes_id int(3),

classes_name varchar(40),

constraint pk_classes_id primary key(classes_id)

)

在t_student中加入外键约束

drop table if exists t_student;

create table t_student(

student_id int(10),

student_name varchar(20),

sexchar(2),

birthdaydate,

emailvarchar(30),

classes_idint(3),

constraint student_id_pk primary key(student_id),

constraintfk_classes_id foreign key(classes_id) references t_classes(classes_id)

)

向t_student中加入数据

insert into t_student(student_id, student_name, sex, birthday, email, classes_id) values(1001, zhangsan, m, 1988-01-01, qqq@163.com, 10)

出现错误,因为在班级表中不存在班级编号为10班级,外键约束起到了作用

存在外键的表就是子表,参照的表就是父表,所以存在一个父子关系,也就是主从关系,主表就是班级表,从表就是学生表

以上成功的插入了学生信息,当时classes_id没有值,这样会影响参照完整性,所以我们建议将外键字段设置为非空

drop table if exists t_student;

create table t_student(

student_id int(10),

student_name varchar(20),

sexchar(2),

birthdaydate,

emailvarchar(30),

classes_idint (3) not null,

constraint student_id_pk primary key(student_id),

constraintfk_classes_id foreign key(classes_id) references t_classes(classes_id)

java里面怎么添加表约束_mysql给表增加约束条件相关推荐

  1. java里面怎么添加表约束_alter table添加表约束

    翻阅了一下网上关于alter table添加表约束的资料,学习下,然后供自己以后使用. 仅仅供自己使用... 总结alter table ### add constraint ## 使用方法 添加表约 ...

  2. mysql 表中添加数据类型_MySQL数据表添加字段(三种方式)

    MySQL 数据表是由行和列构成的,通常把表的"列"称为字段(Field),把表的"行"称为记录(Record).随着业务的变化,可能需要在已有的表中添加新的字 ...

  3. mysql 为数据表添加字段_MySQL数据表添加字段实例

    MySQL 允许在开头.中间和结尾处添加字段.针对不同的位置,MySQL数据表添加字段的方式也有所不同,下面我们一起来看MySQL数据表添加字段的实例,方便我们理解MySQL数据表添加字段的方式. M ...

  4. mysql建表后添加约束_MYSQL建表完成之后添加约束

    1.主键约束 添加:alter table table_name add primary key (字段) 删除:alter table table_name drop primary key 2.非 ...

  5. mysql增加字段设默认值_mysql原表增加字段且设置默认值及修改字段默认值

    -- 增加字段及注释 alter table sr_zjff_main add zjbzjxbj int(1) DEFAULT '0' COMMENT ''; alter table sr_main_ ...

  6. mysql添加列名在第一列_mysql在表的某一位置增加一列、删除一列、修改列名

    如果想在一个已经建好的表中添加一列,可以用以下代码: alter table 表名 add column 列名 varchar(20) not null; 这条语句会向已有的表中加入一列,这一列在表的 ...

  7. 获取mysql可行方法_Mysql学习Java实现获得MySQL数据库中所有表的记录总数可行方法...

    <Mysql学习Java实现获得MySQL数据库中所有表的记录总数可行方法>要点: 本文介绍了Mysql学习Java实现获得MySQL数据库中所有表的记录总数可行方法,希望对您有用.如果有 ...

  8. mysql 添加表索引语句_mysql 为表添加索引

    索引作用 在索引列上,除了上面提到的有序查找之外,数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍. 例如,有3个 ...

  9. 【无标题】mysql增加字段和备注_mysql 修改表名,修改字段类型,增加字段,删除字段,添加字段备注,添加索引...

    mysql语句: 1.修改表名: rename table 旧表名 to 新表名; 2.修改字段类型: alter table 表名 modify column 字段名 字段类型(长度) 3.修改字段 ...

最新文章

  1. 《OpenCV3编程入门》学习笔记9 直方图与匹配(一二) 图像直方图概述直方图的计算与绘制
  2. java jvm学习笔记二(类装载器的体系结构)
  3. python精通-11周精通python计划(完结)-网易云课堂
  4. 审批流程展示html,Web企业在线审批流程系统
  5. 对象的反序列化流_ObjectInputStream
  6. 15个著名的设计心理学原理以及在设计中的应用
  7. lie group and computer vision : 李群、李代数在计算机视觉中的应用
  8. 生产过程代码分析(二)--估计
  9. grid studio python_Grid studio表格应用程序:Python的集大成者
  10. ((ios开发学习笔记 十二))Nib加载的方式实现自定义TableView
  11. VS2005 + VSS2005 的方法
  12. 贪心算法详细讲解(附例题,一看就会)
  13. 整合springmvc+mybatis+veloctiy二
  14. java 接口 实验报告_java-接口练习实验报告
  15. java实现windows下amr转换为mp3(可实现微信语音和qq语音转换)
  16. 推特(twitter)翻译
  17. 音乐生毕业论文有什么好的选题?
  18. 「Leetcode592」分数加减法
  19. AI大模型知识点大梳理
  20. Django-SMTP-QQ邮箱SMTPAuthenticationError: (535

热门文章

  1. 【NIO】IO多路复用
  2. CATransition
  3. swift iOS8 XIB 问题 ViewController.init() xib
  4. 计算机c语言循环作业,C语言计算机作业编程.doc
  5. 定时分量和直流分量_直流电机效率测试的计算与纹波因数及波形因数的计算
  6. dataframe 按条件删行_根据列值删除Pandas中的DataFrame行
  7. iptables相关管理命令
  8. 怎么把项目推到gitlab上_将本地项目添加到 GitLab 上管理
  9. rt-thread端口时钟使能_(2)RTThread启动过程分析
  10. 03系统多界面_领克03+冠军版定制版车联系统全网首测,性能+智能它都给你