書(shū)籍:數(shù)據(jù)庫(kù)系統(tǒng)原理與設(shè)計(jì)(第3版)——萬(wàn)常選 廖國(guó)瓊等編著
達(dá)茂旗ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
任務(wù)驅(qū)動(dòng):由于找不到原始數(shù)據(jù)庫(kù),只有從零開(kāi)始創(chuàng)建數(shù)據(jù)庫(kù)、設(shè)計(jì)數(shù)據(jù)庫(kù)關(guān)系圖、輸入數(shù)據(jù),為后面深入研究數(shù)據(jù)庫(kù)做好數(shù)據(jù)準(zhǔn)備。
數(shù)據(jù)庫(kù)版本:SQL Server 2005
操作步驟:
1.創(chuàng)建數(shù)據(jù)庫(kù)源碼如下或是通過(guò)數(shù)據(jù)庫(kù)管理工具創(chuàng)建數(shù)據(jù)庫(kù)
USE [master]
GO
/****** 對(duì)象: Database [ScoreDB] 腳本日期: 11/28/2018 21:44:34 ******/
CREATE DATABASE [ScoreDB] ON PRIMARY
( NAME = N'ScoreDB', FILENAME = N'D:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\ScoreDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ScoreDB_log', FILENAME = N'D:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\ScoreDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
COLLATE Chinese_PRC_CI_AS
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'ScoreDB', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ScoreDB].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [ScoreDB] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [ScoreDB] SET ANSI_NULLS OFF
GO
ALTER DATABASE [ScoreDB] SET ANSI_PADDING OFF
GO
ALTER DATABASE [ScoreDB] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [ScoreDB] SET ARITHABORT OFF
GO
ALTER DATABASE [ScoreDB] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [ScoreDB] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [ScoreDB] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [ScoreDB] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [ScoreDB] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [ScoreDB] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [ScoreDB] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [ScoreDB] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [ScoreDB] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [ScoreDB] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [ScoreDB] SET ENABLE_BROKER
GO
ALTER DATABASE [ScoreDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [ScoreDB] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [ScoreDB] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [ScoreDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [ScoreDB] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [ScoreDB] SET READ_WRITE
GO
ALTER DATABASE [ScoreDB] SET RECOVERY FULL
GO
ALTER DATABASE [ScoreDB] SET MULTI_USER
GO
ALTER DATABASE [ScoreDB] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [ScoreDB] SET DB_CHAINING OFF
2.創(chuàng)建表源碼如下或是通過(guò)數(shù)據(jù)庫(kù)管理工具創(chuàng)建表
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Class]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Class](
[classNo] [char](6) NOT NULL,
[className] [varchar](30) NULL,
[institute] [varchar](30) NULL,
[grade] [smallint] NULL,
[classNum] [tinyint] NULL,
CONSTRAINT [PK_Class] PRIMARY KEY CLUSTERED
(
[classNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Course]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Course](
[courseNo] [char](3) NOT NULL,
[courseName] [varchar](30) NULL,
[creditHour] [numeric](18, 0) NULL,
[courseHour] [int] NULL,
[priorCourse] [char](3) NULL,
CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED
(
[courseNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Term]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Term](
[termNo] [char](3) NOT NULL,
[termName] [varchar](30) NULL,
[remarks] [varchar](50) NULL,
CONSTRAINT [PK_Term] PRIMARY KEY CLUSTERED
(
[termNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Score]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Score](
[studentNo] [char](7) NOT NULL,
[courseNo] [char](3) NOT NULL,
[termNo] [char](3) NOT NULL,
[score] [numeric](18, 0) NULL,
CONSTRAINT [PK_Score] PRIMARY KEY CLUSTERED
(
[studentNo] ASC,
[courseNo] ASC,
[termNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Student]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Student](
[studentNo] [char](7) NOT NULL,
[studentName] [varchar](20) NULL,
[sex] [char](2) NULL,
[birthday] [datetime] NULL,
[native] [varchar](20) NULL,
[nation] [varchar](30) NULL,
[classNo] [char](6) NOT NULL,
CONSTRAINT [PK_Student_1] PRIMARY KEY CLUSTERED
(
[studentNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Course_Course]') AND parent_object_id = OBJECT_ID(N'[dbo].[Course]'))
ALTER TABLE [dbo].[Course] WITH CHECK ADD CONSTRAINT [FK_Course_Course] FOREIGN KEY([priorCourse])
REFERENCES [dbo].[Course] ([courseNo])
GO
ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Course]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Score_Course]') AND parent_object_id = OBJECT_ID(N'[dbo].[Score]'))
ALTER TABLE [dbo].[Score] WITH CHECK ADD CONSTRAINT [FK_Score_Course] FOREIGN KEY([courseNo])
REFERENCES [dbo].[Course] ([courseNo])
GO
ALTER TABLE [dbo].[Score] CHECK CONSTRAINT [FK_Score_Course]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Score_Student]') AND parent_object_id = OBJECT_ID(N'[dbo].[Score]'))
ALTER TABLE [dbo].[Score] WITH CHECK ADD CONSTRAINT [FK_Score_Student] FOREIGN KEY([studentNo])
REFERENCES [dbo].[Student] ([studentNo])
GO
ALTER TABLE [dbo].[Score] CHECK CONSTRAINT [FK_Score_Student]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Score_Term]') AND parent_object_id = OBJECT_ID(N'[dbo].[Score]'))
ALTER TABLE [dbo].[Score] WITH CHECK ADD CONSTRAINT [FK_Score_Term] FOREIGN KEY([termNo])
REFERENCES [dbo].[Term] ([termNo])
GO
ALTER TABLE [dbo].[Score] CHECK CONSTRAINT [FK_Score_Term]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Student_Class]') AND parent_object_id = OBJECT_ID(N'[dbo].[Student]'))
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_Class] FOREIGN KEY([classNo])
REFERENCES [dbo].[Class] ([classNo])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Class]
3.設(shè)計(jì)數(shù)據(jù)庫(kù)關(guān)系圖
實(shí)戰(zhàn)經(jīng)驗(yàn):建立主鍵與外鍵關(guān)系時(shí),兩者數(shù)據(jù)類(lèi)型必須一致。
作者提供原始數(shù)據(jù),請(qǐng)點(diǎn)擊鏈接下載:http://down.51cto.com/data/2456174。