如何知道中间表中的PK到FK链接?

时间:2017-11-15 17:53:38

标签: mysql database foreign-keys primary-key

我刚开始学习MySQL一个月前。如果有人可以在“加入”表上指导我并且它总是必须存在于M到M的关系中,我将非常感激吗?另外,更重要的是,如何识别它是中间/连接表?是否有连接表的替代品?在我下面的数据库中,我不确定我是否正在将我的中间表的PK和FK链接正确。我很容易混淆表之间的链接。

Drop Database if exists DBS;
Create database DBS;
use DBS;

create table Client(
client_id char(5) not null,
client_type varchar(30),
client_name varchar(50),
rep_firstname varchar(50),
rep_lastname varchar(50),
rep_phone varchar(30),
client_phone varchar(30),
client_address varchar(150),
client_state varchar(50),
primary key(client_id));

create table PhoneContact(
phone varchar(30) not null,
client_id char(5),
primary key (phone),
foreign key (client_id) references Client(client_id));

create table Staff(
staff_id char(5) not null,
staff_name varchar(20),
contract_details enum('full time, part time'),
phone varchar(20),
address varchar(150),
working_hours int(5),
pay_per_hour decimal(10,2) default null,
salary decimal(10,2) default 0,
primary key(staff_id));

create table Hostel(
hostel_id char(5) not null,
hostel_type enum('large dormitories', 'small shared rooms'),
price decimal(10,2),
max_guest int(3),
primary key(hostel_id));

create table Hotel(
hotel_id char(5) not null,
price decimal(10,2),
max_guest int(3),
primary key(hotel_id));

create table Camping(
camping_id char(5) not null,
price decimal(10,2),
max_guest int(3),
primary key(camping_id));

create table IndoorFacility(
indoor_id char(5) not null,
facility_type varchar(50),
start_time time,
checkout_time time,
booking_hours int(3),
charge_per_hour decimal(10,2),
primary key(indoor_id));

create table OutdoorFacility(
outdoor_id char(5) not null,
staff_id char(5),
booking_hours int(3),
outdoor_type varchar(50),
start_time time,
checkout_time time,
charge_per_hour decimal(10,2),
primary key(outdoor_id),
foreign key(staff_id) references Staff(staff_id));

create table Equipment(
equipment_id char(5) not null,
outdoor_id char(5),
equipment_name varchar(10),
price decimal(10,2),
booking_hours int(3),
primary key(equipment_id),
foreign key(outdoor_id) references OutdoorFacility(outdoor_id));

create table Payment(
payment_id char(5) not null,
client_id char(5),
payment_method enum('online','deposit'),
credit_card varchar(30),
created_at timestamp default current_timestamp,
primary key(payment_id),
foreign key(client_id) references Client(client_id));

create table AccommodationBooking(
accommodation_id char(5) not null,
hostel_id char(5),
hotel_id char(5),
camping_id char(5),
accommodation_type varchar(30),
booked_rooms int(3),
no_of_guests int(3),
primary key(accommodation_id),
foreign key(hostel_id) references Hostel(hostel_id),
foreign key(hotel_id) references Hotel(hotel_id),
foreign key(camping_id) references Camping(camping_id));

create table FacilityBooking(
facility_id char(5) not null,
indoor_id char(5),
outdoor_id char(5),
meal_type varchar(30),
created_at timestamp default current_timestamp,
primary key (facility_id),
foreign key(indoor_id) references IndoorFacility(indoor_id),
foreign key(outdoor_id) references OutdoorFacility(outdoor_id));

create table Booking(
booking_id char(5) not null,
client_id char(5),
accommodation_id char(5),
facility_id char(5),
checkin_date date,
checkout_date date,
created_at timestamp default current_timestamp,
primary key(booking_id),
foreign key (client_id) references Client(client_id),
foreign key (accommodation_id) references AccommodationBooking(accommodation_id),
foreign key (facility_id) references FacilityBooking(facility_id));

例如,从我上面的数据库中,它被认为是一个中间表?

0 个答案:

没有答案