文章目录
一、表字段的增删改
1、添加字段
alter table 表名 add 字段名 字段类型; #默认顺移往后添加
alter table 表名 add 字段名 字段类型 first; # 添加到表的第一行
alter table 表名 add 字段名 字段类型 after id; #添加到id的下一行 id:某一行
2、删除字段
alter table 表名 drop 字段名;
3、修改
3.1、修改表名
alter table 表名 rename to 新表名;
3.2、修复字段名
alter table 表名 change 旧字段名 新字段名 字段类型;
3.3、修改字段类型
alter table 表名 modify 字段 新字段类型;
总结:字段的增删改查基本上用不到,一般在设计表格的时候就会确定,这个知识点了解即可
二、约束
约束可以理解为限制,生而为人就会有道德约束和法律约束等等约束,也就是限制同样我们数据得有约束才能让其更加高效更加完美。
desc new_tb; 查看
1、默认约束 default
create table t1 (
id int default 101,
name varchar(10));
在没有设置默认值的情况下,默认值为NULL。
在设置默认值的情况下,默认值为你设置的值,插入数据没有写该字段的时候取默认值。
2、非空约束 not null
限制一个字段的值不能为空,insert的时候必须为该字段赋值。
create table t1(
id int not null,
name varchar(20),
age int default 20);
2.1、删除非空约束
alter table t1 change id id int;
2.2、添加非空约束
alter table t1 change id id int not null;
3、唯一约束 unique key
限制一个字段的值不能重复,该字段的数据不能重复出现,确保字段中的值唯一。
create table t1(
id int unique key,
name varchar(20),
age int default 20 #默认值20
);
3.1、删除唯一约束
drop index id on t1;
3.2、添加唯一约束
alter table t1 add unique(id)
4、主键约束 primary key
主键 = 非空 + 唯一
通常每张表都需要一个主键来体现唯一性,每张表里面只能有一个主键。
create table t1(
id int primary key,
name varchar(20),
age int default 20);
指定主键的另外一种方式:
create table t1(
id int,
name varchar(20),
age int default 20,
primary key(id)
);
4.1、删除主键
alter table t1 drop primary key;
4.2、添加主键
alter table t1 add primary key(id);
5、自增长约束 auto_increment
自动编号,和主键组合使用,一个表里面只能有一个自增长。
默认从1 开始增长。
create table t4(
id int primary key auto_increment,
age int default 18
); #auto_increment=50; 从50开始增长
6、外键约束
表与表之间产生联系,保持数据的一致性: 我有的你一定有,你没有的,我绝对没有。
······这个有点绕,还是直接例子说明吧。
create table a(
id_a int primary key,
name varchar(20)
);
create table b(
id_b int primary key,
name varchar(20),
foreign key(id_b) references ta(id_a)
);
首先,我们先创建两个表,把表b的id_b设置成表a的外键,那么我们只能先对表a进行数据插入(表a没数据时,不能对表b进行插入数据)。假设,表a先插入数据(1,‘wangwu’),(2,‘lisi’),那么表b的id_b插入的值只能为1或2。
三、表关系
1、一对一
两个键都为主键,非空且唯一,所以形成一对一的关系。
用外键的方式,把两个表关联, 主键连接主键。
create table stu( #创建学生表
id int primary key,
name varchar(20) not null
);
create table stu_details( #创建学生详细信息表
id int primary key,
age int,
addr varchar(20),
phone int,
foreign key(id) references stu(id) #两个表关联, 主键连接主键
);
2、一对多
用外键的方式,把两个表关联, 非主键连接主键。
create table academy(
a_id int primary key,
a_name varchar(20) not null
);
create table student(
s_id int primary key,
s_name varchar(20) not null,
aca_id int not null,
foreign key(aca_id) references academy(a_id) #非主键连接主键
);
3、多对多
这个···直接上示例吧
示例:学生选课
课程对多个学生 ,学生对多个课程;
需要创建中间表有两个字段(学生编号,课程编号);
课程表有两个字段(课程编号,课程名字);
主键加主键来实现的(联合主键);
create table course(
c_id int primary key,
c_name varchar(20)
);
create table student(
st_id int primary key,
st_name varchar(20)
);
create table choose_course(
c_id int,
st_id int,
primary key(c_id,st_id),
foreign key(c_id) references course(c_id),
foreign key(st_id) references student1(st_id)
);
----------------
数据插入:
insert into course values(1, 'python'),(2,'java'),(3,'c++');
insert into student values(1,'qiye'),(2,'gg');
insert into choose_course values(1,3);
insert into choose_course values(2,3);
insert into choose_course values(1,1);