目录
MySQL常用数据类型
数值类型
int(n):整数类型
bigint(n):大型整数
double(n):双精度
decimal(n,m):decimal(10,2)数值一共有十位,小数位两位
字符串类型
char(n):定长字符串,最多存储255个字符,长度不可变,如果字符长度少于n,则用“ ”填充
varchar(n):可变字符串,最大长度65535
text:文本数据
longtext:文本数据
日期类型
date:只能存储年月日
datetime:日期+时间 存储年月日时分秒 ,形如:2021-12-14 09:21:32
timestamp:时间戳日期+时间 存储年月日时分秒 ,形如:20211214 092132
字段约束
创建数据表时,对数据表的列的数据限制性的要求
给表中的列添加约束的原因
保证数据的有效性
保证数据的完整性
保证数据的正确性
常见的约束
非空约束(not null):限制此列的值必须提供,不能为空
唯一约束(unique):在表中的多条数据,此列的值不能重复
主键约束(primary key):非空+唯一,能够唯一标识数据表中的一条数据
外键约束(foreign key):加你不同表之间的关联关系
非空约束
创建书表
drop table if exists books;
create table books(
book_isbn char(6),
book_name varchar(10) not null,
book_author varchar(10)
);
添加数据时没给书名:
唯一约束
在创建book表的时候,把isbn后加上unique即可
book_isbn char(6) unique,
主键约束
创建表时设定主键:在创建book表的时候,把isbn后加上primary key即可
book_isbn char(6) primary key,
或者
create table books(
book_isbn char(6),
...,
primary key(book_isbn)
);
创建表的后,再创建主键:
alter table books modify book_isbn char(7) primary key;
特点:主键不能为null,也不能重复
删除主键约束:
alter table books drop primary key;
联合主键
将数据表中的多列组合在一起设置为表的主键
create table grades(
stu_num char(6) not null,
course_id int,
score int,
primary key(stu_num,course_id);
);
外键约束
将表中添加一列外键与另一张表的主键相关联,这个外键约束中的数据,在关联表中的主键李必须存在
创建表时的定义方式:(一般先创建不带外键的表,在创建带外键的表)
constraint 外键名 foreign key(外键列名) references 被关联表名(对方表主键的列名)
create table classes(
class_id int primary key auto_increment,
class_name varchar(50),
class_remark varchar(200)
);
# 创建表的时候定义cid列,并和班级表进行关联,cid的类型必须和对方主键类型一致
create table students(
stu_num char(8) primary key,
stu_name varchar(20) not null,
stu_gender char(2),
stu_age int,
cid int,
constraint FK_student_class foreign key(cid) references classes(class_id)
);
创建表后再定义主键:
alter table 表名 add constraint 外键名 foreign key(外键列名) references 另一张表名 (“另”的主键)
create table students(
stu_num char(8) primary key,
stu_name varchar(20) not null,
stu_gender char(2),
stu_age int,
cid int
);
alter table students add constraint FK_student_class foreign key(cid) references classes(class_id)
级联
准备代码:
insert into classes (class_name,class_remark)
values
('Java01','aaa'),('Java02','bbb'),('Html01','ccc'),('html02','ddd')
insert into students (stu_num,stu_name,stu_gender,stu_age,cid)
values
(20140201,'邱爱军','男',20,1),(20140202,'籍静','女',21,1),
(20140203,'吴岩','男',20,3),(20140204,'张洪明','男',21,4)
当学生中存在学生信息关联班级表中的某个记录时,就不能对班级表的这条记录进行修改和删除操作
Cannot delete or update a parent row: a foreign key constraint fails (`db_test02`.`students`, CONSTRAINT `FK_student_class` FOREIGN KEY (`cid`) REFERENCES `classes` (`class_id`))
如何修改:
方法一:(傻瓜式操作)
1.将student表中引用了Java01的cid设置为null;
2.修改classes中Java01的内容
3.把student表中的cid设为新的Java01的id
update students set cid = null where cid=1;
update classes set class_id=5 where class_id=1;
update students set cid = 5 where cid is null;
方法二:级联
1.先删除外键约束
2.重新添加外键约束后,使用关键字 ON UPDATE CASCADE ON DELETE CASCADE来设置级联修改和级联删除
3.直接修改classes表后,和它关联的students中的数据也会跟着一起修改
alter table students drop foreign key FK_student_class;
alter table students add constraint FK_student_class foreign key(cid) references classes(class_id) on update cascade on delete cascade;
update classes set class_id=1 where class_name='Java01';
删除同理,不写了