创建一个有四个表的电子邮件管理系统,把代码写出来,并写出主键,外键约束,每个表插入五条数据,并进行增删改查
时间: 2024-04-07 08:28:58 浏览: 137
好的,我可以为你提供一个简单的电子邮件管理系统的数据库设计,包含四个表:用户表、邮件表、附件表和收件箱表。
1. 用户表(User)
字段:UserID、UserName、Password、Email
主键:UserID
CREATE TABLE User (
UserID INT PRIMARY KEY,
UserName VARCHAR(50),
Password VARCHAR(50),
Email VARCHAR(50)
);
-- 插入5条数据
INSERT INTO User (UserID, UserName, Password, Email) VALUES
(1, '张三', '123456', 'zhangsan@example.com'),
(2, '李四', '654321', 'lisi@example.com'),
(3, '王五', 'qwerty', 'wangwu@example.com'),
(4, '赵六', 'asdfgh', 'zhaoliu@example.com'),
(5, '钱七', 'zxcvbn', 'qianqi@example.com');
2. 邮件表(Email)
字段:EmailID、Sender、Recipient、Subject、Content、SendTime
主键:EmailID
外键:Sender(来自用户表User的UserID)、Recipient(来自用户表User的UserID)
CREATE TABLE Email (
EmailID INT PRIMARY KEY,
Sender INT,
Recipient INT,
Subject VARCHAR(50),
Content TEXT,
SendTime DATETIME,
FOREIGN KEY (Sender) REFERENCES User(UserID),
FOREIGN KEY (Recipient) REFERENCES User(UserID)
);
-- 插入5条数据
INSERT INTO Email (EmailID, Sender, Recipient, Subject, Content, SendTime) VALUES
(1, 1, 2, '关于明天的会议', '明天下午2点在会议室开会,请准时参加。', '2022-01-01 10:00:00'),
(2, 2, 1, 'RE: 关于明天的会议', '明白了,我会准时到场。', '2022-01-01 10:30:00'),
(3, 3, 1, '新年快乐', '祝您新年快乐!', '2022-01-01 12:00:00'),
(4, 1, 4, '请假申请', '因为身体原因,我需要请假两天,请领导批准。', '2022-01-02 09:00:00'),
(5, 5, 1, '工作报告', '这是本月的工作报告,请查收。', '2022-01-02 10:00:00');
3. 附件表(Attachment)
字段:AttachmentID、EmailID、FileName、FileSize
主键:AttachmentID
外键:EmailID(来自邮件表Email的EmailID)
CREATE TABLE Attachment (
AttachmentID INT PRIMARY KEY,
EmailID INT,
FileName VARCHAR(50),
FileSize INT,
FOREIGN KEY (EmailID) REFERENCES Email(EmailID)
);
-- 插入5条数据
INSERT INTO Attachment (AttachmentID, EmailID, FileName, FileSize) VALUES
(1, 1, 'agenda.docx', 1024),
(2, 2, 'minutes.docx', 2048),
(3, 5, 'report.pdf', 4096),
(4, 5, 'chart.xlsx', 3072),
(5, 5, 'photo.jpg', 512);
4. 收件箱表(Inbox)
字段:InboxID、UserID、EmailID、IsRead
主键:InboxID
外键:UserID(来自用户表User的UserID)、EmailID(来自邮件表Email的EmailID)
CREATE TABLE Inbox (
InboxID INT PRIMARY KEY,
UserID INT,
EmailID INT,
IsRead BIT,
FOREIGN KEY (UserID) REFERENCES User(UserID),
FOREIGN KEY (EmailID) REFERENCES Email(EmailID)
);
-- 插入5条数据
INSERT INTO Inbox (InboxID, UserID, EmailID, IsRead) VALUES
(1, 2, 1, 0),
(2, 1, 2, 0),
(3, 1, 3, 0),
(4, 4, 4, 0),
(5, 1, 5, 0);
以上是电子邮件管理系统的数据库设计,包含四个表,分别是用户表、邮件表、附件表和收件箱表。每个表都有主键和外键约束。下面是增删改查的代码示例:
-- 查询用户表中所有的数据
SELECT * FROM User;
-- 查询邮件表中所有的数据
SELECT * FROM Email;
-- 查询附件表中所有的数据
SELECT * FROM Attachment;
-- 查询收件箱表中所有的数据
SELECT * FROM Inbox;
-- 新增一条用户数据
INSERT INTO User (UserID, UserName, Password, Email) VALUES (6, '孙八', 'qazwsx', 'sunba@example.com');
-- 修改一条用户数据
UPDATE User SET Password = 'yuiop' WHERE UserID = 6;
-- 删除一条用户数据
DELETE FROM User WHERE UserID = 6;
-- 新增一封邮件
INSERT INTO Email (EmailID, Sender, Recipient, Subject, Content, SendTime) VALUES (6, 4, 1, '请假批准', '同意您的请假申请。', '2022-01-03 14:00:00');
-- 修改一封邮件
UPDATE Email SET Subject = '请假不批准' WHERE EmailID = 4;
-- 删除一封邮件
DELETE FROM Email WHERE EmailID = 4;
-- 新增一条附件记录
INSERT INTO Attachment (AttachmentID, EmailID, FileName, FileSize) VALUES (6, 1, 'agenda.pdf', 2048);
-- 修改一条附件记录
UPDATE Attachment SET FileName = 'agenda.docx' WHERE AttachmentID = 1;
-- 删除一条附件记录
DELETE FROM Attachment WHERE AttachmentID = 1;
-- 标记一封邮件为已读
UPDATE Inbox SET IsRead = 1 WHERE EmailID = 1 AND UserID = 2;
-- 查询用户收到的所有未读邮件
SELECT Email.* FROM Email JOIN Inbox ON Email.EmailID = Inbox.EmailID WHERE Inbox.UserID = 1 AND Inbox.IsRead = 0;
-- 查询用户发出的所有邮件
SELECT * FROM Email WHERE Sender = 1;
希望以上示例可以帮助你更好地理解数据库课程设计的相关知识。
阅读全文