用Java语句创建订单表,用户表,价格表和管理员表,并且表与表之间建立连接,实现增删改查要有触发器
时间: 2024-01-18 15:02:13 浏览: 70
好的,下面是一个示例代码,可以创建这些表并建立连接,并实现增删改查以及触发器:
```java
// 导入所需的包
import java.sql.*;
public class CreateTable {
public static void main(String[] args) throws Exception {
// 数据库连接信息
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "root";
// 建立连接
Connection conn = DriverManager.getConnection(url, username, password);
// 创建用户表
String sql1 = "CREATE TABLE users (" +
"id INT NOT NULL AUTO_INCREMENT," +
"username VARCHAR(50) NOT NULL," +
"password VARCHAR(50) NOT NULL," +
"PRIMARY KEY (id)" +
")";
PreparedStatement stmt1 = conn.prepareStatement(sql1);
stmt1.executeUpdate();
// 创建价格表
String sql2 = "CREATE TABLE prices (" +
"id INT NOT NULL AUTO_INCREMENT," +
"name VARCHAR(50) NOT NULL," +
"price DECIMAL(10,2) NOT NULL," +
"PRIMARY KEY (id)" +
")";
PreparedStatement stmt2 = conn.prepareStatement(sql2);
stmt2.executeUpdate();
// 创建订单表
String sql3 = "CREATE TABLE orders (" +
"id INT NOT NULL AUTO_INCREMENT," +
"user_id INT NOT NULL," +
"price_id INT NOT NULL," +
"order_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP," +
"PRIMARY KEY (id)," +
"FOREIGN KEY (user_id) REFERENCES users(id)," +
"FOREIGN KEY (price_id) REFERENCES prices(id)" +
")";
PreparedStatement stmt3 = conn.prepareStatement(sql3);
stmt3.executeUpdate();
// 创建管理员表
String sql4 = "CREATE TABLE admins (" +
"id INT NOT NULL AUTO_INCREMENT," +
"username VARCHAR(50) NOT NULL," +
"password VARCHAR(50) NOT NULL," +
"PRIMARY KEY (id)" +
")";
PreparedStatement stmt4 = conn.prepareStatement(sql4);
stmt4.executeUpdate();
// 创建触发器,实现订单表插入数据时更新用户的余额信息
String sql5 = "CREATE TRIGGER update_user_balance AFTER INSERT ON orders FOR EACH ROW " +
"UPDATE users SET balance = balance - (SELECT price FROM prices WHERE id = NEW.price_id) " +
"WHERE id = NEW.user_id";
PreparedStatement stmt5 = conn.prepareStatement(sql5);
stmt5.executeUpdate();
// 插入一些示例数据
String sql6 = "INSERT INTO users (username, password) VALUES ('user1', 'password1')";
PreparedStatement stmt6 = conn.prepareStatement(sql6);
stmt6.executeUpdate();
String sql7 = "INSERT INTO prices (name, price) VALUES ('product1', 10.00)";
PreparedStatement stmt7 = conn.prepareStatement(sql7);
stmt7.executeUpdate();
String sql8 = "INSERT INTO orders (user_id, price_id) VALUES (1, 1)";
PreparedStatement stmt8 = conn.prepareStatement(sql8);
stmt8.executeUpdate();
// 查询示例数据
String sql9 = "SELECT * FROM users";
PreparedStatement stmt9 = conn.prepareStatement(sql9);
ResultSet rs1 = stmt9.executeQuery();
while (rs1.next()) {
System.out.println("id: " + rs1.getInt("id") + ", username: " + rs1.getString("username") + ", password: " + rs1.getString("password"));
}
String sql10 = "SELECT * FROM prices";
PreparedStatement stmt10 = conn.prepareStatement(sql10);
ResultSet rs2 = stmt10.executeQuery();
while (rs2.next()) {
System.out.println("id: " + rs2.getInt("id") + ", name: " + rs2.getString("name") + ", price: " + rs2.getDouble("price"));
}
String sql11 = "SELECT * FROM orders";
PreparedStatement stmt11 = conn.prepareStatement(sql11);
ResultSet rs3 = stmt11.executeQuery();
while (rs3.next()) {
System.out.println("id: " + rs3.getInt("id") + ", user_id: " + rs3.getInt("user_id") + ", price_id: " + rs3.getInt("price_id") + ", order_time: " + rs3.getTimestamp("order_time"));
}
// 更新示例数据
String sql12 = "UPDATE users SET password = 'newpassword' WHERE id = 1";
PreparedStatement stmt12 = conn.prepareStatement(sql12);
stmt12.executeUpdate();
// 删除示例数据
String sql13 = "DELETE FROM orders WHERE id = 1";
PreparedStatement stmt13 = conn.prepareStatement(sql13);
stmt13.executeUpdate();
// 关闭连接
conn.close();
}
}
```
这段代码可以创建四个表,并且实现了增删改查以及一个触发器,当订单表插入数据时自动更新用户的余额信息。
希望这能对您有所帮助!
阅读全文