2013年9月23日星期一

Ask the next question MYSQL foreign keys .

create table studcourse
(stucourseid int primary key auto_increment,
sid int references student (sid),
cid int references course (cid),
grade int (3) not null

);

this way to create, set up a foreign key Mody. This is the Austrian tolerance test , but my computer only MYSQL, originally written so no problem.

Finally, I SHOW create table studcourse time , did not see foreign keys .
studcourse | CREATE TABLE `studcourse`
`stucourseid` int (11) NOT NULL AUTO_IN
`sid` int (11) DEFAULT NULL,
`cid` int (11) DEFAULT NULL,
`grade` int (3) NOT NULL,
PRIMARY KEY (`stucourseid`)
ENGINE = InnoDB DEFAULT CHARSET = utf8 |

But then , I want to add a foreign key.
alter table studcourse add foreign key (sid) references student (cid);
The results suggest that the error


Can't create table 'hibernate. # sql-7c8_1' (errno: 150)



------ Solution ------------------------------------ --------
program rarely used foreign key of
------ Solution --------------------- -----------------------
estimate you are using MyISAM database engine , it does not support foreign keys, you can change the database engine innodb should try it.
to your code as an example

reate table studcourse(
 stucourseid int primary key auto_increment,
 sid int references student(sid),
 cid int references course(cid),
 grade int(3) not null,
 CONSTRAINT FOREIGN KEY (`sid`) references `student`(`sid`),
 CONSTRAINT FOREIGN KEY (`cid`) references `course`(`cid`)
 )engine=innoDB;

perform this before the above sql to ensure student and course of these two tables is innoDB job , as to how to modify the database engine can find online .




create table t5 (c1 int, c2 int, CONSTRAINT FOREIGN key (`c2`) references `t3` (`c1`)) engine = innoDB < br> ------ Solution ----------------------------------------- ---


behind is my test code , forgot to delete , sorry.

没有评论:

发表评论