SQL Server学习总结(1)

1.数据库管理系统(DBMS)是一种系统软件,由一个互联关联的数据集合和一组访问数据的程序结构.

2.在数据库系统中,数据重复的现象就是数据冗余.

3.SQL Server中的数据库按照用途可以划分为两种.(系统数据库和用户数据库)

4.SQL Server系统数据库:

(1)Master数据库:记录SQL Server系统的所有系统级别信息.(1.所有的登录账户和系统配置设置.2.所有其他的数据库及数据库文件的位置.3.SQL Server的初始化信息.)

(2)Tempdb数据库:保存所有的临时表和临时存储过程,以及临时生成的工作表.

(3)Model数据库:在系统上创建的所有数据库的模板.

(4)Msdb数据库:SQL Server代理程序调度警报,作业以及记录操作时使用.

5.访问限制:指定哪些用户可以访问该数据库.可能值有以下三种:

(1)Multiple:数据的正常状态,允许多个用户同事访问该数据库.

(2)Single:用于维护操作的状态,一次只允许一个用户访问该数据库.

(3)Restricted:只有管理员角色或者特定的成员才能使用该数据库.

6.SQL Server启动的时候,数据库文件是不能复制,粘贴和移动的.

7.数据完整性

1)实体完整性:要求表中的每一行数据都反映不同的实体,不能存在相同的数据.(索引、唯一约束、主键约束或标识列属性)

2)域完整性:给定列的输入有效性(限制数据类型、检查约束、输入格式、外键约束、默认值、非空约束)

3)引用完整性:在输入或删除数据行时,引用完整约束来保存表之剑已定义的关系(主键与外键的引用关系来实现)。

4)自定义完整性:定义特定的规则(数据库的规则、存储过程、触发器对象来进行约束)

8.T-SQL支持逻辑运算符有:AndOr、和Not

9.使用Insert插入数据行

1)插入单行数据

语法:insert [into] <表名> [values] <值列表>

例:insert into students (name,age,sex) values('Harry',20,'')

(2)插入多行数据

语法:insert <表名> [values]

  Select <值列表> union

  Select<值列表

例:insert students(name,age,sex)

Select '张三',20,'' union

Select '李四',18,''

(3)通过 insert select语句将现有表中的数据添加到新表中

语法:insert [into] <1><values>

  Select vaules from <2>

例:insert into student1(姓名,年龄,性别)

    Select name,age,sex from student2

10.使用update更新数据

语法:update <表名> set <列名=更新值>[where <更新条件>]

例:update student set sex='' where studentid=2

11.使用delete删除数据

语法:delete from <表名> [where <删除条件>]

例:delete from student where studentid=2

12.使用turncate table删除表中的所有行数据

语法:turncate table <表名>

例:turncate table student

13.简单查询语句

1select * from students

2select studentID,name,age,sex from students

3select studentID,name,age,sex from students where studentID=10

4select name from students where sex is null

5select 姓名=name,年龄=age,性别=sex'河北新龙' as 学校名称 

6select top 5 name,age,sex from students where objectid=3 order by score desc(或者ASC升序排列)

14.使用like进行模糊查询

例:select * from students where name like '%'

15.使用between在某个范围内进行查询

例:selecr * from student where score between 60 and 80

16.聚合函数

Sum:求和

Avg:平均数

Max:最大值

Min:最小值

Count:求非空行的行数

17.分组查询

(1)使用group by进行分组查询

例:select courseIDavg(score) as 课程平均成绩 from score group by courseID

(2)使用having子句进行筛选

例:select courseIDavg(score) as 课程平均成绩 from score group by courseID having count(score)>1

18.where--->group by--->having

19.多表连接查询

(1)内联接:inner join

(2)外联接

(1)左外联接:left join 或者 left outer join

(2)右外联接:right join 或者 right outer join

(3)完整外联接:full join 或者 full outer join

20.设计数据库步骤

(1)收集信息

(2)标识实体

(3)标识每个实体需要存储的详细信息(属性)

(4)标识实体之间的关系

21.数据库3大范式

(1)第一范式(1NF):列不可再分

(2)第二范式(2NF):每个表只描述一件事情

(3)第三范式(3NF):没有传递依赖

TSQL语句

1.使用TSQL语句创建和删除数据库

Create database 数据库名

On [primary]

(

<数据文件参数>[,......n] [<文件组参数>]

)

[log on]

(

{<日志文件参数>  [,......n]}

)

文件的具体参数如下:

Name=逻辑文件名

Filename=物理文件名

Size=文件大小

Maxsize=最大容量

Filegrowth=增长量

 

例:

--调用CMD命令创建文件夹

exec sp_configure 'show advanced options',1

go

reconfigure

go

exec sp_configure 'xp_cmdshell',1

go

reconfigure

go

exec xp_cmdshell 'mkDir E:\Project'

go

 

if exists(select * from sysdatabases where name='MySchool')--判断是否存在MySchool数据库

drop database MySchool--删除数据库

Go

 

create database MySchool--创建数据库

--主文件

on

(

name='MySchool_data',

filename='E:\project\MySchool_data.mdf',

size=10,

filegrowth=20%

)

--日志文件

log on

(

name='MySchool_log',

filename='E:\project\MySchool_log.ldf',

size=3,

maxsize=20,

filegrowTh=1

)

Go

 

2.使用TSQL创建和删除表

Create table 表名

(

数据类型 列的特征,

数据类型 列的特征

......

例:

use MySchool

go

 

if exists (select * from sysobjects where name='Subject')--判断是否存在Subject

drop table Subject 

go

create table Subject--创建Subject

(

SubjectId int identity(1,1) not null, --identity自动增长列

subjectName nvarchar(50) not null,

ClassHour int not null,

GradeId int not null

)

go

 

3.使用TSQL语句创建和删除约束

主键约束(Primary Key Constraint):要求主键列数据唯一,并且不允许为空,如学号能唯一确定一名学生。

非空约束(Not Null):要求列不能存在空值,如学生的姓名不能为空。

唯一约束(Unique Constraint):要求该列的值必须唯一,允许为空,但只能出现一个空值。

检查约束(Check Constraint):某列取值范围限制,格式限制等,如有关年龄的约束。

默认约束(Default Constraint):某列的默认值,如我们的男性学生较多,性别默认为男。

外键约束(Foreign Key Constraint):用于两表之间建立关系,需要指定引用主表的哪一列。

例:

(1)主键约束(PK_

if exists (select * from sysobjects where name ='pk_SubjectId')

alter table Subject

drop constraint pk_SubjectId

go

alter table Subject

add constraint pk_SubjectId primary key (SubjectId)

go

(2)唯一约束(UQ_)

if exists (select * from sysobjects where name ='uq_SubjectName')

alter table Subject

drop constraint uq_SubjectName

go

alter table Subject

add constraint uq_SubjectName unique (SubjectName)

go

(3)检查约束(CK_)

if exists (select * from sysobjects where name ='ck_ClassHour')

alter table Subject

drop constraint ck_ClassHour

go

alter table Subject

add constraint ck_ClassHour check (ClassHour>0)

go

(4)默认约束(DF_)

if exists (select * from sysobjects where name ='df_Exa')

alter table Result

drop constraint df_Exa

go

alter table Result

add constraint df_Exa default (getdate()) for ExamDate

go 

(5)外键约束(FK_1_2)

if exists (select * from sysobjects where name ='fk_Subject_Grade_GradeId')

alter table Subject

drop constraint fk_Subject_Grade_GradeId

go

alter table Subject

add constraint fk_Subject_Grade_GradeId foreign key (gradeId) references Grade(GradeId)

Go