第三阶段数据库-2:数据库中的sql语句
1_数据库操作
(1)注释:
-- 单行注释
/**/ 多行注释
(2)创建数据库:create database 数据库名
-- create database 数据库名
create database db_first;
(3)查询数据库:
if exsists(select * from sysdatabases where name='StudentManageDB')
(4)删除数据库:drop database 数据库名
-- drop database 数据库名
drop database db_first;
(5)创建变量,可以声明的时候赋值,也可以先声明再赋值
declare @currentPage int;
declare @pageSize int;
set @currentPage=1;
set @pageSize=10;
2_数据表操作
(1)创建表:creat table 表明(列名,数据类型)
not null:设定该列非空,
primary key,设定该列为主键,
identity(1,1) 设定该列自增,从1开始自增,增幅为1
create table Teachers(Id int not null primary key identity(1,1),TeacherName varchar(30) not null,Age int not null default(20),Sex int )
(2)查询表
if exists (select * from sysobjects where name='StudentClass')
(3)删除表
drop table StudentClass
3_插入数据
(1)表的插入 :insert into 表名(列名) values (值),
插入的时候 表名后面填写列名,values 后面填写值 值和列名要--对应
当主键设置了标识规范的时候,不让插入,只有吧IDENTITY_INSERT设置为ON才能插入
数据库对大小写不敏感,IDENTITY_INSERT与identity_insert一样
GETDATE() 是SQL_server 中得函数 用来获取当前日期
--插入
insert into Teachers (TeacherName,Age) values ('孙老师',30)
insert into Teachers (TeacherName,Age,Sex) values ('孙老师',20,0)
SET IDENTITY_INSERT Teachers ON;
insert into Teachers (Id,TeacherName,Age,Sex) values (6,'王老师',20,0)
SET IDENTITY_INSERT Teachers OFF;
insert into Students(StuName,StuAge,Birthday) values ('张三',20,'2025-02-01 14:13:45');
insert into Students(StuName,StuAge,Birthday) values ('韩李四',20,GETDATE());
--插入多条数据
insert into Teachers(TeacherName,Age)
--自定义结果集 开发者用多条数据合并而来
select '王老师1',30
union all
select '王老师2',30
union all
select '王老师3',30
4_修改数据
(1)修改数据:update 表名 set 修改的列名='值' where 条件
or 或者,满足一项就可以,相当于C#中的||
and 并且 满足所有的条件 相当于&&
不加修改条件的时候会修改所有的数据,切记加条件
update Students set StuName='张三' where Id=7;
update Students set StuName='王大陆3',StuAge=10 where Id=6 or Id=5; -- or ===> ||
update Students
set StuName='4563'
where Id=6 and StuAge=100; ---> and ===> &&
5_删除数据:
delete from 表名:删除数据 ,标识符不重置,可以使用where添加删除条件
truncate table 表名:删除清空数据 保留表结构 标识规范重置,不能添加where 谨慎使用
delete from Teachers;
delete from Teachers where Id=2;
delete from Teachers where TeacherName ='孙老师';
truncate table Teachers ;
6_查询数据
6.1_基本查询
(1)查询语句 会查到一个结果集 把结果集返回出来
select * from 表明: * all 全部列
-- * all 全部列
select * from Students;
(2)查询部分行,使用 'as' 或使用 '=' 重新命名字段
--查询部分列
select StuName,StuAge from Students;
-- 查询替换列名
select TeacherName as 老师姓名,Age as 年龄 from Teachers
--使用等号重命名字段
select 出生年月=Birthday from Students where Gender='男'
(3)条件查询,多个并列条件使用 and 连接,多个或条件使用 or 连接
--加 where 筛选
select * from Students where Id =1;--查询id等于1的全部数据
select StuName from Students where Id =4; --查询id等于4的学生姓名
select StuName from Students where Id =4 and StuAge=10;
select StuName from Students where Id =4 or StuAge=10;
(4)使用加号可以将多列数据显示到同一列中
+ 连接的数据类型必须兼容
如果使用 + 连接字符型数据,结果为字符串数据的连接
如果使用 + 连接数值型数据,结果为数值的和
select 学号=StudentId,总成绩=CSharp + SQLServer from ScoreList
(5)查询空列
select * from ScoreList where SQLServer is null
(6)使用常量列:增加新的一列
select StudentName, Gender, Birthday, Age, StudentIdNo,学校='111' from Students where Gender='男' and Age > 24
(7)限制固定行数 top 顶端的,
top n 最上边的n行数据
top 40 percent 返回百分之多少行
select top 4 StudentName, Gender, Birthday from Students
select top 40 percent StudentName, Gender, Birthday from Students
(8)排序
升序:asc 默认为升序排列,可省略
降序:desc
select StudentId, (CSharp + 5) as C#, DB=SQLServer
from ScoreList
where (CSharp + 5) > 80
order by CSharp ASCselect StudentId, (CSharp + 5) as C#, DB=SQLServer
from ScoreList
where (CSharp + 5) > 80
order by CSharp DESC
(9)多列排序,前一个条件相等时,自动按照下一个条件排序。
select StudentId, (CSharp + 5) as C#, DB=SQLServer from ScoreList where (CSharp + 5) > 80 order by CSharp DESC, SQLServer DESC
6.2_模糊查询
(1)like:使用 like 查询时,字段中的内容并不一定与查询内容完全匹配,只要字段中含有这些内容即可。
select * from Students where StuName like '王%';-- 以 '王' 开头
select * from Students where StuName like '%2';-- 以 '2' 结尾
select * from Students where StuName like '%2%';-- 包含 '2' 2 在结尾 中间 开头 都可以匹配
(2)between:把某一字段中的值在特定范围内的记录查询出来,使用 between包含断点值(闭合区间)。
-- between 之间
select StuName from Students where Id between 5 and 8;select StudentName, Birthday from Students where Birthday between '1999-01-01' and '2001-05-05'
--建议不要比较字符串
-- select * from Students where StuName >= '吴亦凡' and StuName<='王大陆'
(3)IN:即把某一字段中内容与所列出的查询内容列表匹配的记录查询出来(相当于把要查询的内容通过枚举的方式一一列出来),更精确一些。
IN: 指定某列的值必须在指定的列表中,
NOT IN :操作符用于指定某列的值不能在指定的列表中。
--IN操作符用于指定某列的值必须在指定的列表中。
select * from Students where StuName in ('张三','李四');
-- NOT IN操作符用于指定某列的值不能在指定的列表中。
select * from Students where StuName not in ('张三','李四');
6.2_多表联查
(1)连接分类
外连接(outer)
左连接:Left,左表为主,返回左表中的所有行,如果左表中行在右表中没有匹配行,则结果中右表中的列返回空值。
右连接,right,右表为主,返回右表中的所有行,如果右表中行在左表中没有匹配行,则结果中左表中的列返回空值。
全连接:返回左表和右表中的所有行。当某行在另一表中没有匹配行,则另一表中的列返回空值
内连接(inner)
等值连接:在连接条件中使用等于号(=)运算符,其查询结果中列出被连接表中的所有列,包括其中的重复列。
不等链接:在连接条件中使用除等于号之外运算符(>、<、<>、>=、<=、!>和!<)
交叉连接
不带where条件子句:它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积(例如:T_student和T_class,返回4*4=16条记录),如果带where,返回或显示的是匹配的行数
有where子句:往往会先生成两个表行数乘积的数据表,然后才根据where条件从中选择。cross join后加条件只能用where,不能用on
--多表查询
--外连接
--左连
select * from CustomerInfo as C
left outer join AddressInfo as A on C.AddressId=A.AddressId;
--左连
--as 可以省略,outer也可以省略,默认为outer
select * from CustomerInfo as C
left join UserInfo U on C.CreateUaerId=U.UserId;
--右连接
select * from CustomerInfo as C
right join UserInfo U on C.CreateUserId=U.UserId;
--全连接
select * from CustomerInfo as C
full join UserInfo U on C.CreateUserId=U.UserId;
--设置显示的列
select C.CustomerId,C.CustomerName,C.Sex,C.Age,C.Phone,A.ProvinceName,A.City,A.Area from CustomerInfo as C
left outer join AddressInfo as A on C.AddressId=A.AddressId;
--列可使用+显示在一列
select C.CustomerId,C.CustomerName,C.Sex,C.Age,C.Phone,A.ProvinceName+A.City+A.Area DataiAddress from CustomerInfo as C
left outer join AddressInfo as A on C.AddressId=A.AddressId;--内连接
--等值连接
select * from CustomerInfo as C
inner join UserInfo U on C.CreateUserId=U.UserId;
--不等连接
select * from CustomerInfo as C
inner join UserInfo U on CreateUserId<>U.UserId;
--不等连接
select C.CustomerId,C.CustomerName,C.AddressId,A.AddressId, A.ProvinceName+A.City+A.Area as DataiAddress from CustomerInfo as C
inner join AddressInfo as A on C.AddressId<>A.AddressId;--交叉连接
--不带where
select C.CustomerId,C.CustomerName,C.Age,A.AddressId, A.ProvinceName+A.City+A.Area as DataiAddress from CustomerInfo as C
cross join AddressInfo as A
--带where
select C.CustomerId,C.CustomerName,C.Age,A.AddressId, A.ProvinceName+A.City+A.Area as DataiAddress from CustomerInfo as C
cross join AddressInfo as A where C.AddressId=A.AddressId;
6.3_分组查询与统计
(1)使用Group by分组
分组 Group By 的标准,一般要出现在展示项中,一般形如:select 聚合函数, xx, [不要出现非聚合项] from table_name group by xx
select COUNT(*) as 总人数, ClassName from Students
inner join StudentClass on StudentClass.ClassId = Students.ClassId
group by ClassNameselect Score ,Count(Score) as ScoreCount from StudentInfo--查询
where Score>=90 and Score<=100 --筛选
group by Score --分组
order by ScoreCount desc,Score asc; --排序
(2)分组统计筛选 having:
分组后筛选:借助having子句,having子句,只能配合group by使用
having count(Score)>=4专门对分组后的结果进行二次筛选,列的别名不能当作having条件
select Score ,Count(Score) as ScoreCount from StudentInfo
where Score>=90 and Score<=100
group by Score having count(Score)>=4
order by ScoreCount desc,Score asc;
--having的其他作用
-- 查询重复的字段
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
(3)嵌套查询,
select * from
(
select Score ,Count(Score) as ScoresCount from StudentInfo
where Score>=90 and Score<=100
group by Score
)
as MyTable where ScoresCount>=4
(4)分组查询对比
where子句:
从数据源中去掉不符合其搜索条件的数据
group by 子句:
搜集数据行到各个组中,统计函数为各个组计算统计值
having 子句:
在分组结果中,去掉不符合其组搜索条件的各组数据行