基于MySQL数据库的数据约束实例

为了防止不符合规范的数据进入数据库,在用户对数据进行插入、修改、删除等操作时,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,#默认值就是nullage 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无效,此主键约束名仍为PRIMARYconstraint 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 把参照该主表记录的从表记录从表设为nulle 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数据库的数据约束实例

相关文章:

你感兴趣的文章:

标签云: