5.5 数据库约束

 数据库约束

目的

  为保证数据的完整性和一致性,内置了如下的可选约束属性数据库

PRIMARY KEY (PK)

  标识该字段为该表的主键,能够惟一的标识记录session

  不可为空 spa

  单表只存在一个主键,一般用 id 自增做为主键 code

FOREIGN KEY (FK)

   标识该字段为该表的外键blog

create table emp_info(
id int primary key auto_increment,
name varchar(20),
dep_id int,
constraint FK_depid_id foreign key(dep_id) references dep(id) #references :关联
on delete cascade    #关联的表删了,被关联的表也删了
on update cascade    #关联的表修改了,被关联的表也修改了
);

NOT NULL

  标识该字段不能为空,rem

  默认容许为空 io

UNIQUE KEY (UK)

   标识该字段的值是惟一table

   可设置多字段联合惟一class

constraint host_port unique(字段1,字段2)

AUTO_INCREMENT

   标识该字段的值自动增加(整数类型,并且为主键date

   可设置起始值 ( auto_increment 以及偏移量 (auto_increment_increment 

================设置自增的时候以10开头
create table dep1(
id int primary key auto_increment,
name char(10)
)auto_increment = 10;
insert into dep1(name) values('IT'),('HR'),('EFO');
select * from dep1;
===============auto_increment_increment:自增步长
create table dep3(
id int primary key auto_increment,
 name char(10)
);
会话:经过客户端连到服务端(一次连接称为一次会话)
set session auto_increment_increment = 2; #会话级,只对当前会话有效
set global auto_increment_increment=2; #全局,对全部的会话都有效
insert into dep3(name) values('IT'),('HR'),('SALE'),('Boss');

DEFAULT

   为该字段设置默认值

   默认值是NULL

UNSIGNED

  无符号

  即必须为正值

ZEROFILL

  使用0填充

相关文章
相关标签/搜索