alter table 子表名 add constraint FK_ID foreign key(子表中的字段名) REFERENCES 父表名(父表表主键名);
示例:
alter table t_dish_rec add constraint FK_ID foreign key(dish_no) REFERENCES t_dish(dish_no);
// 捕获插入外键异常
try{
dishRecMapper.insertList(list);
}catch (Exception e){
String sqlState = ((MySQLIntegrityConstraintViolationException) e.getCause()).getSQLState();
if(e.getCause() instanceof MySQLIntegrityConstraintViolationException && "23000".equals(sqlState)){
logger.error("" , e);
throw new ServiceException(RspCode.FAIL, RspMessage.DISH_NOT_EXC);
}
}