Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top