mysql - 使用最后插入的 id 作为用户名
问题描述
我希望每当我插入一行时,新生成的 id 都可以用作用户名,也可以插入到 user_name 字段中。任何关于我如何实现这一目标的想法都会非常感激。我试图在 mysql 工作台上运行下面的代码,除了插入语句之外,一切似乎都运行良好。
create database if not exists mydatabase;
create table if not exists mydatabase.mytable
(
id int auto_increment not null,
user_name int not null,
pass_word varchar(100),
primary key(id)
);
insert into mydatabase.mytable(user_name,pass_word) values (select SCOPE_IDENTITY(),'mypass');
解决方案
不确定您的 sql 版本是否支持多个 auto_increment 列。但是我认为您可以在每次插入时创建触发器或插入以下内容:
update my_table
set user_name = id
推荐阅读
- python - 如何使用规范文件中的文件
- python - 有没有办法为 Python Selenium 使用 ip:port:auth:password 代理?
- youtube - 有什么方法可以获取仅限会员的视频列表吗?
- python - 如何使用python在opencv中封闭不规则的图形轮廓并用5px的点填充它?
- delphi - 对于 TChart 系列,如何将渐变保持在 XY 轴内?
- python - 使用具有聚合列的 pandas 数据框绘制条形图
- python - 每天远程运行一次 Python 脚本的最佳方法是什么
- vb.net - 访问在 VB .NET 中添加(子)控件
- reactjs - 发送函数参数和参数的问题
- python - 如何使用循环列表中的键将值添加到字典中