天天看点

mysql关于外键设置

浠婂ぉ鍦ㄨ璁¢厭搴楃鐞嗙郴缁熸椂璧颁簡寰堝寮矾

1.mysql浣跨敤鐨勯粯璁ゆ暟鎹簱寮曟搸MyISAM骞朵笉鑳藉緢濂芥敮鎸佸閿紝浜嬪姟绛夋搷浣滐紝澶栭敭鎬绘槸璁剧疆涓嶄笂锛岄渶瑕佸皢寮曟搸鏀逛负InnoDB锛岃繖涓槸鏈€瀹屽杽鏀寔鍚勭sql鎿嶄綔鐨勩€?ENGINE=InnoDB)

2.鍦ㄦ渶鍒濊璁℃暟鎹簱鏄氨搴斿綋鎶婃墍鏈夊叧绯绘兂濂斤紝纭鍏崇郴姝g‘鍐嶅幓鐢熸垚鏁版嵁搴擄紝浠婂ぉ灏辨槸澶栭敭璁剧疆鐨勪笉瀵癸紝鏁翠釜鏁版嵁搴撶殑閫昏緫閮介敊浜嗭紝鍙ソ鍒犳帀鏁版嵁搴擄紝鐢╬ower designer閲嶆柊璁捐鏁版嵁搴撱€?

3.鍏充簬澶栭敭

create table roominfo

(

聽 聽r_code 聽 聽 聽 聽 聽 聽 聽 char(4) not null,

聽 聽r_type 聽 聽 聽 聽 聽 聽 聽 char(10) not null,

聽 聽r_price 聽 聽 聽 聽 聽 聽 聽float(8,2) not null,

聽 聽r_iflive 聽 聽 聽 聽 聽 聽 bool not null,

聽 聽r_floor 聽 聽 聽 聽 聽 聽 聽int not null,

聽 聽r_notes 聽 聽 聽 聽 聽 聽 聽longtext,

聽 聽primary key (r_code, r_type)

) ENGINE=InnoDB DEFAULT CHARSET=gbk;

create table orderinfo

(

聽 聽od_code 聽 聽 聽 聽 聽 聽 聽char(15) not null,

聽 聽r_code 聽 聽 聽 聽 聽 聽 聽 char(4),

聽 聽r_type 聽 聽 聽 聽 聽 聽 聽 char(10),

聽 聽od_intime 聽 聽 聽 聽 聽 聽datetime not null,

聽 聽od_outtime 聽 聽 聽 聽 聽 datetime not null,

聽 聽od_pay 聽 聽 聽 聽 聽 聽 聽 float(8,2) not null,

聽 聽od_hphone 聽 聽 聽 聽 聽 聽char(11),

聽 聽od_notes 聽 聽 聽 聽 聽 聽 longtext,

聽 聽primary key (od_code)

) ENGINE=InnoDB DEFAULT CHARSET=gbk;

alter table orderinfo add constraint FK_Relationship_1 foreign key (r_code, r_type)

聽 聽 聽 references roominfo (r_code, r_type) on delete cascade on update cascade;

鍦╫rderinfo琛ㄤ腑璁剧疆澶栭敭r_code, r_type(灞炰簬琛╮oominfo)

鍦ㄦ坊鍔犱俊鎭椂锛屽簲璇ュ厛娣诲姞roominfo鐨勪俊鎭紝鍐嶆坊鍔爋rderinfo锛屼笉鐒剁洿鎺ユ坊鍔爋rderinfo鎿嶄綔涓嶄細琚厑璁搞€?