문제

I am a newbie to PHP. I have been given the code snippet below as homework:

CREATE  TABLE `admin_log`   (
`id`    INT(11) NOT NULL    AUTO_INCREMENT,
`statusdate`    DATETIME    DEFAULT NULL,
`type` INT(11) DEFAULT  NULL
PRIMARY KEY (`id`)  

)   ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin 

Why will it not be possible to set up any foreign key constraints using this table?

I have done some research on Google and I cant find a reason why foreign key constraints are not possible. Please help

도움이 되었습니까?

해결책

You can do that like this :

CREATE  TABLE `admin_log`   (
`id`    INT(11) NOT NULL    AUTO_INCREMENT,
`statusdate`    DATETIME    DEFAULT NULL,
`type` INT(11) DEFAULT  NULL,

 INDEX (type),
 FOREIGN KEY (type)
    REFERENCES type(id)
    ON UPDATE CASCADE ON DELETE RESTRICT,

PRIMARY KEY (`id`)  
)   ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=latin 

Notice the engine INNODB insetad of MYISAM which doesn't permit foreign key.

Or using MySQLAdmin in the "structure" tab, click on the "relationals view" link below the table description.

다른 팁

Even it has been mentioned on comment before -- just to make it more prominent: MyISAM is not supporting foreign keys. So you will need to change the engine of your table to e.g. INNODB if possible.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top