Heim > Datenbank > MySQL-Tutorial > mysql在表中添加多个外键/增加外键/级联约束_MySQL

mysql在表中添加多个外键/增加外键/级联约束_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-01 13:29:50
Original
2488 Leute haben es durchsucht

Mysql外键

bitsCN.com

mysql在表中添加多个外键/增加外键/级联约束

 

1. 建表时创建外键: CREATE TABLE`xh` ( `id` int(100) unsigned NOT NULL AUTO_INCREMENT COMMENT , `cl_id` smallint(3) unsigned NOT NULL COMMENT, `title` varchar(100) COLLATE utf8_unicode_ci NOT NULL COMMENT , `details` text COLLATE utf8_unicode_ci NOT NULL COMMENT , `date` datetime NOT NULL COMMENT , `au_id` smallint(6) unsigned NOT NULL COMMENT , `click` int(100) unsigned NOT NULL DEFAULT '0' COMMENT , `reco` int(100) unsigned NOT NULL DEFAULT '0' COMMENT, PRIMARY KEY (`id`), KEY `fk_class` (`cl_id`), CONSTRAINT `fk_class`FOREIGN KEY  (`cl_id`)  REFERENCES  `fl` (`id`), KEY `fk_author` (`au_id`), CONSTRAINT `fk_author` FOREIGN KEY (`au_id`) REFERENCES `author` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci  2. 建表后创建外键: Alter Table `ym` Add Constraint `fk_author` Foreign Key (`au_id`) References `author` (`id`);   3. 查看表结构: Show Create Table `ym`; 4.级联: 在末尾可加上(可单独添加,也可全部添加):ON UPDATE CASCADE(级联更新)ON DELETE CASCADE(级联删除) 比如:ALTER TABLE `ym`  ADD  CONSTRAINT  `fk_author`  FOREIGN  KEY  (`au_id`)  REFERENCES  `author`  (`id`) ON UPDATE CASCADE
Nach dem Login kopieren

 


bitsCN.com
Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
So ändern Sie MySQL in MySQL
Aus 1970-01-01 08:00:00
0
0
0
MySQL-Startfehler unter Centos
Aus 1970-01-01 08:00:00
0
0
0
MySQL stoppt den Prozess
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage