我正在尝试为学生标记建立一个数据库(访问)。每个学生每门课程都有很多课程和不同的分数。 例 约翰:
艺术 Q1 = 75,Q2 = 85,Q3 = 80
等等 我建立了3个关系表,如下所示 学生(ID名级) 课程(courseid-coursename) 标记(ID-courseid-Q1-Q2-Q3)
我的工作是否正确?因为我会将它升级到SQL,所以我需要知道报表和表单的表和关系是否相同?
答案 0 :(得分:0)
好的,但你应该重命名一些字段:课程(id,name),标记(stu_id,course_id,Q1,Q2,Q3)
SQL(我使用PostgreSQL,但我认为其他DBMS类似)
CREATE TABLE students
(
id integer NOT NULL,
fname varchar(20),
lname varchar(20),
grade integer,
PRIMARY KEY (id)
);
CREATE TABLE courses
(
id integer NOT NULL,
name varchar(20),
PRIMARY KEY (id)
);
CREATE TABLE marks
(
stu_id integer REFERENCES students(id),
course_id integer REFERENCES courses(id),
Q1 double,
Q2 double,
Q3 double,
PRIMARY KEY (stu_id, course_id)
);
或强>
CREATE TABLE marks
(
id integer NOT NULL,
stu_id integer REFERENCES students(id),
course_id integer REFERENCES courses(id),
Q1 double,
Q2 double,
Q3 double,
PRIMARY KEY (id)
);
希望你的帮助
答案 1 :(得分:0)
您应该在标记表中使用复合键。这不会限制你只有3个问题。
用于创建表的Sql Query女士看起来像 -
/****** Object: Table [dbo].[students] Script Date: 11/11/2014 16:20:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[students](
[id] [int] NOT NULL,
[fname] [varchar](20) NULL,
[lname] [varchar](20) NULL,
[grade] [int] NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[courses] Script Date: 11/11/2014 16:19:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[courses](
[id] [int] NOT NULL,
[name] [varchar](20) NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[marks] Script Date: 11/11/2014 16:19:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[marks](
[stu_id] [int] NOT NULL,
[course_id] [int] NOT NULL,
[q_id] [int] NOT NULL,
[mark] [numeric](18, 0) NOT NULL,
CONSTRAINT [PK_marks] PRIMARY KEY CLUSTERED
(
[stu_id] ASC,
[course_id] ASC,
[q_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[marks] WITH CHECK ADD CONSTRAINT [FK_marks_courses] FOREIGN KEY([course_id])
REFERENCES [dbo].[courses] ([id])
GO
ALTER TABLE [dbo].[marks] CHECK CONSTRAINT [FK_marks_courses]
GO
ALTER TABLE [dbo].[marks] WITH CHECK ADD CONSTRAINT [FK_marks_students] FOREIGN KEY([stu_id])
REFERENCES [dbo].[students] ([id])
GO
ALTER TABLE [dbo].[marks] CHECK CONSTRAINT [FK_marks_students]
GO