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插入数据吗?
ringa_lee2017-04-17 15:16:31
1.select (auto_increment-1) from information_scheme.tables where table_name='TableName'
2.select last_insert_id()
天蓬老师2017-04-17 15:16:31
이는 사용하는 데이터 지속성 계층 프레임워크에 따라 다릅니다. 일반적으로 괜찮습니다.
mybatis의 경우 다음을 확인할 수 있습니다.
http://lavasoft.blog.51cto.com/62575/1384959/
http://my.oschina.net/u/1256344/blog/159703
최대 절전 모드에서도 마찬가지입니다. 자동 증가 ID를 얻으려면 hibernate/Mybatis를 검색하세요.
怪我咯2017-04-17 15:16:31
저는 Hibernate를 덜 사용합니다. 저는 주로 Mybatis를 사용합니다.
선생님 테이블 구조에서 id는 기본 키이며 자체적으로 증가됩니다. mybatis xml 파일에서 insert 앞에
그렇습니다
天蓬老师2017-04-17 15:16:31
last_insert_id()는
트리거도 사용할 수 있습니다.
대략 작성했는데, TeacherCourse에는 null이 아니고 삽입해야 하는 내용이 있습니다