文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

创建原始数据库——创建数据、创建表

2024-04-02 19:55

关注

书籍:数据库系统原理与设计(第3版)——万常选 廖国琼等编著

任务驱动:由于找不到原始数据库,只有从零开始创建数据库、设计数据库关系图、输入数据,为后面深入研究数据库做好数据准备。

数据库版本:SQL Server 2005

操作步骤:

1.创建数据库源码如下或是通过数据库管理工具创建数据库

USE [master]

GO

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.创建表源码如下或是通过数据库管理工具创建表

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.设计数据库关系图

创建原始数据库——创建数据、创建表

实战经验:建立主键与外键关系时,两者数据类型必须一致。

作者提供原始数据,请点击链接下载:http://down.51cto.com/data/2456174。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