一个Teacher有多个Student,一个Student有一个Teacher,经过Teacher来管理Studentweb
Teachersql
public class Teacher
{
private int id;
private String name;
private Set<Student> stus=new HashSet<Student>();
}
Studentsvg
public class Student
{
private int id;
private String name;
}
teacherspa
t_id | t_name |
---|---|
14 | 刘继兵 |
student设计
s_id | s_name | tea_id |
---|---|---|
7 | 邓超元 | 14 |
8 | 余辉 | 14 |
tea_id位student表的外键code
create table teacher(t_id int primary key auto_increment,t_name char(20));
create table student(s_id int primary key auto_increment,s_name char(20),tea_id int);
alter table student add constraint t_s_const foreign key(tea_id) references teacher(t_id) on delete cascade on update cascade;
插入单挑数据:insert into teacher(t_name) values(?);
插入多条数据:insert into student(s_name,tea_id) values ('xio',1),('dad',2);
delete from teacher where t_id=?;
update teacher set t_name='jhonson' where t_id=?;
select teacher.*, student.s_id,student.s_name from teacher left outer join student on student.tea_id=teacher.t_id where teacher.t_id=?;