数据库:
1 create database TourVote; 2 create table VoteTheme 3 ( 4 themeID char(10) primary key, 5 theme char(40), 6 voteSum int 7 ) 8 create table Tourism 9 ( 10 tourID char(10) primary key, 11 tourName char(20), 12 introduction text , 13 image char(20), 14 ) 15 create table VoteResult 16 ( 17 themeID char(10), 18 tourID char(10), 19 voteNum int, 20 voteRate int, 21 primary key(themeID,tourID), 22 foreign key(themeID)references VoteTheme(themeID) , 23 foreign key(tourID)references Tourism(tourID) , 24 ) 25 create table Admin 26 ( 27 ID char(10) primary key, 28 name char(10), 29 pwd char(10) 30 )
alter table VoteResult
add constraint
voteNum
default 0 for voteNum ;
加约束:
在VoteResult中
check(tourID in select tourID from Tourism)
check(themeID in select themeID from VoteTheme)
foreign key(themeID)references VoteTheme(themeID) on delete on update
foreign key(tourID)references Tourism(tourID) on delete on update
1. cascade,级联操作。主表数据被更新(主键值更新),从表也被更新(外键值更新)。主表记录被删除,从表相关记录也被删除。
2. set null,设置为null。主表数据被更新(主键值更新),从表的外键被设置为null。主表记录被删除,从表相关记录外键被设置成null。但注意,要求该外键列,没有not null属性约束。
3. restrict,拒绝父表删除和更新。