可以显示给有default的字段给NULL值,所以有时候需要组合起来!比如某个字段一定不能为NULL!
空约束 非空约束(null not null)
- 两个值:nul(默认的)和not null(不为空)
- 数据库默认字段基本都是字段为空,但是实际开发时,尽可能保证字段不为空,因为数据为空没办法参与运算。
例1 非空约束
创建一个班级表,包含班级名和班级所在的教室。
站在正常的业务逻辑中:
- 如果班级没有名字,你不知道你在哪个班级
- 如果教室名字可以为空,就不知道在哪上课
所以我们在设计数据库表的时候,一定要在表中进行限制,满足上面条件的数据就不能插入到表中。这就是“约束”。
不能为空
可以插入空串
非空属性保证了这两个字段一定有值
默认值(default)
例1 default
没设置Default默认18
没填入的数据默认给出了
例2 not null default
意义:
不允许显示插入NULL,如果不显示插入NULL,自动给这个字段默认值,保证了这个字段一定不为空。
为空通过,因为有默认值18。
显示为空不通过。
没有default值不能给not null插入
例3
显示给NULL值 (不受约束not null影响)
ps
列描述 (comment)
zerofill
整数没有指名圆括号数据库也会自动在建表语句给出。
插入数据
对a列增加zerofill属性
并没有补0
zerofill,如果宽度小于设定宽度,自动填充为0;
为什么一个是11,一个是10;
多的一位是符号位。
为什么是10,10位字节可以表示所有的整数。
一个整数是范围是0~2^32-1 四个字节可以表示
主键
primary key 用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表最多只能有一个主键;主键所在的列通常是整数类型。
像搜索二叉树
记录:表里面的一行
属性值:表中一列信息
例:创建表的时候直接在字段上指定主键
主键重复(通过主键增删改查)
通过主键,一定能唯一选出一条数据。
不要给主键设置default值,没有意义。
例:不想要id当主键
1.删除主键约束。
2.没有主键约束了pri。
3.id不再是主键没有主键约束,有两个李四。
例:加上主键
1.有记录重复不能加上主键了
2.删除后再添加
3.desc t17
4. show create table t17;
复合主键(列+列...)如(班级+小学号)
1.建表
2.插入
3.冲突
自增长
如何设计主键 呢?
- 结合你的业务,我就可以选择一个唯一的列属性作为主键:
- 选择与业务无关的唯一值(特定设计的)->这样设计,主键值和业务无关,不影响整体主键的表结构
auto increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值。通常和主键搭配使用,作为逻辑主键。
自增长的特点:
- 任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
- 自增长字段必须是整数
- 一张表最多只能有一个自增长
1.本身不是一个索引(key一栏有值)
2.加主键key值是PRI
3.desc t19
4.插入
4.show create table t19;
auto_increment在建表语句是12;
每一次插入只要取出来插入,再++就好了;
5.插入id=1000
ddl告诉我下一个一个插1001
6. 下一个正常插入延续ddl的值
7.我就想建表之后就从10000开始计数
唯一键
我们数据库中的学生表,本质其实是用来描述一堆学生信息,以一个学生为例:表中存的都是我们身上的属性值我们身上的属性值,有的是可以重复的,有的是不会重复的!唯一键和主键并不冲突共同补充表的完整性。
没有唯一键,导致两个人一个qq号,发邮件一个人没收到。
增加qq为唯一键,对qq进行唯一性约束。
主键:更多的是为了保证我们在查找的时候,找到唯一的一条记录
准一键:更多的是为了保证在表中,各个不同的值,一定要在mysql层面也保证他的唯一性
实例
1.drop table
2.建表,可以看到Key值为UNI
qq重复插入失败
实验SQL
select null;
select 0;
select '';
create table if not exists `myclass`(
class_name varchar(20) not null,
class_room varchar(20) not null
)engine=InnoDB default charset=utf8;
desc myclass;
insert into myclass values ('软件1班', '102教室');
insert into myclass values ('软件2班', '106教室');
select *from myclass;
insert into myclass values ('软件3班',NULL);
insert into myclass (class_name)values ('软件3班');
insert into myclass values ('软件3班','');
create table if not exists t13(
name varchar(20) not null,
age tinyint unsigned default 18,
gender char(2) default '男'
);
desc t13;
insert into t13 (name)values('张三');
select * from t13;
insert into t13 (name,age)values('李四',26);
create table t14(
name varchar(20) not null,
age int not null default 18
);
desc t14;
insert into t14 values('张三丰','100');
insert into t14 (name,age)values('李四',NULL);
select * from t14;
insert into t14 (age)values(101);
create table if not exists t15 (
name varchar(20) not null,
age tinyint default 18 comment '这里是用户年龄,我们禁止18岁以下用户注册',
gender char(1) not null default '男' comment '这里是用户的性别'
);
desc t15;
select *from t15;
insert into t15 (name,age)values ('算了',99);
insert into t15 (name,age)values ('算了把',NULL);
create table if not exists `t16`(
a int not null,
b int unsigned not null
);
show create table t16;
insert into t16 values(6,2);
select *from t16;
alter table t16 modify a int(5) unsigned zerofill;
select * from t16;
alter table t16 modify a int(9) unsigned zerofill;
select * from t16 where a=1;
create table if not exists t17(
id int unsigned primary key comment '学生的学号是主键',
name varchar(20) not null
);
desc t17;
insert into t17 (id,name)values(1,'张三');
insert into t17 (id,name)values(2,'李四');
insert into t17 (id,name)values(3,'李四');
insert into t17 (id,name)values(3,'李四');
select *from t17 where id=3;
alter table t17 drop primary key;
desc t17;
delete from t17 where id =3;
alter table t17 add primary key(id);
show create table t17;
create table t18(
id varchar(20),
course varchar(20) comment '课程代码',
score tinyint unsigned default 60 comment '特定id的学生在特定课程中的得分',
primary key (id,course)
);
desc t18;
show create table t18;
insert into t18 (id,course,score) values('0001','0004',78);
insert into t18 (id,course,score) values('0002','0004',78);
insert into t18 (id,course,score) values('0001','0004',88);
create table if not exists t19(
id int unsigned primary key auto_increment,
name varchar(20) not null
);
desc t19;
insert into t19 (name) values('张飞');
insert into t19 (id,name) values(1000,'张飞');
select *from t19;
show create table t19;
create table t20(
id int unsigned auto_increment,
name varchar(20) not null,
primary key(`id`)
)engine=InnoDB auto_increment=10000 default CHARSET=utf8;
insert into t20 (name) values('马化腾');
select *from t20;
create table if not exists stus(
stu_id bigint unsigned primary key auto_increment,
name varchar(20) not null,
qq varchar(20) not null comment '注意',
telephone varchar(16) not null comment'注意'
)auto_increment=100;
desc stus;
insert into stus (name,qq,telephone) values('张三','1918@qq.com','19867899876');
insert into stus (name,qq,telephone) values('李四','1918@qq.com','19867899871');
select * from stus where qq='1918@qq.com';
select *from stus;
drop table stus;
create table if not exists stus(
stu_id bigint unsigned primary key auto_increment,
name varchar(20) not null,
qq varchar(20) unique not null comment '注意qq号码虽然不是主键但是是唯一键',
telephone varchar(16) unique not null comment'注意电话号码也应该保持唯一性'
)auto_increment=100;
desc stus;
insert into stus (name,qq,telephone) values('张三','1918@qq.com','19867899876');
insert into stus (name,qq,telephone) values('李四','1918@qq.com','19867899871');
引申
运行SQL快捷键
索引
书加目录,不是翻书的内容而是先看目录 ,空间记录索引,空间换时间。