在mysql中创建带有外键的表时出错

hsvhsicv  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(336)

我在mysql中用一些外键创建表时出错,下面是我的脚本:

CREATE TABLE tbl_mt_kuliah (

KD_AUTO INT (4) NOT NULL AUTO_INCREMENT,
NIM INT (10) NOT NULL,
KD_MATA_KULIAH INT (4) NOT NULL,
RUANG VARCHAR (4) NOT NULL,
KD_DOSEN INT (4) NOT NULL,
JAM TIME NOT NULL,
HARI DATE NOT NULL,
TH_AJARAN YEAR NOT NULL,
SMESTER INT (2) NOT NULL,
PRIMARY KEY KD_AUTO,
FOREIGN KEY FK_MATKUL(KD_MATA_KULIAH) REFERENCES mt_kuliah (KD_MATA_KULIAH),
FOREIGN KEY FK_MHS(NIM) REFERENCES tbl_mahasiswa (NIM),
FOREIGN KEY FK_DOSEN(KD_DOSEN) REFERENCES tbl_dosen (KD_DOSEN)

);

下面是错误:

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to 
your MariaDB server version for the right syntax to use near '
foreign KEY FK_MATKUL(KD_MATA_KULIAH) references mt_kuliah (KD_MATA_KULIAH),
' at line 12
pbossiut

pbossiut1#

我想这对你有帮助。

CREATE TABLE tbl_mt_kuliah (
    KD_AUTO INT (4) NOT NULL AUTO_INCREMENT,
    NIM INT (10) NOT NULL,
    KD_MATA_KULIAH INT (4) NOT NULL,
    RUANG VARCHAR (4) NOT NULL,
    KD_DOSEN INT (4) NOT NULL,
    JAM TIME NOT NULL,
    HARI DATE NOT NULL,
    TH_AJARAN YEAR NOT NULL,
    SMESTER INT (2) NOT NULL,
    PRIMARY KEY (KD_AUTO),
    CONSTRAINT FK_MATKUL FOREIGN KEY (KD_MATA_KULIAH) REFERENCES mt_kuliah (KD_MATA_KULIAH),
    CONSTRAINT FK_MHS FOREIGN KEY (NIM) REFERENCES tbl_mahasiswa (NIM),
    CONSTRAINT FK_DOSEN FOREIGN KEY (KD_DOSEN) REFERENCES tbl_dosen (KD_DOSEN)
 );

相关问题