CREATE TABLE customer ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, gender ENUM('男', '女') NOT NULL, phone VARCHAR(20) NOT NULL, address VARCHAR(100) NOT NULL ); CREATE TABLE car_model( id INT PRIMARY KEY AUTO_INCREMENT, brand VARCHAR(50) NOT NULL, model VARCHAR(50) NOT NULL, price DECIMAL(10,2) NOT NULL ); CREATE TABLE car( model_id INT PRIMARY KEY AUTO_INCREMENT, color VARCHAR(50) NOT NULL, description VARCHAR(50) NOT NULL ); CREATE TABLE order1( id INT PRIMARY KEY AUTO_INCREMENT, customer_id INT NOT NULL, car_id INT NOT NULL, order_date DATETIME NOT NULL , price DECIMAL(10,2) NOT NULL, ); CREATE TABLE employee( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, gender ENUM('男', '女') NOT NULL, phone VARCHAR(20) NOT NULL, address VARCHAR(100) NOT NULL, salary DECIMAL(10,2) NOT NULL给出五个存储函数以及调用语句 );
时间: 2023-06-19 11:05:04 浏览: 165
存储函数1:
DELIMITER //
CREATE FUNCTION get_total_price(order_id INT)
RETURNS DECIMAL(10,2)
BEGIN
DECLARE total_price DECIMAL(10,2);
SELECT SUM(price) INTO total_price FROM order1 WHERE id = order_id;
RETURN total_price;
END//
DELIMITER ;
调用语句:SELECT get_total_price(1);
存储函数2:
DELIMITER //
CREATE FUNCTION get_customer_orders(customer_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE order_count INT;
SELECT COUNT(*) INTO order_count FROM order1 o JOIN customer c ON o.customer_id = c.id WHERE c.name = customer_name;
RETURN order_count;
END//
DELIMITER ;
调用语句:SELECT get_customer_orders('John Doe');
存储函数3:
DELIMITER //
CREATE FUNCTION get_employee_salary_increase(employee_id INT, percent DECIMAL(10,2))
RETURNS DECIMAL(10,2)
BEGIN
UPDATE employee SET salary = salary * (1 + percent/100) WHERE id = employee_id;
RETURN salary;
END//
DELIMITER ;
调用语句:SELECT get_employee_salary_increase(1, 10);
存储函数4:
DELIMITER //
CREATE FUNCTION get_model_count(brand_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE model_count INT;
SELECT COUNT(*) INTO model_count FROM car_model WHERE brand = brand_name;
RETURN model_count;
END//
DELIMITER ;
调用语句:SELECT get_model_count('Toyota');
存储函数5:
DELIMITER //
CREATE FUNCTION get_car_info(car_id INT)
RETURNS VARCHAR(100)
BEGIN
DECLARE car_info VARCHAR(100);
SELECT CONCAT(c.color, ' ', cm.brand, ' ', cm.model) INTO car_info FROM car c JOIN car_model cm ON c.model_id = cm.id WHERE c.id = car_id;
RETURN car_info;
END//
DELIMITER ;
调用语句:SELECT get_car_info(1);
阅读全文