微软官网 https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads
如果要通过局域网访问数据库,在安装过程中,需要勾选多个扩展项(机器学习的可以不装)
如果不使用局域网访问数据库,安装默认功能即可
表可以通过脚本或者管理工具创建,不过一般用管理工具的UI就可以了
就像是每个人都有一个唯一的身份证号一样,每一行数据也要有一个唯一的编码,不然怎么从数据库中快速找到它。这个唯一的编码也就是主键,通常是int类型(int类型的查找速度比较快),数据库中每个表都必须要有主键。
主键就是一个唯一ID,如果每次输入数据都要自己输入这个ID,还要保证这个唯一,想想都麻烦。可以让数据库自己维护,添加数据时这个ID自动变化。
数据库在设计过程中为了减少数据冗余,设置了三大范式,三大范式个人理解其实就是将数据列和数据表设计为不可再划分状态,也就是最小状态,这样需要什么数据直接拿需要的表的需要的列进行组合即可。
假设要设计一个学校的数据库,记录学生信息和老师信息,学生和老师都有名字,性别,家庭住址,联系方式等信息,在设计数据库时,设计了如下五张表
后续有可能会用的城市信息,例如要统计每个城市学生人数等和城市相关的信息时,如果将地址信息做出一列数据,处理就比较麻烦
将可以重复利用的城市信息保存在城市信息表中,地址信息需要用的城市时,只要填城市的编号即可(查询时可以通过城市编号找到对应城市),节省了存储空间。
为什么不将地址继续划分?
当然可以继续划分,不过做的是学校师生信息数据库,大学中人数有限,来自每个市的人数也不会不多(重复利用率不高,不会节省太多存储控件),所以大概率不会用的市以下的地址信息,因此设计地址数据列时只划分了省、市、详细地址,如果要做一个居民信息数据库,那就需要更进一步划分了。当然了年纪表其实也可以再详细划分,专业-年纪-班级,这里为了简单就没有做划分处理,其实最终是否划分还是看设计需求,不过一般都会设计成最简的列,方便后续增添需求
什么是数据表不可再划分?
存在直接关系的放在一张表中,例如在学生信息表中,城市和省份是直接关联的,学生地址存放城市信息即可,没必要在多一个省份信息的列。学生和老师之间是通过班级联系起来的,因此学生信息中不需要包含老师以及老师的信息,只需要包含班级信息即可,利用班级表可以找到对应的老师。
外键是什么?
为了节省存储空间,多张表格相互关联时会将一张表格的主键(下图中城市信息表的id列)填到另一张表格的列(学生信息表的cityID)中,这样每一条学生信息就可以不存储城市名字(例如东城区),只填一个数字即可。但是如果向学生信息表中的cityID添加一个不存在的城市id,当查询时就找不到对应的城市,为了避免学生信息表中存在不存在的城市id,在向学生信息表中添加cityID时,对cityID进行检查,如果在城市信息表的id列中存在,才可以添加到学生信息表中,为了方便操作,数据库定义了一个叫做外键的东西(外部表的主键)来帮我们自动做这个检查。
如何建立外键
设置外键
设置完外键后,再向学生信息表中的cityID列添加信息时就会自动检索cityTable中的id列,如果存在才可以添加成功。同样在删除数据时,比如要删除cityTable中的一个城市信息,则需要保证外键(学生信息表中cityID没有使用对应的城市),否则不能删除,需要先删除学生信息表使用该城市的学生信息,然后才能删除cityTable中的城市信息。其实还是为了确保学生信息表中cityID列中所有城市都可以在cityTable中查询到。
五张表的创建脚本
USE [master]
GO
/****** Object: Database [TestDB] Script Date: 2021/7/23 18:29:26 ******/
CREATE DATABASE [TestDB]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'TestDB', FILENAME = N'D:\SQL2019\MSSQL15.MSSQLSERVER\MSSQL\DATA\TestDB.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'TestDB_log', FILENAME = N'D:\SQL2019\MSSQL15.MSSQLSERVER\MSSQL\DATA\TestDB_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT
GO
ALTER DATABASE [TestDB] SET COMPATIBILITY_LEVEL = 150
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [TestDB].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [TestDB] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [TestDB] SET ANSI_NULLS OFF
GO
ALTER DATABASE [TestDB] SET ANSI_PADDING OFF
GO
ALTER DATABASE [TestDB] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [TestDB] SET ARITHABORT OFF
GO
ALTER DATABASE [TestDB] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [TestDB] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [TestDB] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [TestDB] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [TestDB] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [TestDB] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [TestDB] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [TestDB] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [TestDB] SET DISABLE_BROKER
GO
ALTER DATABASE [TestDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [TestDB] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [TestDB] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [TestDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [TestDB] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [TestDB] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [TestDB] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [TestDB] SET RECOVERY FULL
GO
ALTER DATABASE [TestDB] SET MULTI_USER
GO
ALTER DATABASE [TestDB] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [TestDB] SET DB_CHAINING OFF
GO
ALTER DATABASE [TestDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [TestDB] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [TestDB] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [TestDB] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'TestDB', N'ON'
GO
ALTER DATABASE [TestDB] SET QUERY_STORE = OFF
GO
USE [TestDB]
GO
/****** Object: Table [dbo].[cityTable] Script Date: 2021/7/23 18:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[cityTable](
[id] [int] IDENTITY(1,1) NOT NULL,
[provinceID] [int] NOT NULL,
[cityName] [nvarchar](20) NOT NULL,
CONSTRAINT [PK_cityTable] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[genderTable] Script Date: 2021/7/23 18:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[genderTable](
[id] [bit] NOT NULL,
[name] [nchar](1) NOT NULL,
CONSTRAINT [PK_genderTable] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[gradeTable] Script Date: 2021/7/23 18:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[gradeTable](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](20) NOT NULL,
[teacherID] [int] NOT NULL,
CONSTRAINT [PK_gradeTable] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[provinceTable] Script Date: 2021/7/23 18:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[provinceTable](
[id] [int] IDENTITY(1,1) NOT NULL,
[province] [nvarchar](10) NOT NULL,
CONSTRAINT [PK_provinceTable] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[studentTable] Script Date: 2021/7/23 18:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[studentTable](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](10) NOT NULL,
[gender] [bit] NOT NULL,
[cityID] [int] NOT NULL,
[address] [nvarchar](50) NULL,
[gradeID] [int] NOT NULL,
CONSTRAINT [PK_studentTable] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[teacherTable] Script Date: 2021/7/23 18:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[teacherTable](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](10) NOT NULL,
[gender] [bit] NOT NULL,
[cityID] [int] NOT NULL,
[address] [nvarchar](50) NULL,
[phone] [char](11) NOT NULL,
CONSTRAINT [PK_teacher] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[cityTable] ON
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (1, 1, N'东城区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (2, 1, N'西城区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (5, 1, N'崇文区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (7, 1, N'宣武区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (8, 1, N'朝阳区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (9, 1, N'丰台区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (10, 1, N'石景山区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (11, 1, N'海淀区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (12, 1, N'门头沟区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (13, 1, N'房山区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (14, 1, N'通州区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (15, 1, N'顺义区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (16, 1, N'昌平区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (17, 1, N'大兴区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (18, 1, N'怀柔区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (19, 1, N'平谷区')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (20, 1, N'密云县')
GO
INSERT [dbo].[cityTable] ([id], [provinceID], [cityName]) VALUES (21, 1, N'延庆县')
GO
SET IDENTITY_INSERT [dbo].[cityTable] OFF
GO
INSERT [dbo].[genderTable] ([id], [name]) VALUES (0, N'女')
GO
INSERT [dbo].[genderTable] ([id], [name]) VALUES (1, N'男')
GO
SET IDENTITY_INSERT [dbo].[gradeTable] ON
GO
INSERT [dbo].[gradeTable] ([id], [name], [teacherID]) VALUES (5, N'一年级一班', 3)
GO
INSERT [dbo].[gradeTable] ([id], [name], [teacherID]) VALUES (7, N'一年级二班', 4)
GO
INSERT [dbo].[gradeTable] ([id], [name], [teacherID]) VALUES (8, N'一年级三班', 5)
GO
SET IDENTITY_INSERT [dbo].[gradeTable] OFF
GO
SET IDENTITY_INSERT [dbo].[provinceTable] ON
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (1, N'北京')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (2, N'天津')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (3, N'河北')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (4, N'山西')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (5, N'内蒙古')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (6, N'辽宁')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (7, N'吉林')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (8, N'黑龙江')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (9, N'上海')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (10, N'江苏')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (11, N'浙江')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (12, N'安徽')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (13, N'福建')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (14, N'江西')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (15, N'山东')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (16, N'河南')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (17, N'湖北')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (18, N'湖南')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (19, N'广东')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (20, N'广西')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (21, N'海南')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (22, N'重庆')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (23, N'四川')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (24, N'贵州')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (25, N'云南')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (26, N'西藏')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (27, N'陕西')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (28, N'甘肃')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (29, N'青海')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (30, N'宁夏')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (31, N'新疆')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (32, N'台湾')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (33, N'香港')
GO
INSERT [dbo].[provinceTable] ([id], [province]) VALUES (34, N'澳门')
GO
SET IDENTITY_INSERT [dbo].[provinceTable] OFF
GO
SET IDENTITY_INSERT [dbo].[studentTable] ON
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (2, N'张三', 1, 1, N'xx街道', 5)
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (3, N'李四', 0, 7, NULL, 5)
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (4, N'王五', 1, 7, NULL, 5)
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (5, N'赵六', 0, 8, NULL, 7)
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (6, N'田七', 1, 7, NULL, 7)
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (7, N'刘八', 0, 9, NULL, 5)
GO
INSERT [dbo].[studentTable] ([id], [name], [gender], [cityID], [address], [gradeID]) VALUES (8, N'钱九', 0, 7, NULL, 7)
GO
SET IDENTITY_INSERT [dbo].[studentTable] OFF
GO
SET IDENTITY_INSERT [dbo].[teacherTable] ON
GO
INSERT [dbo].[teacherTable] ([id], [name], [gender], [cityID], [address], [phone]) VALUES (3, N'老师1', 0, 1, N'xx街道', N'11111111111')
GO
INSERT [dbo].[teacherTable] ([id], [name], [gender], [cityID], [address], [phone]) VALUES (4, N'老师2', 0, 1, N'yy街道', N'22222222222')
GO
INSERT [dbo].[teacherTable] ([id], [name], [gender], [cityID], [address], [phone]) VALUES (5, N'老师3', 1, 1, N'xx街道', N'11111111112')
GO
SET IDENTITY_INSERT [dbo].[teacherTable] OFF
GO
ALTER TABLE [dbo].[cityTable] WITH CHECK ADD CONSTRAINT [FK_cityTable_provinceTable] FOREIGN KEY([provinceID])
REFERENCES [dbo].[provinceTable] ([id])
GO
ALTER TABLE [dbo].[cityTable] CHECK CONSTRAINT [FK_cityTable_provinceTable]
GO
ALTER TABLE [dbo].[gradeTable] WITH CHECK ADD CONSTRAINT [FK_gradeTable_teacherTable] FOREIGN KEY([teacherID])
REFERENCES [dbo].[teacherTable] ([id])
GO
ALTER TABLE [dbo].[gradeTable] CHECK CONSTRAINT [FK_gradeTable_teacherTable]
GO
ALTER TABLE [dbo].[studentTable] WITH CHECK ADD CONSTRAINT [FK_studentTable_cityTable] FOREIGN KEY([cityID])
REFERENCES [dbo].[cityTable] ([id])
GO
ALTER TABLE [dbo].[studentTable] CHECK CONSTRAINT [FK_studentTable_cityTable]
GO
ALTER TABLE [dbo].[studentTable] WITH CHECK ADD CONSTRAINT [FK_studentTable_genderTable] FOREIGN KEY([gender])
REFERENCES [dbo].[genderTable] ([id])
GO
ALTER TABLE [dbo].[studentTable] CHECK CONSTRAINT [FK_studentTable_genderTable]
GO
ALTER TABLE [dbo].[teacherTable] WITH CHECK ADD CONSTRAINT [FK_teacherTable_cityTable] FOREIGN KEY([cityID])
REFERENCES [dbo].[cityTable] ([id])
GO
ALTER TABLE [dbo].[teacherTable] CHECK CONSTRAINT [FK_teacherTable_cityTable]
GO
ALTER TABLE [dbo].[teacherTable] WITH CHECK ADD CONSTRAINT [FK_teacherTable_genderTable] FOREIGN KEY([gender])
REFERENCES [dbo].[genderTable] ([id])
GO
ALTER TABLE [dbo].[teacherTable] CHECK CONSTRAINT [FK_teacherTable_genderTable]
GO
USE [master]
GO
ALTER DATABASE [TestDB] SET READ_WRITE
GO