数据库
首页 > 数据库> > SQLServer基础开发入门

SQLServer基础开发入门

作者:互联网

创建数据库和数据表

数据库的创建

数据库分类

通过查看对象资源管理器来区分数据库类型

用户数据库文件组成

数据库物理文件的组成:数据库文件+日志文件

一个数据库必须且只能包含一个mdf,可以有多个ndf和ldf(至少一个)

创建数据库

use master
go
create database StudentManageDB
on primary
(
	-- 数据库文件的逻辑名
	name='StudentManageDB_data',
	-- 数据库物理文件名(绝对路径)
	filename='D:\SQLserverDB\StudentManageDB_data.mdf',
	-- 数据库文件初始大小
	size=10MB,
	-- 数据文件增常量
	filegrowth=1MB
)
-- 创建日志文件
log on
(
	name='StudentManageDB_log',
	filename='D:\SQLserverDB\StudentManageDB_log.ldf',
	size=2MB,
	filegrowth=2MB
)
go

文件组类似于文件夹,主要用于管理磁盘文件,文件组分为主文件组和次文件组,日志文件不属于任何文件组

use master
go
create database StudentManageDB
on primary
(
	-- 数据库文件的逻辑名
	name='StudentManageDB_data',
	-- 数据库物理文件名(绝对路径)
	filename='D:\SQLserverDB\StudentManageDB_data.mdf',
	-- 数据库文件初始大小
	size=10MB,
	-- 数据文件增常量
	filegrowth=1MB
),
(
	name='StudentManageDB_data1',
	-- 创建次要数据文件,不能和主数据文件同名
	filename='D:\SQLserverDB\StudentManageDB_data1.ndf',
	size=10MB,
	filegrowth=1MB
)
-- 创建日志文件
log on
(
	name='StudentManageDB_log',
	filename='D:\SQLserverDB\StudentManageDB_log.ldf',
	size=2MB,
	filegrowth=2MB
),
(
	name='StudentManageDB_log1',
	filename='D:\SQLserverDB\StudentManageDB_log1.ldf',
	size=2MB,
	filegrowth=2MB
)
go

删除数据库

-- 判断当前数据是否存在
if exists (select * from sysdatabases where name='StudentManageDB')
drop database StudentManageDB		-- 删除数据库
go 

drop删除后数据库将不可恢复,谨慎使用

分离与附加数据库

--分离数据库
exec sp_detach_db @dbname=StudentManageDB
--附加数据库方法1
exec sp_attach_db @dbname=StudentManageDB,
@filename1='D:\SQLserverDB\StudentManageDB_data.mdf',
@filename2='D:\SQLserverDB\StudentManageDB_log.ldf'
--附加数据库方法2
exec sp_attach_db StudentManageDB,
'D:\SQLserverDB\StudentManageDB_data.mdf',
'D:\SQLserverDB\StudentManageDB_log.ldf'

数据表的创建

SQL Server数据类型

创建数据表

标识符的特殊说明

use StudentManageDB
go
if exists(select * from sysobjects where name='Students')
drop table Students
go

create table Students
(
	StudentID int identity(100000,1), --学号
	StudentName varchar(20) not null, --姓名
	Gender char(2) not null, --性别
	Birthday datetime not null, --出生日期
	StudentIdNo numeric(18,0) not null, --身份证号
	Age int not null, --年龄
	PhoneNumber varchar(50), --手机号
	StudentAddress varchar(500), --地址
	ClassId int not null --班级外键
)
go

-- 创建班级表
if exists(select * from sysobjects where name='StudentClass')
drop table StudentClass
go

create table StudentClass
(
	ClassId int primary key, --班级编号
	ClassName varchar(20) not null
)
go

-- 创建成绩表
if exists(select * from sysobjects where name='ScoreList')
drop table ScoreList
go

create table ScoreList
(
	Id int identity(1,1) primary key,
	StudentID int not null, --学号外键
	CSharp int null,
	SQLServer int null,
	UpdateTime datetime not null --更新时间
)
go

-- 创建管理员表
if exists(select * from sysobjects where name='Admins')
drop table Admins
go

create table Admins
(
	LoginId int identity(1000,1) primary key,
	LoginPwd varchar(20) not null, --登录密码
	AdminName varchar(20) not null
)
go

数据的基本操作

插入实体

查询实体

T-SQL中的运算符

更新实体

删除实体

