我收到错误消息:
Error Code: 1005. Can't create table 'a_schuldb.st_stunden' (errno: 150)
我认为这是外键的问题。
这是我的代码。
drop database if exists a_schuldb;
create database a_schuldb;
use a_schuldb;
create table s_schueler (
s_schnr int primary key,
s_name varchar(15),
s_vorname varchar(10),
s_gebdat DATE,
s_adresse varchar(10),
s_k_klasse char (6)
);
create table k_klassen (
k_id char(6) primary key,
k_bez varchar(25),
k_s_klaspr int,
k_s_klasprstv int,
k_l_klavst int,
foreign key (k_s_klaspr) references s_schueler(s_schnr)
on delete set null
on update cascade,
foreign key (k_s_klasprstv) references s_schueler(s_schnr)
on delete set null
on update cascade ,
foreign key (k_l_klavst) references s_schueler(s_schnr)
on delete set null
on update cascade
);
alter table s_schueler add foreign key (s_k_klasse)
references k_klassen (k_id)
on update cascade
on delete set null;
create table l_lehrer (
l_id char(3) primary key,
l_name varchar(15),
l_vorname varchar(10),
l_gebdat DATE,
l_gehalt int,
l_l_chef char(3),
foreign key (l_l_chef) references l_lehrer(l_id)
on delete set null
on update cascade
);
create table r_raume (
r_id char(5) primary key,
r_plaetze int
);
create table st_stunden (
st_k_klasse char(6),
st_stunde char(5),
st_g_fach char (3),
st_l_lehrer char(3),
st_r_raum char (5),
constraint primary key (st_k_klasse, st_stunde,st_g_fach),
foreign key (st_k_klasse) references k_klassen(k_id)
on delete set null
on update cascade,
foreign key (st_l_lehrer) references l_lehrer(l_id)
on delete set null
on update cascade,
foreign key (st_r_raum) references r_raume(r_id)
on delete set null
on update cascade
);
create table p_pruefungen (
p_date Date,
p_s_kandidat int,
p_l_pruefer char (3),
p_g_fach char(3),
p_art varchar(3),
p_note int,
constraint primary key (p_date, p_s_kandidat, p_l_pruefer,p_g_fach),
foreign key (p_s_kandidat) references s_schueler(s_schnr)
on delete set null
on update cascade,
foreign key (p_l_pruefer) references l_lehrer(l_id)
on delete set null
on update cascade
);
create table g_gegenstaende (
g_id char(5) primary key,
g_bez varchar(50)
);
alter table st_stunden add foreign key (st_g_fach) references g_gegenstaende(g_id)
on delete set null
on update cascade;
alter table p_pruefungen add foreign key (p_g_fach) references g_gegenstaende(g_id)
on delete set null
on update cascade;
答案 0 :(得分:0)
st_stunden
定义中有一些错误:
foreign key (st_k_klasse) references k_klassen(k_id)
on delete set null
on update cascade,
on delete set null
但是st_k_klasse
列不允许空值。
列相同的问题:p_s_kandidat
和p_l_pruefer
但是 - 你不能将所有这些列设置为DEFAULT NULL,因为它们是主键的元素。
也许代替你的PK尝试将自动增量列添加为PK,如下所示:
CREATE TABLE `st_stunden` (
`st_id` int(11) NOT NULL AUTO_INCREMENT,
`st_k_klasse` char(6) DEFAULT NULL,
`st_stunde` char(5) NOT NULL DEFAULT '',
`st_g_fach` char(3) NOT NULL DEFAULT '',
`st_l_lehrer` char(3) DEFAULT NULL,
`st_r_raum` char(5) DEFAULT NULL,
PRIMARY KEY (`st_id`),
UNIQUE KEY `st_k_klasse` (`st_k_klasse`,`st_l_lehrer`,`st_r_raum`), -- optional
KEY `st_l_lehrer` (`st_l_lehrer`),
KEY `st_r_raum` (`st_r_raum`)
) ENGINE=InnoDB AUTO_INCREMENT=1 ;
ALTER TABLE `st_stunden`
ADD CONSTRAINT `st_stunden_ibfk_1` FOREIGN KEY (`st_k_klasse`) REFERENCES `k_klassen` (`k_id`) ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT `st_stunden_ibfk_2` FOREIGN KEY (`st_l_lehrer`) REFERENCES `l_lehrer` (`l_id`) ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT `st_stunden_ibfk_3` FOREIGN KEY (`st_r_raum`) REFERENCES `r_raume` (`r_id`) ON DELETE SET NULL ON UPDATE CASCADE;