Hi all while studying for mysql
exam i came across this question in a past exam.
Information about the distance travelled so far by each truck is no longer needed. Instead we would like to store information about the present status of a truck. A status can be either Available , Used or Maintained
This status part of the question is where I'm a bit stuck. My only idea to tackle this issue would be through either a where clause or setting a string value between 4 and 9. Another idea i just had as perhaps using a check constraint?
Move the status to a diferent table status(id, name) and store status_id in trucks as a foreign key pointing to one of these
status(1, 'available')
status(2, 'used')
status(3, 'maintained')
The best way to do this would be to create a separate table containing the available status and then implement a foreign key between the trucks
relation and the status
relation:
CREATE TABLE status (
id INT NOT NULL PRIMARY KEY,
description VARCHAR(32)
);
CREATE TABLE trucks (
id INT NOT NULL PRIMARY KEY,
status_id INT NOT NULL,
INDEX idx_status_id (status_id),
FOREIGN KEY (status_id)
REFERENCES status(id)
);
Some people would say to use an ENUM type. However, I (and a lot of other DBA's that I have worked with) personally hate the ENUM type. Using an ENUM, you move data from where it belongs to the table metadata, updating the list is a very expensive operation, it is difficult to get all available values from your app, you can't store additional info with it, etc.
You could create ENUM
:
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time
CREATE TABLE t(
id INT NOT NULL PRIMARY KEY,
status ENUM('available','used', 'maintained')
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.