-
Notifications
You must be signed in to change notification settings - Fork 0
/
020-ALTER命令.js
54 lines (47 loc) · 1.32 KB
/
020-ALTER命令.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @Date: 2023-06-06 19:39:33
* @LastEditors: with-the-winds
* @LastEditTime: 2023-06-06 19:39:47
* @Description: 头部注释
* @FilePath: \study-node-mysql\020-ALTER命令.js
*/
const mysql = require('mysql')
// 创建连接
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root123',
database: 'nodemysql2'
})
// 连接数据库
db.connect(err => {
if (err) throw err;
console.log('mysql connected ......');
})
/**
* 描述:当需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令
*/
// 删除表字段
const deleteSql = `ALTER TABLE goods DROP i`
db.query(deleteSql, (error, result) => {
if (error) throw error;
console.log('删除表字段成功', result);
})
// 新增表字段
const addSql = `ALTER TABLE goods ADD i INT`
db.query(addSql, (error, result) => {
if (error) throw error;
console.log('新增表字段成功', result);
})
// 修改表字段(名称和类型 MODIFY CHANGE)
const updateSql = `ALTER TABLE goods CHANGE i d INT`
db.query(updateSql, (error, result) => {
if (error) throw error;
console.log('修改表字段成功', result);
})
// 修改表名
const updateTableName = `ALTER TABLE goods RENAME TO prices`
db.query(updateTableName, (error, result) => {
if (error) throw error;
console.log(result);
})