demo_alter_table_pk.js:
var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "myusername", password: "mypassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); //Add primary key to an existing table: var sql = "ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY"; con.query(sql, function (err, result) { if (err) throw err; console.log("Table altered"); }); });
C:\Users\My Name>node demo_alter_table_pk.js
Connected!
Table altered