我是SQL的新手,除了更新之外,我能够在此数据库中完成所需的一切。我不知道为什么我一直收到1451错误:
无法删除或更新父行。外键约束失败
但由于某些原因我无法运行此行:
update team set TCollege='UNLV' where TCollege='Michigan';
然而,两条删除线都运行得很好
以下是所有SQL:
create database ncaa7;
use ncaa7;
select database();
create table team
(TCollege varchar(30) not null,
Conference varchar(15),
TicketPrice float(10, 2),
TCity varchar(10),
TState varchar(10),
primary key(TCollege))
Engine=INNODB;
insert into team values('Kansas', 'DIV I', 200.0, 'Lawrence', 'KS');
insert into team values('Arkansas Tech', 'DIV II', 300.0, 'Russel', 'Ar');
insert into team values('Michigan', 'DIV I', 400.0, 'Detroit', 'MI');
create table highschool(
HighSchoolName varchar(30) not null,
HCity varchar(15),
HState varchar(15),
NumberOfCollegiateAthletes int(10),
primary key(HighSchoolName)
)
Engine=INNODB;
insert into highschool values('Bryant', 'Bryant', 'Arkansas', 2);
insert into highschool values('Sheridan', 'Sheridan', 'Arkansas', 2);
insert into highschool values('Jackson', 'Jackson', 'Arkansas', 2);
create table coach(
CoachName varchar(15) not null,
Title varchar(15),
Salary float(6, 2),
YearStarted int(4),
CCollege varchar(15),
primary key(CoachName),
foreign key(CCollege) references team(TCollege) on delete set null on update cascade
)
Engine=INNODB;
insert into coach values('John', 'Head Coach', 600.00, 1996, 'Arkansas Tech');
insert into coach values('Don', 'Assistant', 1000.00, 2004, 'Kansas');
insert into coach values('Shaun', 'Assistant', 1000.00, 2004, 'Michigan');
create table player(
PCollege varchar(30) not null,
JerseyNumber int(2),
PlayerName varchar(30),
YearEntered int(4),
PPosition varchar(2),
PHighSchoolName varchar (30) not null,
primary key(PCollege, JerseyNumber),
foreign key(PCollege) references team(TCollege) on delete cascade on update cascade,
foreign key(PHighSchoolName) references highschool(HighSchoolName) on delete cascade on update cascade
)
Engine=INNODB;
insert into player values('Kansas', 34, 'Mike', 1996, 'PG', 'Sheridan');
insert into player values('Arkansas Tech', 37, 'shaun', 1998, 'SF', 'Bryant');
insert into player values('Michigan', 29, 'bill', 2004, 'C', 'Jackson');
create table scholarshipoffer(
OfferingCollege varchar(30) not null,
CurrentCollege varchar(30) not null,
JerseyNumber int(2) not null,
Amount float(10, 2),
primary key(OfferingCollege, CurrentCollege, JerseyNumber),
foreign key(OfferingCollege) references team(TCollege) on update cascade on delete cascade,
foreign key(CurrentCollege) references team(TCollege) on update cascade on delete cascade,
foreign key(CurrentCollege, JerseyNumber) references player(PCollege, JerseyNumber) on update cascade on delete cascade
)
Engine=INNODB;
create table previousjobs(
PreCollege varchar(30) not null,
PreCoachName varchar(30) not null,
Title varchar(15),
NumberOfYears int (10),
primary key(PreCollege, PreCoachName),
foreign key(PreCollege) references team (TCollege) on delete cascade on update cascade,
foreign key(PreCoachName) references coach(CoachName) on delete cascade on update cascade
)
Engine=INNODB;
create table game(
HomeCollege varchar(30) not null,
VisitingCollege varchar(30) not null,
Homescore int (15),
VisitingScore int (15),
primary key(HomeCollege, VisitingCollege),
foreign key(HomeCollege) references team (TCollege) on delete cascade on update cascade,
foreign key(VisitingCollege) references team(TCollege) on delete cascade on update cascade
)
Engine=INNODB;
show databases;
show tables;
describe team;
describe scholarshipoffer;
select * from team;
select * from player;
update team set TCollege='UNLV' where TCollege='Michigan';
select * from team;
select * from player;
select * from team;
select * from player;
delete from team where TCollege='Kansas';
select * from team;
select * from player;
select * from team;
select * from coach;
delete from team where TCollege='Arkansas Tech';
select * from team;
select * from coach;