设为首页 加入收藏

TOP

五种完整性约束分别是什么――基于MySQL数据库的数据约束实例(一)
2015-11-21 01:33:27 来源: 作者: 【 】 浏览:0
Tags:完整性 约束 别是 什么 基于 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,

#默认值就是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_teache
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据库-DataBase设计 下一篇postgresql整理

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: