我在铁路数据库中创建了3个表。一个表(TrainTravel)使用两个字段(TrainNo)和(TravelDate)作为主键。 另一个预订表使用(TrainTravelID)作为外键,它使用TrainTravel Table的主键。
应该在Booking表中为(TrainTravelID)定义一个数据类型。
railway_create.sql
use railway;
create table Booking(
PassengerID int(4),
TrainTravelID
);
create table TrainTravel(
TrainNo int(3),
TravelDate date,
TrainName varchar(20),
BoardingStation varchar(20),
Destination varchar(20),
Ac1Seats int(3),
Ac3Seats int(3),
SlSeats int(3),
SeSeats int(3),
constraint JourneyID PRIMARY KEY (TrainNo,TravelDate)
);
create table Person(
Name varchar(20),
LoginID int(4),
Password varchar(20),
Address varchar(50),
Phno int(10),
constraint PRIMARY KEY (LoginID)
);
railway_alter.sql
use railway;
alter table Booking
add constraint fk_TrainTravelID foreign key as (TrainTravelID) references Train(JourneyID),
add constraint fk_PassengerID foreign key as (PassengerID) references Person(LoginID);
我想知道TrainTravel的组合主键的数据类型,以便它可以用作Booking表中的TrainTravelID属性的数据类型。
如果没有放置任何数据类型,则会出错,
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(int(3),date)
)' at line 3
答案 0 :(得分:0)
TrainTravelID在预订中没有数据类型。
您还需要预订一对夫妇。
创建表预订( PassengerID int(4), TrainNo int(3), 旅行日期 );
创建表TrainTravel( TrainNo int(3), 旅行日期, TrainName varchar(20), BoardingStation varchar(20), 目的地varchar(20), Ac1Seats int(3), Ac3Seats int(3), SlSeats int(3), SeSeats int(3), 约束JourneyID PRIMARY KEY(TrainNo,TravelDate) );
创建表人( 名称varchar(20), LoginID int(4), 密码varchar(20), 地址varchar(50), Phno int(10), 约束PRIMARY KEY(LoginID) );
alter table Booking
添加约束fk_TrainTravelID外键(TrainNo,TravelDate)引用TrainTravel(TrainNo,TravelDate),
添加约束fk_PassengerID外键(PassengerID)引用Person(LoginID);