"Example 2" creates a customer reservation form in the hotel management system and sets the customer number and room number to a federated primary key, the table structure is as follows:
|
Number |
Data type |
|
Customer ID |
|
|
The ID of the |
|
|
Check in Date |
|
|
Check out Date |
|
|
Amount |
7,2 ) |
|
Remarks |
$ ) |
The CREATE TABLE ordeinfo
(
CUSTOMER ID INTEGER,
The ID INTEGER,
CHECK in DATE DATETIME
CHECK out DATE DATETIME,
AMOUNT NUMERIC (7,2),
REMARKS Varcher (200)
Pimary KEY (Customerid,orderid)
);”
3 Adding a PRIMARY KEY constraint when modifying a table
To add a primary key constraint to a single column in the table if the data table already exists, the syntax is:
"ALTER TABLE table_name
ADD CONSTRAINT pk_name PRIMARY KEY (column name) "
"Example 1" creates a room information table (Roominfo) in the hotel management system, with the following table structure:
|
Number |
Data type |
|
Roominfo |
|
|
The typeID |
|
|
Roomprice |
7,2 ) |
|
Roomstate |
2 ) |
|
Roomark |
$ ) |
The code for creating the Room Information table (Roominfo) is as follows:
Crreate TABLE Roominfo
(
Roominfo INTEGER,
Roomtypeid INTEGER,
Roomprice NUMERIC (7,2),
Roomstate VARCHAR (2),
Roomark VARCHAR (200)
);
To add a primary key constraint to the Roomid column in the Room Information table (roominfo), the code is as follows
ALTER TABLE Roominfo
(
ADD CONSTRAINT pk_roominfo PRIMARY KEY (roomid)