-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_create_table.js
More file actions
41 lines (37 loc) · 1.13 KB
/
demo_create_table.js
File metadata and controls
41 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
/*Create a table named "customers":*/
var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
});
// Primary Key
// con.connect(function(err) {
// if (err) throw err;
// console.log("Connected!");
// var sql = "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))";
// con.query(sql, function (err, result) {
// if (err) throw err;
// console.log("Table created");
// });
// });
// If the table already exists, use the ALTER TABLE keyword:
// con.connect(function(err) {
// if (err) throw err;
// console.log("Connected!");
// 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");
// });
// });