- 主键约束和外键约束
- 主键约束 (primary key): 约束在当前表中,指定列的值非空且唯一
- 外键约束(foreign key): A 表中的外键列,。 A表中的外键列的值必须参照BIAO中的某一列(B表主键)
- 外键必须是另一张表的主键的值(外键要引用主键!)
- 外键可以重复
- 外键可以为空
- 一张表中可以有多个外键
create table tb_card
(
cid int auto_increment,
cno char(18) not null,
ps varchar(128) not null,
expire date not null,
pid int not null,
primary key(cid)
)
create table tb_person
(
pid int auo_increment,
pname varchar(50),
primarykey(pid)
)
alter table tb_card add constartint fk_card_pid foreign keu(pid)
references tb_person(pid); # 将tb_card表中的pid设置为外键约束,参考tb_person表
alter table tb_card add constraint uni_card_pid unique (pid);
- 数据库分类:
- 关系形数据库(SQL)
- 实体完整性 - 每一个实体都是独一无二的 - 主键、索引、
- 参照完整性 - 外键
- 域完整性 - 存储的数据都是有效数据 - 数据列席、数据长度、非空约束、默认值约束
- 一致性 - 事务 - 要么全成功要么全失败,操作不可分割 - ACID特效
- A - Atomicity - 原子性 - 不可分割
- C - Consistency - 一致性 - 事务钱后数据状态要一致
- I - ISOlation - 隔离性 - 并发的多个事务不知道彼此的中间状态
- D - Duration - 持久性 - 事务完成后数据要做持久化
- 非关系型数据库(NoSQL)
- 文档数据库 - MongoDB \ ElasticSearch
- 键值对数据库 Redis
- 列族数据库
- 图数据库