create table teacher(
id int(11) not null auto_increment,
name varchar(10) not null,
primary key(id)
)engine=innodb;
create table teacherCourse(
teacherId int(11) not null,
courseNum int(10) not null,
courseName varchar(50) not null,
constraint foreign key(teacherId) references staff(id) on delete cascade,
primary key(teacherId, courseNum)
)engine=innodb;
我想在teacher表增加一条记录的同时也要为teacherCourse增加一条记录,问题是表1的id是自增的,能先获取刚插入自增的id然后作为表2的teacherId插入数据吗?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
1.
select (auto_increment-1) from information_scheme.tables where table_name='TableName'2.
select last_insert_id()Hibernate我用的比较少,平时用的是Mybatis,说说Mybatis的做法吧。
你的teacher表结构,id是主键,并且自增,是这样进行配置。mybatis xml文件里,需要在insert前面加上
即可
last_insert_id()是一种;
触发器也可以,
大致写了下,teacherCourse里面还有些是not null的也要插入