alter table news add constraint fk_category foreign key(id) references category(id) --news新闻表,category新闻类别表
alter table comment add constraint fk_news foreign key(newsId) references news(id) --comment新闻评论表
以下是网上搜到的资料:
--创建student表 create table student ( studentid varchar(10) primary key, name varchar(20), sex varchar(2), password varchar(20) not null default '000000', age int, classid varchar(10) not null, address varchar(50) ) --创建course表 create table course ( courseid varchar(10) primary key, coursename varchar(40), teacher varchar(20), kind varchar(20), coursetime varchar(20), limitenum int not null, registernum int not null )
--创建department表 create table department ( departid varchar(10) primary key, departname varchar(20), office varchar(40), telephone varchar(20), chairman varchar(20) ) --创建class表 create table class ( classid varchar(10) primary key, departid varchar(10) not null, classname varchar(20), classmonitor varchar(20) ) --创建sc表 create table sc ( studentid varchar(10), courseid varchar(10), grade float, constraint pk_sc primary key(studentid,courseid) )
alter table student add constraint fk_student foreign key(classid) references class(classid)
alter table class add constraint fk_class foreign key(departid) references department(departid)
alter table sc add constraint fk_sc1 foreign key(studentid) references student(studentid), constraint fk_sc2 foreign key(courseid) references course(courseid)