MySQL 数据的完整性

一、实体的完整性

        1.主键约束 primary key 

                特点:非空且唯一

                添加方式有三种: 
               

               第一种:

                        create table student01(

                        sid int primary key,

                        name varchar(10)

                        );

               第二种:

                        create table student02(

                        sid int,

                        name varchar(10),

                        # 好处是可以同时设置多个

                        primary key(sia,name)

                        ); 

               第三种:

                        create table student03(

                        sid int,

                        name varchar(10)

                        );

                        alter table student03 add primary key(sia);  

        2.唯一约束unique

                特点:唯一

                添加方式有三种,与上同法。常用方式如下:              

CREATE TABLE `student05`(
classid INT ,
stuid INT,
`name` VARCHAR(10) UNIQUE
);

              

        3.自动增长 auto_increment

               特点:给主键添加自动增长的数值,列只能是整数类型

                常用的添加方式:

                

create table student06(
sid int primary key aotu_increment,
name varchar(10)
);

二、域完整性

        域代表当前单元格,即限制当前单元格数据的格式

        1.数据类型

                   数值类型、日期类型、字符串类型  

   

        2.非空约束 not null

                    

create table student07(
sid int not null,
name varchar(10)
);

                

        3.默认值约束 defalut

                

create table student08(
sid int defalut 0001,
name varchar(10)
);

三、引用完整性

        1.外键约束 foreign key

                在score表中添加外键,来自student表中的sid

create table student(
sid int primary key,
name varchar(10) not null
);
create table score(
cid int primary key,--
sid int,--外键列的数据类型一定要与主键的类型一致
degree float,
constraint fk_score_sid foreign key (sid) references student(sid)
);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 林先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值