I want to connect to my sql with node js and create tables if there are no tables, but I get the following error.
Mistake: sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS Trips (\n" + ' trip_id INT PRIMARY KEY AUTO_INCREMENT,\n' + " ' at line 9"
My code:
const createTables = `
CREATE TABLE IF NOT EXISTS Members (
user_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
member_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(40) NOT NULL,
date DATE,
time TIME
);
CREATE TABLE IF NOT EXISTS Trips (
trip_id INT PRIMARY KEY AUTO_INCREMENT,
city_id INT NOT NULL,
trip_description TEXT
);
CREATE TABLE IF NOT EXISTS Participants (
trip_id INT,
user_id INT,
PRIMARY KEY (trip_id, user_id),
FOREIGN KEY (trip_id) REFERENCES Trips(trip_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES Members(user_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS Cities (
city_id INT PRIMARY KEY AUTO_INCREMENT,
city_name VARCHAR(255) NOT NULL UNIQUE
);
`;
pool.query(createTables, (err, results) => {
if (err) {
console.error(err);
} else {
console.log('Tables created successfully');
}
});