表关系(一对一 ,多对多)
desc 表名 --查看表属性
show create table 表名 \g; --查看代码
alter table 表名 auto_increment=20; --改自增的值
MySQL:自增步长
基于会话级别:
show session variables like 'auto_inc%'; --站看全局变量
set session auto_increment_increment=2; --设置会话步长
基于全局级别:
show global variables like 'auto_inc%'; --查看全局变量
set global auto_increment_increment=2; --设置会话步长
SQLServer:自增步长:
基础表级别:
create table 't1'(
'nid' int(11) not null auto_increment primary key,
'pid' int(11) not null,
'num' int(11) default null,
primary key('nid')
)ENGINE=InnoDB auto_increment=4, 步长=2 DEFAULT CHARSET=utf8
唯一索引:
create table t1(
id int ...,
num int,
xx int,
unique uq1 (num,xx)
)
PS:
唯一:
约束不能重复(可以为空)
PS:主键不能重复(不能为空)
加速查找
外键的变种:
a.用户表和部门表
用户:
1 alex 1
2 root 1
3 egon 2
4 laoyao 3
部门:
1 服务
2 保安
3 公关
====》 一对多
示列1
b. 用户表和博客表
用户表:
1 alex
2 root
3 egon
4 laoyao
博客表:
FK() + 唯一
1 /alex3714/ 4
2 /yuancheqi/ 1
3 /wupeiqi/ 1
====》一对一
create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table admin(
id int not null auto_increment primary key,
username varchar(64) not null,
password varchar(64) not null,
user_id int not null,
unique uq_u1 (user_id),
constraint fk_admin_ul foreign key (user_id) references userinfo1(id)
)engine=innodb default charset=utf8;
示列2
用户表
主机表
用户主机关系表
====》 多对多
create table userinfo2(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table host(
id int auto_increment primary key,
hostname char(64)
)engine=innodb default charset=utf8;
create table user2host(
id int auto_increment primary key,
userid int not null,
hostid int not null,
unique uq_user_host foreign key (userid) references userinfo2(id),
constraint fk_u2h_user foreign key (userid) references userinfo2(id),
constraint fk_u2h_host foreign key (hostid) references host(id)
)engine=innodb default charset=utf8;
a.用户表和部门表
用户:
1 alex 1
2 root 1
3 egon 2
4 laoyao 3
部门:
1 服务
2 保安
3 公关
====》 一对多
示列1
b. 用户表和博客表
用户表:
1 alex
2 root
3 egon
4 laoyao
博客表:
FK() + 唯一
1 /alex3714/ 4
2 /yuancheqi/ 1
3 /wupeiqi/ 1
====》一对一
create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table admin(
id int not null auto_increment primary key,
username varchar(64) not null,
password varchar(64) not null,
user_id int not null,
unique uq_u1 (user_id),
constraint fk_admin_ul foreign key (user_id) references userinfo1(id)
)engine=innodb default charset=utf8;
示列2
用户表
主机表
用户主机关系表
====》 多对多
create table userinfo2(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table host(
id int auto_increment primary key,
hostname char(64)
)engine=innodb default charset=utf8;
create table user2host(
id int auto_increment primary key,
userid int not null,
hostid int not null,
unique uq_user_host foreign key (userid) references userinfo2(id),
constraint fk_u2h_user foreign key (userid) references userinfo2(id),
constraint fk_u2h_host foreign key (hostid) references host(id)
)engine=innodb default charset=utf8;
此时此刻,非我莫属
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)