how do you reference a foreign key
am 29.05.2006 21:50:43 von kal stevensI have been trying to write a database schema in mysql, and I cant
figure this out.
Here is a database schema
DROP DATABASE IF EXISTS d;
CREATE DATABASE d;
USE d;
CREATE TABLE t (
tid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE s (
sid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
tid INT NOT NULL REFERENCES t(tid),
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
I thought this would work, at least that is what the manual says.
I also tried Foreign Key instead of references, but I am having trouble
using Hibernate.
But I dont think it is referencing the foreign key properly.
When I back up my schema with the administrator tool, it does not have
the foreign key referenced.
I am using Server version 4.1.14
What am I doing wrong?
Here is the back up database schema
Thanks
Kal
-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 4.1.14
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,
FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'
*/;
--
-- Create schema d
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ d;
USE d;
--
-- Table structure for table `d`.`s`
--
DROP TABLE IF EXISTS `s`;
CREATE TABLE `s` (
`sid` int(11) NOT NULL auto_increment,
`tid` int(11) NOT NULL default '0',
PRIMARY KEY (`sid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `d`.`s`
--
/*!40000 ALTER TABLE `s` DISABLE KEYS */;
LOCK TABLES `s` WRITE;
UNLOCK TABLES;
/*!40000 ALTER TABLE `s` ENABLE KEYS */;
--
-- Table structure for table `d`.`t`
--
DROP TABLE IF EXISTS `t`;
CREATE TABLE `t` (
`tid` int(11) NOT NULL auto_increment,
PRIMARY KEY (`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `d`.`t`
--
/*!40000 ALTER TABLE `t` DISABLE KEYS */;
LOCK TABLES `t` WRITE;
UNLOCK TABLES;
/*!40000 ALTER TABLE `t` ENABLE KEYS */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;