这是应该从表中删除学生的代码。 但它不起作用。 它只在表名是静态时才有效。
public function delete_student($student_class,$school_session,$id){
$table_name = $student_class."_".$school_session."_student_record";
$pdo = KITE::getInstance('pdo');
$query = $pdo->prepare("DELETE FROM ".$table_name." WHERE `id` = ?");
$query->bindValue(1,$id);
$query->execute();
}
答案 0 :(得分:2)
正如我在评论中提到的,在表格,列,函数名称,过程名称等的名称中不允许使用斜杠/
。为什么在名称静态输入时您的查询有效是一个谜。
快速构建的演示,使用innodb
表foreign keys
,希望很少/没有冗余。如果您在本地运行mysql,请尝试在您的gui中运行它(例如https://support.google.com/googleplay/android-developer/answer/140504?hl=en),以了解它是如何完成的。
drop database if exists `demo_school_sessions`;
create database if not exists `demo_school_sessions`;
use `demo_school_sessions`;
drop table if exists `classes`;
create table if not exists `classes` (
`id` int(10) unsigned not null auto_increment,
`class` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `classes` (`id`, `class`) values
(1, 'mathematics'),
(2, 'physics'),
(3, 'computing');
drop table if exists `sessions`;
create table if not exists `sessions` (
`id` int(10) unsigned not null auto_increment,
`session` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `sessions` (`id`, `session`) values
(1, 'summer'),
(2, 'winter'),
(3, 'spring'),
(4, 'autumn');
drop table if exists `students`;
create table if not exists `students` (
`id` int(10) unsigned not null auto_increment,
`student` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `students` (`id`, `student`) values
(1, 'bobby droptables'),
(2, 'sue select'),
(3, 'david delete'),
(4, 'valerie view'),
(5, 'peter procedure'),
(6, 'freddy function');
drop table if exists `year`;
create table if not exists `year` (
`id` int(10) unsigned not null auto_increment,
`year` varchar(9) not null default '2014/2015',
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `year` (`id`, `year`) values
(1, '2014/2015'),
(2, '2015/2016'),
(3, '2016/2017'),
(4, '2017/2018'),
(5, '2018/2019'),
(6, '2019/2020');
drop table if exists `master`;
create table if not exists `master` (
`id` int(10) unsigned not null auto_increment,
`student` int(10) unsigned not null default '0',
`class` int(10) unsigned not null default '0',
`session` int(10) unsigned not null default '0',
`year` int(10) unsigned not null default '0',
primary key (`id`),
key `student` (`student`),
key `class` (`class`),
key `session` (`session`),
key `year` (`year`),
constraint `fk_year` foreign key (`year`) references `year` (`id`) on delete cascade on update cascade,
constraint `fk_class` foreign key (`class`) references `classes` (`id`) on delete cascade on update cascade,
constraint `fk_session` foreign key (`session`) references `sessions` (`id`) on delete cascade on update cascade,
constraint `fk_stud` foreign key (`student`) references `students` (`id`) on delete cascade on update cascade
) engine=innodb default charset=latin1;
insert into `master` (`id`, `student`, `class`, `session`, `year`) values
(1, 1, 3, 4, 1),
(2, 1, 1, 4, 1),
(3, 2, 2, 4, 1),
(4, 2, 3, 4, 1);
/* example to select records */
select * from `master` m
left outer join `students` s on s.id=m.student
left outer join `classes` c on c.id=m.class
left outer join `sessions` ss on ss.id=m.`session`
left outer join `year` y on y.id=m.`year`
where m.`class`=3 or m.`student`=1;
/* Example to delete a student */
delete from `students` where `id`=1;
/*
*/
由于Foreign Key Dependencies
,如果您从学生表中删除学生,master
表中的所有相关记录也将被删除,因为cascade
设置了update
}和delete
。使用这样的foreign keys
时必须小心,因为如果您意外删除了一条记录,并且该密钥上有一个级联删除集,那么所有相关记录都会消失!