How can the concept of "Autoincrement" be effectively utilized when splitting and importing data into multiple tables in PHP and MySQL/MariaDB?
When splitting and importing data into multiple tables in PHP and MySQL/MariaDB, the concept of "Autoincrement" can be effectively utilized by ensuring that the primary key in each table is set to auto increment. This allows the database to automatically generate a unique ID for each record inserted into the table, simplifying the process of managing relationships between the tables.
// Create a table with auto increment primary key
$sql = "CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
)";
// Insert data into the table
$sql = "INSERT INTO users (username, email) VALUES ('john_doe', 'john.doe@example.com')";
$result = mysqli_query($conn, $sql);
// Get the auto generated ID of the inserted record
$user_id = mysqli_insert_id($conn);
// Use the auto generated ID in another table
$sql = "INSERT INTO user_details (user_id, full_name, age) VALUES ('$user_id', 'John Doe', '30')";
$result = mysqli_query($conn, $sql);