习题一学员成绩:
1.创建一个名为学员成绩的数据库, 主要数据文件初始大小为10MB,最大空间为30MB,文件增长幅度为5MB,日志文件初始大小为5MB,最大为20MB,文件增长幅度为3MB,请自行设定文件的保存路径。
2.创建学生信息表(Student)包含:学生编号、学生姓名、性别、年龄、地区、联系电话、入学日期。
3.创建课程信息表(Score)包含:课程编号、科目、教师编号
4.创建学生成绩表(chengji)包含:学生编号、课程编号、考试分数。
5.创建教师信息表(teacherInfo)包含:教师编号、教师姓名。
6.建立四个表间的主从表关系。
6.1 学生成绩表中能不能出现在学员信息表中不存在的学号?
6.2 学生信息表中学生编号发生改变时应如何处理成绩表中的学生编号?
6.3 可以任意删除学生信息表中的数据吗?
6.4 学生成绩表中的课程编号可以随意添加吗?
7. 对学生信息表添加检查约束,要求性别只能是男或者女,年龄必须在0—100之间。
8.对学生信息表的入学日期字段添加默认约束,使其默认值为当前日期。
9.教师信息表中教师编号(teacherId)使用自动增长序列,初始编号为1001.
答案:
6.1 不能
6.2 不能
6.3不能
6.4不能
参照完整性约束
限制(Restrict)。不允许进行修改或删除操作。若修改或删除主表的主键时,如果子表中存在子记录,系统将产生一个错误提示。这是缺省的参照完整性设置。
置空(Set Null)。如果外键列允许为空,若修改或删除主表的主键时,把子表中参照的外键列设置为空值(NULL)。
置为缺省(Set Default)。如果指定了缺省值,若修改或删除主表的主键时,把子表中参照的外键设置为缺省值(Default)。
级联(Cascade)。把主表中主键修改为一个新的值时,相应修改子表中外键的值;或者删除主表中主键的记录时,要相应删除子表中外键的记录。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 USE [master]
2 GO
3 /****** 对象: Database [学员成绩] 脚本日期: 05/16/2009 20:35:26 ******/
4 CREATE DATABASE [学员成绩] ON PRIMARY
5 ( NAME = N'学员成绩', FILENAME = N'D:\训练营\sql实例\学员成绩.mdf' , SIZE = 10MB , MAXSIZE = UNLIMITED, FILEGROWTH = 5MB )
6 LOG ON
7 ( NAME = N'学员成绩_log', FILENAME = N'D:\训练营\sql实例\学员成绩_log.ldf' , SIZE = 5MB , MAXSIZE = 20MB , FILEGROWTH = 5MB )
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CREATE TABLE [dbo].[student](
[number] [int] IDENTITY(1,1) NOT NULL,
[name] [nchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
[sex] [nvarchar](1) COLLATE Chinese_PRC_CI_AS NULL CONSTRAINT [DF_student_sex] DEFAULT ('男'),
[age] [smallint] NULL,
[datetime] [datetime] NULL CONSTRAINT [DF_student_datetime] DEFAULT (getdate()),
[diqu] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[lianxidianhua] [int] NULL,
CONSTRAINT [PK_student] PRIMARY KEY CLUSTERED
(
[number] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[student] WITH CHECK ADD CONSTRAINT [CK_student] CHECK (([age]>(0) AND [age]<=(100)))
GO
ALTER TABLE [dbo].[student] CHECK CONSTRAINT [CK_student]
GO
ALTER TABLE [dbo].[student] WITH CHECK ADD CONSTRAINT [CK_student_sex] CHECK (([sex]='男' OR [sex]='女'))
GO
ALTER TABLE [dbo].[student] CHECK CONSTRAINT [CK_student_sex]
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CREATE TABLE [dbo].[teacherinfo](
[teacherid] [int] IDENTITY(1001,1) NOT NULL,
[teachername] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
CONSTRAINT [PK_teacherinfo] PRIMARY KEY CLUSTERED
(
[teacherid] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CREATE TABLE [dbo].[score](
[scorenumber] [int] IDENTITY(1,1) NOT NULL,
[kemu] [varchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
[teacherid] [int] NOT NULL,
CONSTRAINT [PK_score] PRIMARY KEY CLUSTERED
(
[scorenumber] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CREATE TABLE [dbo].[chengji](
[number] [int] NOT NULL,
[scorenumber] [int] NOT NULL,
[score] [float] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[chengji] WITH CHECK ADD CONSTRAINT [FK_chengji_score] FOREIGN KEY([scorenumber])
REFERENCES [dbo].[score] ([scorenumber])
GO
ALTER TABLE [dbo].[chengji] CHECK CONSTRAINT [FK_chengji_score]
GO
ALTER TABLE [dbo].[chengji] WITH CHECK ADD CONSTRAINT [FK_chengji_student] FOREIGN KEY([number])
REFERENCES [dbo].[student] ([number])
GO
ALTER TABLE [dbo].[chengji] CHECK CONSTRAINT [FK_chengji_student]
关系图如下: