mysql 基本指令 1
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 (userid, hostid),
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;
分页:
select * from stu limit 1,10;
select * from stu limi;
select * from stu order by sni xxx desc;
排序:
select * from stu order by sni desc limit 2; 从大到小
select * from stu order by sni asc;从小到大
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 (userid, hostid),
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;
分页:
select * from stu limit 1,10;
select * from stu limi;
select * from stu order by sni xxx desc;
排序:
select * from stu order by sni desc limit 2; 从大到小
select * from stu order by sni asc;从小到大
分组:
select count(id) from userinfo;
select count(id),part_id from userinfo1 group by part_id;
max
min
sum
avg
***** 如果对于聚合函数结果进行二次筛选时? 必须使用having ****
select count(id), part_id from userinfo1 group by part_id having count(id)>1;
select count(id) from userinfo;
select count(id),part_id from userinfo1 group by part_id;
max
min
sum
avg
***** 如果对于聚合函数结果进行二次筛选时? 必须使用having ****
select count(id), part_id from userinfo1 group by part_id having count(id)>1;
连表操作:
select * from userinfo1,userinfo2;
select * from userinfo1,userinfo2 where userinfo1.part_id = userinfo2.id;
select * from userinfo1 left join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2左边全显示
select * from userinfo right join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2右边全显示
select * from userinfo1 innder join userinfo2 on userinfo1.part_id = userinfo2.id;
# 将出现null的行隐藏
select
work.id
from
work
left join userinfo1 on work.userinfo1_id = userinfo1.id
left join userinfo2 on work.userinfo2_id = userinfo2.id;
select * from userinfo1,userinfo2;
select * from userinfo1,userinfo2 where userinfo1.part_id = userinfo2.id;
select * from userinfo1 left join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2左边全显示
select * from userinfo right join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2右边全显示
select * from userinfo1 innder join userinfo2 on userinfo1.part_id = userinfo2.id;
# 将出现null的行隐藏
select
work.id
from
work
left join userinfo1 on work.userinfo1_id = userinfo1.id
left join userinfo2 on work.userinfo2_id = userinfo2.id;
此时此刻,非我莫属
【推荐】国内首个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)