温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-04 21:53:28 来源:网络 阅读:612 作者:sky9890 栏目:关系型数据库

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

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

数据库版本:SQL Server 2005

操作步骤:

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

USE [master]

GO

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI