I am new to mySQL development and I'm trying to set a simple hotel reservation database. One part of the database is the list of tables below:
- Hotels
- Rooms
The relationship is: One hotel contains many rooms, but one room belongs to one specific hotel (one-to-many relationship). So I designed the table like this:
CREATE TABLE Hotels (
HotelName CHAR(50) NOT NULL UNIQUE,
HotelEmail CHAR(200) NOT NULL,
Category CHAR(1) NOT NULL,
Street CHAR(200),
StreetNumber CHAR(5),
PostalCodhotelse CHAR(10),
City CHAR(20),
Country CHAR(30),
Phone CHAR(10),
PRIMARY KEY (HotelName)
);
Rooms
table contains the rooms with the primary key of the table combined with HotelName
and RoomNumber
. At this point, I want every time I insert a new HotelName
, theauto_increment (
RoomNumber`) to be reset, i.e.:
Hotel RoomNumber
------ ----------
HotelA 1
HotelA 2
HotelA 3
HotelB 1
HotelB 2
HotelC 1
So I designed the table Rooms
like this:
CREATE TABLE Rooms (
HotelName CHAR(200) NOT NULL,
RoomNumber int auto_increment,
RoomType CHAR(30),
NumOfBeds INT Unsigned,
Description TEXT,
PRIMARY KEY (HotelName, RoomNumber),
FOREIGN KEY (HotelName)
References hotels (HotelName)
ON UPDATE cascade
);
However, when I try to execute the last SQL statement to create the table, I get the message:
Error Code: 1075. Incorrect table definition; there can be only one auto column and it must be defined as a key
My question is how to set the combined key to the table so I can insert RoomNumber
every time I change the HotelName
?