SqlServer必知必会之我的小笔记

        这里分为二十二个课时把平时学习到的SqlServer数据库知识要点在这里一一罗列下来,做为工做中笔记速查,无论是面试仍是在项目中对SqlServer数据库的操做应用,这些知识点都是很是基础和经常使用的,有必要把它们汇总记录、往后备查!话很少说,直接上干货!面试

 

课时(一)登陆数据库

 

登陆数据库的方式:数据库

①Windows身份验证;函数

②Sql Server身份验证学习

 

 

课时(二)备份数据库

 

 

课时(三)数据表的建立

nvachar 存储类型字体

遇到操做系统是英文操做系统而且对中文字体的支持不全面时,在SQL Server存储中文字符为varchar就会出现乱码(显示为??),惟一能解决问题的是把数据库字段的类型改成nvarchar或 者nchar。spa

 

使用nvarchar的另外一个很是好处就是在判断字符串的时候能够不须要考虑中英文两种字符的差异.固然,使用nvarchar存储英文字符会增大一倍的存储空间,可是在存储代价已经很低廉的状况下,优先考虑兼容性会给你带来更多好处的。操作系统

 

课时(四)建立外键关联

选择主表的某一列,右键点击“关系”,添加关系,在表和列规范点击,3d

 

 

设置好主键表和外键表的关联ID便可:code

 

 

课时(五)数据的添加Insert

 

 

设置主键自增blog

 

 

课时(六)批量插入数据使用UNION

 

 

 

 

课时(七)删除delete和更新update

 

 

课时(八)select查询语句

 

 

select top 7 * from Student; ##查询表中的行数据
select top 5 percent * from Student; ##查询表中的5%的数据

 

课时(九)like模糊查询

 

 

 

 

课时(十)查询排序

 

 

课时(十一)聚合函数的使用

 

 

 

 

 

 

课时(十二)分组查询

 

 

 

 

select ShoolID 学校编号, COUNT(*) 学生数量, SUM(Score) 成绩总和 FROM student GROUP By SchoolID

 

课时(十三)分组条件查询having

 

 

课时(十四)链接查询

 

 

课时(十五)UNION联合查询

 

 

 

 

课时(十六)子查询——单表查询

 

 

 

 

举例:

如查询山东大学全部的学生信息:

①最笨的方式实现;

首先查询到山东大学的编号,而后根据编号查询出学生信息;

 

 

②链接表方式;

 

 

③子查询;

Select * from student where student.SchoolID = (select SchoolID from School where SchoolName = ‘山东大学’);

课时十七 子查询——多表子查询

 

 

举例:

咱们要查询全部学校编号大于山东大学编号的学生信息。

Select * from student where student.SchoolID > (select SchoolID from School where SchoolName = ‘山东大学’);

课时十八 子查询——in和not in的使用

 

 

 

 

课时(十九)子查询—all和any/some关键字的使用

 

 

 

 

 

 

 

课时(二十)视图和索引

 

 

 

 

 

 

课时(二十一)变量的使用

 

 

 

 

注意声明和使用必须同时执行。

 

 

课时(二十二)存储过程

 

 

 

 

 

 

示例Sql代码:

create procedure pro_student1
as 
select * from Student;
select * from School;

exec pro_student1

go
create procedure pro_student2
(
    @name varchar(50),
    @sname varchar
)
as 
select * from Student where Name = @name;
select * from School where SchoolName = @sname;

exec pro_student2 '王小二', '西南财经大学'

go
create procedure pro_student3
(
    @name varchar(50),
    @sAge int output
)
as 
select @sAge = Age from Student where Name = @name;

declare @outAge int;
exec pro_student3 '王小二',@outAge output
print @outAge
相关文章
相关标签/搜索