数据库的约束,mysql数据库约束详解_基于MySQL数据库的数据约束实例及五种完整性约束介绍

 2023-09-25 阅读 21 评论 0

摘要:为了防止不符合规范的数据进入数据库,在用户对数据进行插入、修改、删除等操作时,DBMS自动按照一定的约束条件对数据进行监测,使不符合规范的数据不能进入数据库,以确保数据库中存储的数据正确、有效、相容。#数据约束#五种完整性约束:#NO

为了防止不符合规范的数据进入数据库,在用户对数据进行插入、修改、删除等操作时,DBMS自动按照一定的约束条件对数据进行监测,使不符合规范的数据不能进入数据库,以确保数据库中存储的数据正确、有效、相容。

#数据约束

#五种完整性约束:

#NOT NULL :非空约束,指定某列不能为空;

#UNIQUE : 唯一约束,指定某列或者几列组合不能重复

#PRIMARY KEY :主键,指定该列的值可以唯一地标识该列记录

#FOREIGN KEY :外键,指定该行记录从属于主表中的一条记录,主要用于参照完整性

#CHECK :检查,指定一个布尔表达式,用于指定对应的值必须满足该表达式(mysql不支持check约束)

#--------------------------------NOT NULL 非空约束 ---------------------------

create table test4

(

#建立非空约束

id int not null,

name varchar(55) default 'ABCD' not null,

#默认值就是null

age int null

);

#取消非空约束

alter table test4

modify name varchar(55) default 'ABCD' not null,

#增加非空约束

alter table test4

modify age int not null;

#--------------------------------UNIQUE : 唯一约束--------------------------------

#列级约束语法建立约束

create table test_unique

(

#建立行级唯一约束

id int not null unique,

age int

);

#表级约束语法格式

create table unique_test3

(

test6_id int not null,

test6_name varchar(255),

test6_pass varchar(255),

#使用表级约束语法建立唯一约束,指定test6_id和test6_name两列组合不能重复

constraint test6_unique unique(test6_id,test6_name),

#使用表级约束语法建立唯一约束,约束名为test6_unique_2,test6_pass不能重复

constraint test6_unique_2 unique(test6_pass)

);

#add关键字增加唯一约束

alter table test4

add unique(id,name,age);

#modify关键字删除或者增加唯一约束

alter table test4

modify age varchar(255) not null;

alter table test4

modify age varchar(255) not null unique;

#对大部分数据库而言,删除约束使用: alter table 表名 drop constraint 约束名

#但是Mysql不采取此方式,而是: alter table 表名 drop index 约束名

#--------------------------------PRIMARY KEY : 主键约束--------------------------------

#主键约束相当于非空约束和唯一约束。

#每个表只允许拥有一个主键,但是这个主键可以由多个数据列组成,这些列组合不能重复

#标准SQL允许给主键自行命名,但是对于Mysql来说自己的名字没有任何作用,总是默认名为PRIMARY

create table primary_test

(

#使用列级语法建立主键约束

test_id int primary key,

test_name varchar(255)

);

#使用表级语法建立主键约束

create table primary_test2

(

test_id int not null,

test_name varchar(255),

test_pass varchar(255),

#指定主键约束名为test2_pk,对大部分数据库有效,但是对mysql无效,此主键约束名仍为PRIMARY

constraint test2_pk primary key (test_id)

);

#以多列组合创立主键

create table primary_test3

(

test_id int,

test_name varchar(255),

primary key(test_id,test_name)

);

#使用列级约束语法

alter table primary_test3

modify test_id int primary key();

#使用表级约束语法

alter table primary_test3

add primary key(test_id,test_name);

#删除主键约束:alter table 表名 drop primary key;

#主键列自增长特性:如果某个数据列的类型是整型,而且该列作为主键列,则可指定该列具有自增长功能

#mysql使用auto_increment来设置自增长,向该表插入记录时可不为该列指定值,由系统生成

create table primary_test3

(

//建立主键约束、设置自增长

test_id int auto_increment primary key,

test_name varchar(255)

);

#外键约束 FOREIGN KEY

#Mysql中只有表级语法建立的外键约束才可以生效

#为保证参照主表的存在,先建立主表

create table teacher_tb

(

t_id int auto_increment,

t_name varchar(255),

primary key(t_id)

);

create table student_tb

(

s_id int auto_increment primary key,

s_name varchar(255) not null,

t_java int,

foreign key(t_java) references teacher_tb(t_id)

);

#如果使用表级约束语法,则需要使用foreign key指定本表的外键列,如果创建外键约束时没有指定约束名,

#则mysql会为该外键约束命名为table_name_ibfk_n,其中table_name是从表的表名,n是从1开始的整数

create table teacher_tb2

(

t_id int auto_increment,

t_name varchar(255),

primary key(t_id)

);

create table student_tb2

(

s_id int auto_increment primary key,

s_name varchar(255) not null,

t_java int,

constraint student_teacher_fk foreign key(t_java) references teacher_tb2(t_id)

);

#建立多列组合外键约束

create table teacher_tb5

(

t_name varchar(255),

t_pass varchar(255),

primary key(t_name,t_pass)

);

create table student_tb5

(

s_id int auto_increment primary key,

s_name varchar(255) not null,

t_java_pass varchar(255),

t_java_name varchar(255),

foreign key(t_java_name,t_java_pass)

references teacher_tb5(t_name,t_pass)

);

#删除外键约束

alter table student_tb2

drop foreign key student_teacher_fk;

#增加外键约束

alter table student_tb2

add foreign key(t_java) references teacher_tb2(t_id);

#外键约束参照自身,自约束

create table foreign_test9

(

foreign_id int auto_increment primary key,

foreign_name varchar(255),

refer_id int,

foreign key(refer_id) references foreign_test9(foreign_id)

);

#定义当删除主表记录时,从表记录也随之删除

#on delete cascade 把参照该主表记录的从表记录全部级联删除

#on delete set null 把参照该主表记录的从表记录从表设为null e

create table teacher_tb8

(

t_id int auto_increment,

t_name varchar(255),

primary key(t_id)

);

create table student_tb8

(

s_id int auto_increment primary key,

s_name varchar(255) not null,

t_java int,

constraint student_teacher_fk foreign key(t_java) references teacher_tb8(t_id) on delete cascade

);

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

本文标题: 基于MySQL数据库的数据约束实例及五种完整性约束介绍

本文地址: http://www.cppcns.com/shujuku/mysql/248957.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/95221.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息