Junior — Middle
How can a value be inserted into an enum type in SQL?
sobes.tech AI
Answer from AI
In SQL, the ENUM type represents a set of predefined string values. To insert a value into an ENUM field, you simply specify one of the allowed values in quotes during insertion.
For example, if you have a table:
CREATE TABLE users (
id INT PRIMARY KEY,
status ENUM('active', 'inactive', 'pending')
);
Inserting a value:
INSERT INTO users (id, status) VALUES (1, 'active');
It is important that the value strictly matches one of the options listed in the ENUM definition. If you specify a value not in the list, an error will occur.
In PHP, you can use prepared statements for insertion, for example with PDO:
$stmt = $pdo->prepare("INSERT INTO users (id, status) VALUES (:id, :status)");
$stmt->execute(['id' => 1, 'status' => 'active']);
Thus, inserting a value into an ENUM is a normal string insertion, but the value must be from the allowed set.