1月10日 SQL SERVER 增删改查(第一节)

一、登陆

SQL SERVER两种登录方式的设置:Windows身份登录;SqlServer身份登录。
如何设置SQLServer身份验证?
1.对象资源管理器右击--属性--安全性--SqlServer和Windows身份登录。
2.对象资源管理器--安全性--登录--sa--右击--属性--常规--设置密码
3.对象资源管理器--安全性--登录--sa--右击--属性--状态--授予,启用
重启数据库服务。

二、SQL语句 ( 增、删、改、查) 

1、增(两种写法)

insert into 表名(列名,列名,列名,...) values(值,值,值,....)

insert into 表名 values(值,值,值,值。。)

2、删

delete from 表名  --删除表内所有数据

delete from 表名 where 条件 --删除符合条件的语句

3、改

update 表名 set 列名=值,列名=值..... where 条件

4、查(重点)

查询方式有很多种

(一)、简单查询

  select*from 表名  ----基本形式(*代表所有列,*位置也可加列名)

 1、投影

   select 列名,列名,...from 表名 

 2、筛选

 (1)、等值、不等值查询

select * from 表名 where 列名=值 ---等值查询

select * from 表名 where 列名 <> 值      --不等值查询(<>相当于!=)
select * from 表名 where 列名 > 值 >=
select * from 表名 where 列名 < 值 <=

(2)、多条件查询(逻辑与(and),逻辑或(or)) 

select * from 表名 where 条件1 and 条件2 ...
select * from 表名 where 条件1 or 条件2 ...
如果在where筛选条件中,既出现and又出现or,则先运算and。除非使用小括号改变优
先级。

(3)、范围查询

select * from 表名 where 列名 >=范围1 and 列名<=范围2

例如:

select * from Car where Price >=30 and Price<=50
select * from Car where Price between 30 and 50

select * from Car where Oil=7.4 or Oil=8.5 or Oil=9.4
select * from Car where Oil in(7.4,8.5,9.4)        --可以用where 列名 in(1,2,,,,)

(4)、模糊查询

select * from 表名 where 列名 like '标识字%'

%——任意多个任意字符
_——一个任意字符

例:
select * from Car where Name like '宝马%'
宝马%——以宝马开头的
%宝马——以宝马结尾的
%宝马%——只要含有宝马这两个字就可以。

__宝马%——代表第三个字符以宝马开头的。

(5)、去重查询:

select distinct 列名 from 表名     ——如果列中有重复值,则只查1个出来。

(6)、top 查询

取前几条数据
select top 数量 [列名|*] from 表名

3、排序

select * from 表名 where 条件 order by 列名 ASC|DESC,列名 ASC|DESC

例:

select * from car order by price asc ——默认是升序 ascending descending
select * from car order by price desc
select * from Car order by Oil asc,Price desc ——Oil主排序,Price次排序

4、分组

统计函数(聚合函数)
count(), max(), min(), sum(), avg()

count()统计总行数
count(*)得到所有的行数
count(列)得到该列中所有非null个数。
select COUNT(*) from car where Brand='b003'

max(列) 这一列的最大,min(列)这一列的最小
select min(price) from car

sum(列)这一列的和,avg(列)这一列的平均
select AVG(price) from car

group by ...having...

1.group by后面跟的是列名。
2.一旦使用group by分组了,则select和from中间就不能用*,只能包含两类东西一类是:group by 后面的列名,另一类是统计函数
select Oil,avg(price) from Car group by oil
对于统计函数生成的列,默认是无列名,可以通过下面的方法指定列名。
select Oil as 油耗,COUNT(*) as 数量,avg(price) 均价 from Car group by oil

having后面一般跟得是统计函数。它用来对分组后的数据进一步筛选。

(二)、复杂查询

1、链接查询:
第一步:求笛卡尔积
select * from Info,nation
第二步:根据两个表相对应的列,对笛卡尔积进行有效数据的筛选。
select * from Info,Nation where Info.Nation = Nation.code
第三步:调整显示要查询的列
select Info.Code,Info.Name,Info.Sex,Nation.Name,Info.Birthday 
from Info,nation where Info.Nation=Nation.Code

一般用join。。。on连接

select * from 表名1 
join 表名2 on 表名1.列 = 表名2.列
join 表名3 on 表名2.列 = 表名3.列
....
where 查询条件

左连(left join),右连(right join),全连(full join)

2、联合查询
把多个表的行合在一个界面视图中。
用union把两个查询组合在一起。要求是这两个查询的列要一一对应。

3、子查询(嵌套查询)

(一)无关子查询:
至少是两层查询,在外层查询的里面再写查询。

里层查询为外层查询提供查询的中间内容。

例:查询“张旭“教师任课的学生成绩。--成绩、教师、课程都不在一个表中

select degree from score where cno=
(
select cno from course where tno=
(
select tno from teacher where tname='张旭'
)
)

原文地址:https://www.cnblogs.com/tzq9308/p/4215749.html