帮我用sql sever写一个运动会成绩管理系统,功能包括是否破学校记录并统计
时间: 2023-08-04 15:32:25 浏览: 107
成绩管理系统 完整版 可直接交的
好的,以下是一个更完整的运动会成绩管理系统的SQL Server数据库设计,包括判断是否破学校记录和统计的功能:
1. 表:比赛项目表
```
CREATE TABLE [dbo].[Event]
(
[EventID] INT PRIMARY KEY NOT NULL IDENTITY(1,1),
[EventName] VARCHAR(50) NOT NULL,
[EventDate] DATE NOT NULL,
[EventTime] TIME NOT NULL,
[Record] VARCHAR(50) NOT NULL
)
```
2. 表:运动员表
```
CREATE TABLE [dbo].[Athlete]
(
[AthleteID] INT PRIMARY KEY NOT NULL IDENTITY(1,1),
[AthleteName] VARCHAR(50) NOT NULL,
[Gender] VARCHAR(10) NOT NULL,
[Grade] INT NOT NULL,
[Class] VARCHAR(50) NOT NULL
)
```
3. 表:比赛成绩表
```
CREATE TABLE [dbo].[Result]
(
[ResultID] INT PRIMARY KEY NOT NULL IDENTITY(1,1),
[EventID] INT NOT NULL,
[AthleteID] INT NOT NULL,
[Result] VARCHAR(50) NOT NULL,
[Rank] INT NOT NULL,
[IsRecord] BIT NOT NULL,
CONSTRAINT [FK_EventID] FOREIGN KEY([EventID]) REFERENCES [dbo].[Event]([EventID]),
CONSTRAINT [FK_AthleteID] FOREIGN KEY([AthleteID]) REFERENCES [dbo].[Athlete]([AthleteID])
)
```
4. 存储过程:插入比赛成绩
```
CREATE PROCEDURE [dbo].[InsertResult]
@EventID INT,
@AthleteID INT,
@Result VARCHAR(50),
@Rank INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @IsRecord BIT = 0
DECLARE @Record VARCHAR(50) = (SELECT [Record] FROM [dbo].[Event] WHERE [EventID] = @EventID)
-- 判断是否破学校记录
IF @Result > @Record
SET @IsRecord = 1
-- 插入成绩
INSERT INTO [dbo].[Result] ([EventID], [AthleteID], [Result], [Rank], [IsRecord])
VALUES (@EventID, @AthleteID, @Result, @Rank, @IsRecord)
END
```
5. 存储过程:查询比赛成绩和是否破记录
```
CREATE PROCEDURE [dbo].[GetResultInfo]
@EventID INT = NULL,
@AthleteName VARCHAR(50) = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT [ResultID], [Event].[EventName], [Athlete].[AthleteName], [Result], [Rank], [IsRecord], [Record]
FROM [dbo].[Result]
INNER JOIN [dbo].[Event] ON [Result].[EventID] = [Event].[EventID]
INNER JOIN [dbo].[Athlete] ON [Result].[AthleteID] = [Athlete].[AthleteID]
WHERE [Result].[EventID] = @EventID OR [Athlete].[AthleteName] = @AthleteName OR (@EventID IS NULL AND @AthleteName IS NULL)
END
```
6. 存储过程:统计比赛成绩和记录信息
```
CREATE PROCEDURE [dbo].[GetResultStats]
@EventID INT
AS
BEGIN
SET NOCOUNT ON;
SELECT COUNT(*) AS [TotalCount], AVG([Rank]) AS [AverageRank], SUM(CASE WHEN [IsRecord] = 1 THEN 1 ELSE 0 END) AS [RecordCount]
FROM [dbo].[Result]
WHERE [EventID] = @EventID
END
```
这些SQL语句和存储过程可以让你实现一个更完整的运动会成绩管理系统,其中包括比赛项目信息的录入、运动员信息的录入、比赛成绩的录入和查询、比赛成绩的统计和判断是否破学校记录等功能。具体的实现方式可能需要根据你的实际需求做出一些调整。
阅读全文