北大“数据库原理”上机实践题目总结(10)
答案:
/*建表*/
create table Numbers
(
NID char(5),
NName char(6) not null,
NAdd char(100) not null,
NCash int
primary key(NID),
check(NCash > 0)
)
create table Horses
(
HID char(5),
HCol char(4) not null,
HAdd char(100) not null,
primary key(HID)
)
create table RRecord
(
SerNum int,
HID char(5),
primary key(SerNum),
foreign key(HID) references Horses(HID)
)
create table CRecord
(
NID char(5) not null,
HID char(5) not null,
SerNum int not null,
foreign key(NID) references Numbers(NID),
foreign key(HID) references Horses(HID),
foreign key(SerNum) references RRecord(SerNum),
primary key(NID,SerNum)
)
/*插入数据*/
insert into Numbers
VALUES ('U001','Num1', 'address1',10)
insert into Numbers
VALUES ('U002','Num2', 'address2',20)
insert into Numbers
VALUES ('U003','Num3', 'address3',30)
insert into Horses
VALUES ('H001','R', 'address3')
insert into Horses
VALUES ('H002','G', 'address2')
insert into Horses
VALUES ('H003','B', 'address1')
insert into RRecord
VALUES (1,'H003')
insert into RRecord
VALUES (2,'H001')
insert into RRecord
VALUES (3,'H002')
insert into CRecord
VALUES ('U001','H003',1)
insert into CRecord
VALUES ('U001','H003',2)
insert into CRecord
VALUES ('U002','H003',1)
insert into CRecord
VALUES ('U003','H003',2)