java里面怎么添加表约束_mysql给表增加约束条件

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)

7488544e83ad0915812d8f75ba8728ab.png

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

唯一约束,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)

0251729a2cf1aaeecb9e9f5db001ed5e.png

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

我们可以查看一下约束

mysql> use information_schema;

mysql> select * from table_constraints where table_name = t_student;

0f91a0cca9e2cfdaaae0362ae1d37b8a.png

关于约束名称可以到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的两条记录,出现如下错误,因为加入了主键约束

871989bde462fe1b2c5d209065fc6f8d.png

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

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)

3514d35ffb3f1d3a1064b88581feea7a.png

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

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

c63bea306715d071121ac7ec747c3699.png

以上成功的插入了学生信息,当时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)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值