mysql中将表warehouse的 UpdateTime DATETIME COMMENT 'Update Time',修改为`UpdateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'updatetime'
时间: 2024-01-13 09:05:39 浏览: 122
您可以使用 ALTER TABLE 语句来修改表的列定义。要将表warehouse的UpdateTime列修改为timestamp类型并设置默认值,请使用以下语句:
```
ALTER TABLE warehouse MODIFY UpdateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'updatetime';
```
这将修改表warehouse的UpdateTime列定义为timestamp类型,使其在更新时自动更新,并将列名改为updatetime。请注意,如果有数据在该列中,它们将保留在列中。
相关问题
需求场景:仓库部门需要登记每次入库信息,入库信息如下:入库人、入库名称、入库时间、入库物料、入库数量。根据以上需求设计一个mysql表。用sql写出来。再用java代码将这个表基础的增删改查写出来
MySQL表设计:
CREATE TABLE warehouse (
id INT PRIMARY KEY AUTO_INCREMENT,
enter_name VARCHAR(50) NOT NULL,
enter_time DATETIME NOT NULL,
material_name VARCHAR(50) NOT NULL,
material_quantity INT NOT NULL
);
Java代码实现:
public class WarehouseDAO {
private String url = "jdbc:mysql://localhost:3306/dbname";
private String username = "root";
private String password = "password";
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet rs = null;
public void addWarehouse(String enterName, LocalDateTime enterTime, String materialName, int materialQuantity) {
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO warehouse (enter_name, enter_time, material_name, material_quantity) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, enterName);
stmt.setTimestamp(2, Timestamp.valueOf(enterTime));
stmt.setString(3, materialName);
stmt.setInt(4, materialQuantity);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
public void deleteWarehouse(int id) {
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM warehouse WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
public void updateWarehouse(int id, String enterName, LocalDateTime enterTime, String materialName, int materialQuantity) {
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "UPDATE warehouse SET enter_name = ?, enter_time = ?, material_name = ?, material_quantity = ? WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, enterName);
stmt.setTimestamp(2, Timestamp.valueOf(enterTime));
stmt.setString(3, materialName);
stmt.setInt(4, materialQuantity);
stmt.setInt(5, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
}
public List<Warehouse> getAllWarehouse() {
List<Warehouse> warehouses = new ArrayList<>();
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM warehouse";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String enterName = rs.getString("enter_name");
LocalDateTime enterTime = rs.getTimestamp("enter_time").toLocalDateTime();
String materialName = rs.getString("material_name");
int materialQuantity = rs.getInt("material_quantity");
Warehouse warehouse = new Warehouse(id, enterName, enterTime, materialName, materialQuantity);
warehouses.add(warehouse);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return warehouses;
}
public Warehouse getWarehouseById(int id) {
Warehouse warehouse = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM warehouse WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
String enterName = rs.getString("enter_name");
LocalDateTime enterTime = rs.getTimestamp("enter_time").toLocalDateTime();
String materialName = rs.getString("material_name");
int materialQuantity = rs.getInt("material_quantity");
warehouse = new Warehouse(id, enterName, enterTime, materialName, materialQuantity);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return warehouse;
}
private void closeAll() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class Warehouse {
private int id;
private String enterName;
private LocalDateTime enterTime;
private String materialName;
private int materialQuantity;
public Warehouse(int id, String enterName, LocalDateTime enterTime, String materialName, int materialQuantity) {
this.id = id;
this.enterName = enterName;
this.enterTime = enterTime;
this.materialName = materialName;
this.materialQuantity = materialQuantity;
}
// getters and setters
}
阅读全文