艺人管理系统建立三个表三个表之间有联系,创建一个触发器和两个带参存储过程实现功能和页面
时间: 2024-02-13 09:01:30 浏览: 54
创建触发器和存储过程
好的,我来为您解答。
三个表可以分别命名为:Artist(艺人表)、Manager(经纪人表)、Contract(合约表),它们之间的联系可以用外键来实现。具体地,合约表 Contract 中应该包含两个外键:一个指向艺人表 Artist 的主键,一个指向经纪人表 Manager 的主键。
为了实现艺人和经纪人的管理,我们可以创建一个触发器和两个带参存储过程。触发器的作用是在插入或更新合约表 Contract 的时候,自动更新艺人表 Artist 和经纪人表 Manager 中的相关信息。而存储过程则可以用来实现一些常用功能,比如查询艺人和经纪人的信息、添加或删除艺人和经纪人等等。
下面是一个简单的示例:
首先,我们创建三个表:
```sql
CREATE TABLE Artist (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
birthday DATE,
phone VARCHAR(20),
email VARCHAR(50)
);
CREATE TABLE Manager (
id INT PRIMARY KEY,
name VARCHAR(50),
gender VARCHAR(10),
phone VARCHAR(20),
email VARCHAR(50)
);
CREATE TABLE Contract (
id INT PRIMARY KEY,
artist_id INT,
manager_id INT,
start_date DATE,
end_date DATE,
FOREIGN KEY (artist_id) REFERENCES Artist(id),
FOREIGN KEY (manager_id) REFERENCES Manager(id)
);
```
接下来,我们创建一个触发器,用来在插入或更新合约表 Contract 的时候,自动更新艺人表 Artist 和经纪人表 Manager 中的相关信息:
```sql
CREATE TRIGGER update_artist_manager AFTER INSERT, UPDATE ON Contract
FOR EACH ROW
BEGIN
IF NEW.artist_id IS NOT NULL THEN
UPDATE Artist SET manager_id = NEW.manager_id WHERE id = NEW.artist_id;
END IF;
IF NEW.manager_id IS NOT NULL THEN
UPDATE Manager SET artist_id = NEW.artist_id WHERE id = NEW.manager_id;
END IF;
END;
```
最后,我们创建两个带参存储过程,一个用来添加或修改艺人信息,一个用来添加或修改经纪人信息:
```sql
CREATE PROCEDURE add_or_update_artist (
IN artist_id INT,
IN artist_name VARCHAR(50),
IN artist_gender VARCHAR(10),
IN artist_birthday DATE,
IN artist_phone VARCHAR(20),
IN artist_email VARCHAR(50),
IN manager_id INT
)
BEGIN
IF artist_id IS NOT NULL THEN
UPDATE Artist SET name = artist_name, gender = artist_gender, birthday = artist_birthday, phone = artist_phone, email = artist_email, manager_id = manager_id WHERE id = artist_id;
ELSE
INSERT INTO Artist (name, gender, birthday, phone, email, manager_id) VALUES (artist_name, artist_gender, artist_birthday, artist_phone, artist_email, manager_id);
END IF;
END;
CREATE PROCEDURE add_or_update_manager (
IN manager_id INT,
IN manager_name VARCHAR(50),
IN manager_gender VARCHAR(10),
IN manager_phone VARCHAR(20),
IN manager_email VARCHAR(50),
IN artist_id INT
)
BEGIN
IF manager_id IS NOT NULL THEN
UPDATE Manager SET name = manager_name, gender = manager_gender, phone = manager_phone, email = manager_email, artist_id = artist_id WHERE id = manager_id;
ELSE
INSERT INTO Manager (name, gender, phone, email, artist_id) VALUES (manager_name, manager_gender, manager_phone, manager_email, artist_id);
END IF;
END;
```
至此,我们已经完成了艺人管理系统的建立。您可以根据实际需要,进一步完善系统的功能和页面。
阅读全文