1.实体:客户,属性:姓名,性别,身份证号,信用度,账户; 2.实体:账户,属性:账号,账户类型,账户余额,利率,取款限额; 3.实体:存取款信息,属性:账号,操作类型,存取金额,流水号,操作时间; 4.实体:银行员工,属性:工号,考姓名,岗位,联系电话; 5.实体:贷款信息,属性:账号,贷款金额,贷款利率,贷款日期; 将以上实体和属性,用sql需要创建两个视图,两个索引,创建一个触发器,创建一个存储过程
时间: 2024-04-28 10:25:39 浏览: 80
根据表名创建实体类的字段和属性.(单个表生成)存储过程快速生成
创建实体和属性的SQL语句如下:
```SQL
-- 创建客户实体
CREATE TABLE customer (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
id_card VARCHAR(50),
credit_score INT,
account_id INT
);
-- 创建账户实体
CREATE TABLE account (
id INT PRIMARY KEY,
account_no VARCHAR(50),
account_type VARCHAR(50),
balance DECIMAL(10, 2),
interest_rate DECIMAL(4, 2),
withdraw_limit DECIMAL(10, 2)
);
-- 创建存取款信息实体
CREATE TABLE transaction (
id INT PRIMARY KEY,
account_id INT,
transaction_type VARCHAR(10),
amount DECIMAL(10, 2),
serial_no VARCHAR(50),
transaction_time DATETIME
);
-- 创建银行员工实体
CREATE TABLE employee (
id INT PRIMARY KEY,
name VARCHAR(50),
position VARCHAR(50),
phone VARCHAR(50)
);
-- 创建贷款信息实体
CREATE TABLE loan (
id INT PRIMARY KEY,
account_id INT,
loan_amount DECIMAL(10, 2),
loan_rate DECIMAL(4, 2),
loan_date DATETIME
);
```
创建视图的SQL语句如下:
```SQL
-- 创建客户账户视图
CREATE VIEW customer_account AS
SELECT c.name, c.gender, c.id_card, a.account_no, a.account_type, a.balance
FROM customer c
JOIN account a ON c.account_id = a.id;
-- 创建账户交易视图
CREATE VIEW account_transaction AS
SELECT a.account_no, t.transaction_type, t.amount, t.serial_no, t.transaction_time
FROM account a
JOIN transaction t ON a.id = t.account_id;
```
创建索引的SQL语句如下:
```SQL
-- 为客户表的姓名字段创建索引
CREATE INDEX idx_customer_name ON customer(name);
-- 为账户表的账号字段创建唯一索引
CREATE UNIQUE INDEX idx_account_no ON account(account_no);
```
创建触发器的SQL语句如下:
```SQL
-- 在存取款信息表上创建触发器,当插入新记录时,更新对应账户表的余额字段
CREATE TRIGGER tr_update_balance
AFTER INSERT ON transaction
FOR EACH ROW
BEGIN
IF NEW.transaction_type = 'deposit' THEN
UPDATE account SET balance = balance + NEW.amount WHERE id = NEW.account_id;
ELSEIF NEW.transaction_type = 'withdraw' THEN
UPDATE account SET balance = balance - NEW.amount WHERE id = NEW.account_id;
END IF;
END;
```
创建存储过程的SQL语句如下:
```SQL
-- 创建存储过程,用于查询客户的账户余额
CREATE PROCEDURE get_customer_balance(IN customer_name VARCHAR(50))
BEGIN
DECLARE account_balance DECIMAL(10, 2);
SELECT a.balance INTO account_balance
FROM customer c
JOIN account a ON c.account_id = a.id
WHERE c.name = customer_name;
SELECT CONCAT(customer_name, '的账户余额为:', account_balance) AS result;
END;
```
阅读全文