Więc próbuję dodać klucz obcy do tabeli, ale nadal otrzymuję ten błąd:
Kod błędu: 1215. Nie można dodać ograniczenia klucza obcego
Kod jest następujący
create table Students
(
ID char(10) primary key not null,
StudentsName varchar(30),
Address varchar(40),
StudentsTelephone varchar(20),
constraint M1 check(len(NIM)=10),
constraint M2 check(NIM like "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
)
create table Lecturers
(
CodeLecturer char(5) primary key not null,
LecturersName varchar(30),
Salary numeric,
Address varchar(40),
LecturerTelephone varchar(20),
constraint D1 check(len(CodeLecturer)=5),
constraint D2 check(CodeLecturer like "D[0-9][0-9][0-9][0-9]")
)
To jest kod klucza obcego
create table TrHeaderKampus
(
TransactionCOde char(5) primary key not null,
ID char(10) not null,
CodeLecturer char(5) not null,
DateandTime datetime,
constraint H1 check(len(TransactioniCode)=5),
constraint H2 check(TransactionCode like "TR[0-9][0-9][0-9]"),
foreign key(ID)REFERENCES Students ON UPDATE CASCADE ON DELETE CASCADE,
foreign key(CodeLecturer)REFERENCES Lecturers ON UPDATE CASCADE ON DELETE CASCADE
)
Odpowiedzi:
0 dla odpowiedzi № 1W dokumentacji MySQL na za pomocą kluczy obcych wyjaśnia: musisz podać nie tylko tabelę, ale także pola (pola), do których odnosi się klucz obcy.
[CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) [ON DELETE reference_option] [ON UPDATE reference_option]
Dodaj nazwy pól do więzów klucza obcego, do których się odwołują.