SQL Server数据库之代码篇

各种基本之语句:

--前言之基本操作
create database temp1
create table temp2(表格名 nvarchar(50) not null)
insert into temp2 values(999)
select * from temp2


--一、插入语句insert格式:insert 表名(列名1, 列名2) values(插入信息1,插入信息2)
--1,一次添加一行数据
insert Customer(loginID, password, name, address, phone) 
values(103, 123, '王五','湖南长沙',120)
--2,一次添加两行数据
insert Customer(loginID, password, name, address, phone) 
select 103, 123, '生七','湖南长沙',121 
union
select 104, 123, '赵六','湖南郴州',119


--二、更新语句update格式:update 表名 set column1 = value1, volumn2 = value2 [where ...]
update employee set address = '美国'
update employee set salary = 10000+100 where name = '阿六'


--三、删除语句delete格式:delete from 表名 [where ...]
delete from customer where loginID = 102
delete from customer
--删除整张表
drop table OrderCopy
--复制表格:select * into 新表名 from 复制表名
select * into OrderCopy from Customer


--四、查询语句select格式:select * from 表名
select * from employee where name = '阿六'--查询employee表中的阿六
select name from employee--查询employee表中的全部姓名
select * from [Order]--order是关键字
--查询加薪前后的工资以及对应的姓名
select name 姓名, salary 加薪前的工资, salary*2 加薪后的工资 from employee


--五、列标题/别名格式:select 列名1 别名1, 列名2 别名2 from 表名
select name 姓名, price 价格 from [order]


--六、过滤语句distinct格式:select distinct 列名 from 表名
select distinct birthday 生日,name 姓名 from employee

各种基本之函数:

create table temp(数字 int not null)--创建表格
insert into temp values(100)--向表中插入100这个数据
select * from temp--查询表格temp
drop table temp--删除整张表


--一、加法运算
select num 数字, num+1 加法, num%1010 from temp


--二、多条件查询
select * from Customer where name != '李一'
select * from Customer where name != '李一' and address = '江西'


--三、排序:默认升序,desc降序
select name 姓名, loginId 账号, password 密码, address 地址, phone 电话
 from customer order by loginId desc, phone--loginid相同就排phone


--四、取前N个值:TOP  N
select * from customer
select top 3  * from customer order by phone 
select top 3 percent * from customer order by phone 

--五、like运算符
--1,“%”通配符
--查询姓为“张”的
select name 姓名, address 地址, phone 电话 from Customer where 
name like '张%'
--查询名为“三”的
select name 姓名, address 地址, phone 电话 from Customer where 
name like '%三'
--查询名为“三”的
--2,“_”通配符:如查询张*,
--如果该姓名有三个字那么就用两个通配符,即“__”(一个通配符=一个字)
select name 姓名, address 地址, phone 电话 from Customer where 
name like '张_'
--3,“[]”通配符:查询存在某个字符的数据([^])
select name 姓名, address 地址, phone 电话 from Customer where 
name like '[张]三'


--六、in和between...and
--1,in:用于判断一个值是否在一个指定的数据集合之内
select * from customer where address in('湖南永州')
select * from customer where address = '北京' or address = '上海'
--2,between... and:值可以是数字、字符和日期类型,取值包含边界
select * from customer where customerid between 10 and 30
--七、字符串函数
--1,replace
select replace('ming','ing','i') 替换后的结果
--2,reverse
select reverse('ming')
--3,str:数字转字符
select str('1212.322')
--4,substring
select substring('超神学院',1,2)
select name 姓名, substring(name,2,1) 截取后 from customer


--八、日期时间函数
--1,获取当前时间
select getDate()
--2,获取当前时间的年、月、日(year、month、day)
select day(getdate()), month(getdate()), year(getdate())--3,获取两个时间之间的间隔(datediff)
select datediff(day,getdate(),'2021/4/13') 天数, 
datediff(month,getdate(),'2021/4/13') 月数,
datediff(year,getdate(),'2021/4/13') 年数
--4,dateadd函数
select dateadd(day, 1, getdate()) 一天后, 
dateadd(month, 1, getdate()) 一月后,
dateadd(year, 1, getdate()) 一年后
--5,获取当前是星期几
select datename(weekday, getdate())


--九、数学函数
--1,power(开方)
select power(2,2) '2的2次方', power(2,3) '2的3次方'
--2,round函数(保留小数位)
select round(122.23232,2)
--2,rand函数(0~1之间的随机数)	
select round(rand(),1)*10 '转化为0~10之间的随机数'


--十、类型转换
--1,cast:CAST(expression as data_type)
select cast('mingright' as varchar(2)) cast结果
--2,convert:CONVERT(data_type[(length)],expression[,style])
select convert(nvarchar(30),getdate(),130) convert结果

其中convert可以选择的样式有,如下表所示
在这里插入图片描述

聚合函数:

--一、聚合函数
--1,sum(和)
select sum(salary) 全体员工工资总和 from employee
--2,max/min函数(最大值/最小值)
select min(salary) 最高工资 from employee
select max(salary) 最高工资 from employee
--3,avg函数(平均值)
select avg(salary) 最高工资 from employee
--4,count函数(返回列数/行数)
--比如count(*):获取表行数;count(column):获取不会空的行数
select count(*) 员工人数 from employee
select count(birthday) 有出生记录的员工人数 from employee

发布了86 篇原创文章 · 获赞 3 · 访问量 1385

猜你喜欢

转载自blog.csdn.net/wnamej/article/details/105488772