USE school;
CREATE TABLE student(
id INT(10) NOT NULL PRIMARY KEY,
NAME VARCHAR(30) DEFAULT NULL,
tid INT(10) DEFAULT NULL,
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY(`tid`) REFERENCES `teacher` (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO student VALUES(1,'小明',1);
INSERT INTO student VALUES(2,'小紅',1);
INSERT INTO student VALUES(3,'小剛',1);
INSERT INTO student VALUES(4,'小王',1);
INSERT INTO student VALUES(5,'小智',1);
SELECT * FROM student;
CREATE TABLE teacher (
id INT(10) PRIMARY KEY NOT NULL,
NAME VARCHAR (30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO teacher VALUES(1,'陳老師');
SELECT * FROM teacher;
到此這篇關(guān)于sql語句創(chuàng)建外鍵關(guān)聯(lián)的文章就介紹到這了,更多相關(guān)sql創(chuàng)建外鍵關(guān)聯(lián)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!