数据完整性设计与实现

数据完整性的设计

完整性约束的类型

检查约束

-- 创建检查约束
if exists(select * from sysobjects where name='ck_Age')
alter table Students drop constraint ck_Age
alter table Students add constraint ck_Age check(Age between 18 and 25)

if exists(select * from sysobjects where name='ck_PhoneNumber')
alter table Students drop constraint ck_PhoneNumber
alter table Students add constraint ck_PhoneNumber check(len(PhoneNumber)=11)

默认约束

-- 默认约束
if exists(select * from sysobjects where name='df_StudentAddress')
alter table Students drop constraint ck_StudentAddress
alter table Students add constraint ck_StudentAddress default('地址不详') for StudentAddress

外键约束

-- 外键约束
if exists(select * from sysobjects where name='fk_ClassId')
alter table Students drop constraint fk_ClassId
alter table Students add constraint fk_ClassId foreign key (ClassId) references StudentClass(ClassId)

数据完整性总结

实体完整性

域完整性

- 表中特定列数据的有效性,确保不会输入无效的值
- 实现方式:数据类型限制、缺省值、非空值

引用完整性

完整数据库创建过程

graph LR 建库 --> 建表 --> 主键约束 --> 域完整性约束 --> 外键约束

插入数据过程

graph LR 验证主键 --> 主外键关系 --> 约束检查 --> 插入成功

常用数据查询

数据的基本查询

理解查询

基本语法构成

查询函数的使用

聚合函数

多表之间的数据查询

内连接查询

查询结果是两个源表中严格满足连接条件的记录相连

select Students.StudentId,C#成绩=CSharp,StudentName,ClassName 
from ScoreList
inner join Students on Students.StudentId=ScoreList.StudentId
inner join StudentClass on Students.ClassId=StudentClass.ClassId
where CSharp > 80

需要注意的问题:

  1. 需要连接的表
  2. 两个表连接的条件(主外键)
  3. 两个表中相同的公共字段必需说明来自那个表

左外连接查询

查询的结果包括两个表所有满足连接条件的记录,以及左表所有不满足条件的其他记录。这些不满足的左表记录,在结果的右边位置,全部填上Null值

select Students.StudentId,StudentName, Gender,C#成绩=CSharp
from Students
left outer join ScoreList on Students.StudentId=ScoreList.StudentId
where Gender='男'

右外连接查询

查询的结果包括两个表所有满足连接条件的记录,以及右表所有不满足条件的其他记录。这些不满足的右表记录,在结果的左边位置,全部填上Null值

select Students.StudentId,StudentName,ClassName
from Students
right outer join StudentClass on Students.ClassId=StudentClass.ClassId

分组查询与统计

-- 分组查询
select 班级=StudentClass.ClassName,人数=COUNT(*),C#最高分=MAX(CSharp)
from Students
inner join StudentClass on Students.ClassId=StudentClass.ClassId
inner join ScoreList on ScoreList.StudentId=Students.StudentId
group by ClassName

分组统计筛选--having

select 班级=StudentClass.ClassName,人数=COUNT(*),C#最高分=MAX(CSharp),DB平均分=AVG(SQLServer)
from Students
inner join StudentClass on Students.ClassId=StudentClass.ClassId
inner join ScoreList on ScoreList.StudentId=Students.StudentId
group by ClassName
having AVG(CSharp)>=70 and AVG(SQLServer)>=70

查询重复数据

-- 在知道那个字段重复的情况下
select StudentId from ScoreList group by StudentId having COUNT(*)>1

-- 查询所有的重复的记录
select * from ScoreList
where StudentId in(select StudentId from ScoreList group by StudentId having COUNT(*)>1)
order by StudentId

-- 其他方法
select * from ScoreList
where (select COUNT(*) from ScoreList s where s.StudentId=ScoreList.StudentId)>1
order by StudentId

分组查询对比

graph LR A[ where ] B[ group by ] C[ having ] A --> B --> C

数据库设计

数据库设计的基本步骤

数据库设计的检验与模型设计

数据库三范式

第一范式(1st NF - First Normal Form)

第二范式(2nd NF - Second Normal Form)

如果一个关系满足1NF,并且除了主键以外的其他列,都和主键列相关,则满足第二范式(2NF)

标签:StudentId,入门,--,数据库,SQLServer,Students,开发,where,select
来源: https://www.cnblogs.com/holychan/p/15759638.html