MYSQLÍâ¼ü(Foreign Key)µÄʹÓ㨶þ£©
×÷ÕߣºDirk (dirk.ye AT gmail.com)
Url£ºhttp://dirk.pdx.cn
ÈÕÆÚ£º2004/12/08
Ê×ÏÈ£¬Ä¿Ç°ÔÚ²úÆ·»·¾³¿ÉÓõÄMySQL°æ±¾£¨Ö¸4.0.xºÍ4.1.x£©ÖУ¬Ö»ÓÐInnoDBÒýÇæ²ÅÔÊÐíʹÓÃÍâ¼ü£¬ËùÒÔ£¬ÎÒÃǵÄÊý¾Ý±í±ØÐëʹÓÃ
InnoDBÒýÇæ¡£
ÏÂÃ棬ÎÒÃÇÏÈ´´½¨ÒÔϲâÊÔÓÃÊý¾Ý¿â
񡜧
CREATE TABLE `roottb` (
`id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL,
`data` VARCHAR(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) TYPE=InnoDB;
CREATE TABLE `subtb` (
`id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL,
`rootid` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`data` VARCHAR(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
INDEX (`rootid`),
FOREIGN KEY (`rootid`) REFERENCES roottb(`id`) ON DELETE CASCADE
) TYPE=InnoDB;
×¢Ò⣺
1¡¢±ØÐëʹÓÃInnoDBÒýÇ棻
2¡¢Íâ¼ü±ØÐ뽨Á¢Ë÷Òý£¨INDEX£©£»
3¡¢Íâ¼ü°ó¶¨¹ØϵÕâÀïʹÓÃÁË“ ON DELETE
CASCADE”£¬Òâ˼ÊÇÈç¹ûÍâ¼ü¶ÔÓ¦Êý¾Ý±»É¾³ý£¬½«¹ØÁªÊý¾ÝÍêȫɾ³ý£¬¸ü¶àÐÅÏ¢Çë²Î¿¼MySQLÊÖ²áÖйØÓÚInnoDBµÄÎĵµ£»
ºÃ£¬½Ó×ÅÎÒÃÇÔÙÀ´²åÈë²âÊÔÊý¾Ý£º
INSERT INTO `roottb` (`id`,`data`)
VALUES ('1', 'test root line 1'),
('2', 'test root line 2'),
('3', 'test root line 3');
INSERT INTO `subtb` (`id`,`rootid`,`data`)
VALUES ('1', '1', 'test sub line 1 for root 1'),
('2', '1', 'test sub line 2 for root 1'),
('3', '1', 'test sub line 3 for root 1'),
('4', '2', 'test sub line 1 for root 2'),
('5', '2', 'test sub line 2 for root 2'),
('6', '2', 'test sub line 3 for root 2'),
('7', '3', 'test sub line 1 for root 3'),
('8', '3', 'test sub line 2 for root 3'),
('9', '3', 'test sub line 3 for root 3');
ÎÒÃÇÏÈ¿´Ò»Ïµ±Ç°Êý¾Ý±íµÄ״̬£º
mysql>; show tables;
+----------------+
| Tables_in_test |
+----------------+
| roottb |
| subtb |
+----------------+
2 rows in set (0.00 sec)
mysql>; select * from `roottb`;
+----+------------------+
| id | data |
+----+------------------+
| 1 | test root line 1 |
| 2 | test root line 2 |
| 3 | t
Ïà¹ØÎĵµ£º
µ±mysqlÊý¾Ý¿âΪGBK±àºÅ£¬.net³ÌÐòʹÓÃĬÈϱàÂ루UTF-8£©£¬ÏòmysqlÖвåÈëÊý¾ÝÈÝÒײúÉúÂÒÂë¡£½â¾ö°ì·¨ÊÇÔÚÁ¬½Ó×Ö·û´®¼ÓÈëCharset=gbk£º
MySqlConnection myConnection = new MySqlConnection("Database=test2;Data Source=localhost;User Id=root;Password=ikmbikmb;Charset=gbk"); ......
ÎÊÌ⣺
1¡¢Êý¾Ý¿âĬÈϵÄ×Ö·û¼¯ÉèÖÃΪUTF-8£»
2¡¢Í¨¹ýibatis¶ÁдÊý¾ÝÖÐÎÄÕý³££»
3¡¢µ«ÊÇÓÃwindowsÃüÁîÐÐÖ´ÐÐselectÓï¾äÖÐÎÄÏÔʾÂÒÂë¡£
½â¾ö£º
Êý¾Ý¿âÓû§µÇ¼ʱ²ÎÊýÉ趨×Ö·û¼¯£º
mysql --default-character-set=gb2312 -u ̞ -p
£¨¹À¼ÆºÍwindowsµÄcmdÃüÁîÐÐÓйأ© ......
linux
ÏÂ mysql
Óû§µÄ¹ÜÀí
ÎÄÕ·ÖÀà:Êý¾Ý¿â
¹Ø¼ü×Ö: linux
mysql
Óû§¹ÜÀí
×Ô´ÓÉÏÔÚredhat Enterprise 5 Öа²×°ÁËMySQL
,Õâ´ÎÀ´Êµ¼ù²Ù×÷Ò»ÏÂMySQL
Óû§µÄ¹ÜÀí£»
Ò»¡¢rootÓû§ÃÜÂëµÄά»¤£º
ÓÉÓÚ°²×°MySQL
Íêºó,MySQL
»á×Ô¶¯Ìṩһ¸ö²»´ø ......
×î½üÏëÔÚ·þÎñÆ÷ÉÏÊÔÊÔMySQL5.1µÄ·ÖÇø£¬ÓÚÊǾÍÏë×Å×°¸öMySQL5.1£¬¾¹ýÍ´¿àµÄÒ»¶Î¾Àúºó£¬ÖÕÓÚÕÒ¸öÏÂÃæ×°¸öºÜºÃµÄ½Ì³Ì£¬ÌûÉÏÀ´±¸Íü¡£
1¡¢MySQLÃâ°²×°°æ/¶þ½øÖÆ°æÈí¼þ£¬²»ÓñàÒ룬ÏÂÔصØÖ·£º
http://dev.mysql.com/downloads/
Îļþ¸ñʽ£ºMYSQL-VERSION-OS.tar.gz
2¡¢´ ......
MySQL°æ±¾: Server version: 5.1.44 Source distribution
ÐÞ¸ÄrootÃÜÂë
Èç¹ûûÓÐÃÜÂëʹÓÃÏÂÃæµÄÃüÁî, ½«ÃÜÂëÉèΪ"123456"
$ mysqladmin -u root password 123456
Èç¹ûÓÐÃÜÂëʹÓÃÏÂÃæµÄÃüÁî, ½«ÃÜÂë¸ÄΪ"123456"
$ mysqladmin -u root -p password 123456
Enter password:
Óû§ÕË»§¹ÜÀí
Ìí¼ÓÕË» ......