SQLServer入门基础(SQL语句)

图形化用户界面
命令行用户界面

数据类型:
char(固定长度)   varchar(长度随输入变化)
在字符串长度固定的情况下,用char,因为char速度快

nchar和nvarchar是在前者的基础上采用了Unicode编码
Unicode编码的特点是字节和汉字占用的空间一样啊

bit(不用)   int	bigint	floa t	numeric(更加精准,好控制)

datetime	timestamp
getdate()	得到当前时间
create table aaa
(shijian datetime)
insert aaa values(getdate())  --插入系统时间
insert aaa values('2019/1/23')  --插入时间 按照默认的格式会将/变成-
select * from aaa
drop table aaa

图片类型image和视频类型binary一般都放在专门的服务器上,因为太占用带宽
只有当需要安全时才直接放入数据库,而且尽量小
基础操作

–是注释 格式 拼写
–新建数据库
create database shuguo
–建表的同时建字段
create table renwu
( bianhao int,
xingming nvarchar(5),
xingbie nchar(1),
zhiwu nvarchar(10),
nianling int,
gongzi numeric(6,2) --一共占6位,小数占2位
)
–删除表(包括字段) drop只删除表和数据库
drop table renwu

select * from renwu
–别名
select xingming 姓名,zhiwu 职务 from renwu
–添加记录
insert into renwu values(1,‘刘备’,‘男’,‘主公’,40,9999)
insert into renwu (bianhao,xingming,xingbie,zhiwu) values(2,‘甘夫人’,‘女’,‘夫人’)
insert into renwu values(3,‘诸葛亮’,‘男’,‘军师’,37,8000)
insert into renwu values(4,‘关羽’,‘男’,‘将军’,38,6000)
insert into renwu values(5,‘张飞’,‘男’,‘将军’,35,5000)
–删除所有记录,不删字段
delete from renwu
–按条件删除记录
delete from renwu where bianhao=2
delete from renwu where xingming=‘甘夫人’
–按条件查询
select * from renwu where gongzi>7000
select xingming,xingbie,zhiwu,gongzi from renwu where gongzi>7000
–把工资低于8000的加上2%
update renwu set gongzi=gongzi*1.02 where gongzi<8000

–主键可以改,只要不重复,插入部分数据,主键必须得插入
create table gsyg
( bianhao int primary key,
xingming nvarchar(5),
nianling char(2)
)
insert gsyg values(1,‘唐僧’,‘78’)
insert gsyg values(2,‘孙悟空’,‘50’)
insert gsyg values(3,‘猪八戒’,‘57’)
insert gsyg values(4,‘沙和尚’,‘23’)
insert gsyg values(5,‘白龙马’,‘19’)
insert gsyg (bianhao,xingming) values(6,‘如来’)
update gsyg set nianling=24,xingming=‘观音菩萨’,bianhao=7 where bianhao=6
update gsyg set nianling=13 where nianling is null --当数据为空时
delete from gsyg where bianhao=7 and nianling=13
delete from gsyg where bianhao=7 or nianling=24
select * from gsyg

数据查询

水浒实例:
create database shuihu

create table yuangong
( ygbianhao int primary key,
xingming nvarchar(5),
zhiwu nvarchar(8),
shangji int,
rzshijian datetime,
gongzi numeric(6,1),
buzhu numeric(5,1),
bmbianhao int foreign key references bumen(bmbianhao)
)

insert into yuangong (ygbianhao,xingming,zhiwu,rzshijian,gongzi,buzhu,bmbianhao) values (101,‘宋江’,‘寨主’,‘2002/3/15’,20000,3000,1)
insert into yuangong values(102,‘卢俊义’,‘寨主’,101,‘2002/6/16’,18000,3000,1)
insert into yuangong values(103,‘吴用’,‘军师’,101,‘2002/8/19’,17000,2800,2)
insert into yuangong values(104,‘公孙胜’,‘军师’,103,‘2002/9/22’,16000,2600,2)
insert into yuangong values(105,‘关胜’,‘大将’,102,‘2003/2/15’,15000,2300,3)
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(106,‘徐宁’,‘小将’,105,‘2005/7/9’,14000,3)
insert into yuangong values(107,‘鲁智深’,‘大将’,102,‘2003/4/22’,13000,2000,4)
insert into yuangong values(108,‘武松’,‘大将’,107,‘2003/9/8’,12000,1800,4)
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(109,‘李逵’,‘小将’,107,‘2005/4/11’,10000,4)
insert into yuangong values(110,‘李俊’,‘大将’,102,‘2005/1/6’,13000,3000,5)
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(111,‘阮小二’,‘小将’,110,‘2005/6/24’,13000,5)
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(112,‘阮小七’,‘小将’,110,‘2005/6/15’,12000,5)
insert into yuangong values(113,‘柴进’,‘总管’,101,‘2004/3/5’,11000,1000,6)
insert into yuangong values(114,‘李英’,‘总管’,113,‘2006/3/10’,10000,600,6)
insert into yuangong (ygbianhao,xingming,zhiwu,shangji,rzshijian,gongzi,bmbianhao) values(115,‘时迁’,‘小头领’,113,‘2007/11/19’,8000,6)

create table bumen
(
	bmbianhao int primary key,
	mingcheng nvarchar(8),
	didian nvarchar(8)
)

insert into bumen values(1,‘总头领’,‘聚义厅’)
insert into bumen values(2,‘文职’,‘前山’)
insert into bumen values(3,‘马军’,‘左山’)
insert into bumen values(4,‘步军’,‘右山’)
insert into bumen values(5,‘水军’,‘山下’)
insert into bumen values(6,‘后勤’,‘后山’)

–查询鲁智深的工资,职务,所在部门的编号
select xingming,gongzi,zhiwu,bmbianhao from yuangong where xingming=‘鲁智深’
–查询一共有几个部门(去重)
select distinct bmbianhao from yuangong
–显示每个员工的年薪,以中文显示字段
select xingming 姓名,gongzi12 年薪 from yuangong
–计算年总收入和 空值 做四则运算
select xingming 姓名,gongzi
12+isnull(buzhu,0)12 年总收入 from yuangong
–查询2004年以后入职的员工
select xingming,rzshijian from yuangong where rzshijian>‘2004-1-1’
–查询工资在13000到1800之间的员工
select xingming 姓名,gongzi 工资 from yuangong where gongzi>13000 and gongzi<=18000
–查询姓李的员工
select xingming 姓名 from yuangong where xingming like ‘李%’
–查询第二个字是进的员工
select xingming 姓名 from yuangong where xingming like ‘_进%’
–批量查询
select xingming 姓名,ygbianhao 员工编号 from yuangong where ygbianhao in (101,105,108,114)
–显示一把手的姓名和职务
select xingming 姓名,zhiwu 职务 from yuangong where shangji is null
–排序 默认升序 desc降序 中文按照拼音顺序和音调
select xingming 姓名,zhiwu 职务,gongzi 工资 from yuangong order by gongzi desc
select xingming 姓名 from yuangong order by xingming
–查询总工资和平均工资
select sum(gongzi) 总工资,avg(gongzi) 平均工资 from yuangong
–查询最高工资和最低工资
select max(gongzi) 最高工资,min(gongzi) 最低工资 from yuangong
–查询最高工资所有人的姓名和工资
select xingming 姓名,gongzi 工资 from yuangong where gongzi=(select max(gongzi) from yuangong)
–查询高于平均工资的员工的姓名和工资
select xingming 姓名,gongzi 工资 from yuangong where gongzi>(select avg(gongzi) from yuangong)
select xingming 姓名,gongzi 工资,(select avg(gongzi) from yuangong) 平均工资 from yuangong where gongzi>(select avg(gongzi) from yuangong)
–按照部门编号升序,工资降序
select xingming 姓名,gongzi 工资,bmbianhao from yuangong order by bmbianhao,gongzi desc
–用起别名的方法算年总收入并降序排列
select xingming 姓名,gongzi
12+isnull(buzhu,0) 年总收入 from yuangong order by gongzi12+isnull(buzhu,0) desc
select xingming 姓名,gongzi
12+isnull(buzhu,0) 年总收入 from yuangong order by 年总收入 desc
–统计有多少条记录
select count(*) from yuangong
–统计每个部门的平均工资和总工资 group by 后面的字段必须出现在显示字段中
select bmbianhao,sum(gongzi) 总工资,avg(gongzi)平均工资 from yuangong group by bmbianhao
–查询每个部门每个职务的平均工资和最低工资 降序排列
select bmbianhao,avg(gongzi) 平均工资,min(gongzi)最低工资,zhiwu 职务 from yuangong group by bmbianhao,zhiwu order by bmbianhao
–显示平均工资低于15000的部门编号和平均工资
select bmbianhao 部门编号,avg(gongzi) 平均工资 from yuangong group by bmbianhao having avg(gongzi) <15000
–笛卡尔集现象(主外键没有对应)
select * from yuangong,bumen
select * from yuangong,bumen where bumen.mingcheng=‘水军’ and yuangong.bmbianhao=bumen.bmbianhao
select * from yuangong,bumen where yuangong.bmbianhao=bumen.bmbianhao
–查询姓名和所在部门以及编号
select xingming 姓名,mingcheng 部门,yuangong.bmbianhao 部门编号 from yuangong,bumen where yuangong.bmbianhao=bumen.bmbianhao
–查询部门号为4的部门名称,职员姓名和工资
select mingcheng 部门,xingming 姓名,gongzi 工资 from yuangong,bumen where yuangong.bmbianhao=bumen.bmbianhao and bumen.bmbianhao=4
–查询员工名,部门名和工资,按照部门排序
select xingming,mingcheng,gongzi from yuangong,bumen where bumen.bmbianhao=yuangong.bmbianhao order by bumen.bmbianhao desc
–查询李逵的上级
select xingming from yuangong where ygbianhao=(select shangji from yuangong where xingming=‘李逵’)
–查询所有员工的姓名和其上级(自连接)
select a.xingming 员工,b.xingming 领导 from yuangong a,yuangong b where a.shangji=b.ygbianhao
–子查询(嵌套查询)单行子查询:返回结果为单行的子查询称为单行子查询
–显示与鲁智深同部门的员工
select xingming from yuangong where bmbianhao=(select bmbianhao from yuangong where xingming=‘鲁智深’)
–多行子查询:返回结果为多行的子查询称为多行子查询
–多行子查询如果不能一次写对,就写两行,先写子查询,再写主查询
–查询和5号部门相同职务的员工姓名,职务,工资和部门编号
select xingming 姓名,zhiwu 职务,gongzi 工资,bmbianhao 部门编号 from yuangong where zhiwu in
(select distinct zhiwu from yuangong where bmbianhao=5) and bmbianhao!=5
–查询高于部门平均工资的员工姓名和工资,部门编号以及部门的平均工资
–首先得到每个部门的平均工资
select avg(gongzi) from yuangong group by bmbianhao
select xingming 姓名,gongzi 工资,yuangong.bmbianhao 部门编号,pjgz 平均工资 from yuangong,(select avg(gongzi) pjgz,bmbianhao from yuangong group by bmbianhao) gzb where gongzi>pjgz and gzb.bmbianhao=yuangong.bmbianhao
–分页查询
–查询第一个到第五个入职的员工
select top 5 xingming 姓名,zhiwu 职务,rzshijian 入职时间 from yuangong order by rzshijian
–查询第6个到第13个入职的员工
select top (13-6) xingming 姓名,zhiwu 职务,rzshijian 入职时间 from yuangong where ygbianhao not in (select top 5 ygbianhao from yuangong order by rzshijian) order by rzshijian
–内连接
select a.xingming 员工,b.xingming 领导 from yuangong a,yuangong b where a.shangji=b.ygbianhao
–左外连接
–左边表中的数据全部显示,右边的有则显示,没有就显示空
select a.xingming 员工,b.xingming 领导 from yuangong a left join yuangong b on a.shangji=b.ygbianhao

删除重复数据

–删除重复数据
create table xiaobaio
(bh int,
mc nvarchar(5))
insert into xiaobaio values (1,‘姓名1’)
insert into xiaobaio values (2,‘姓名2’)
select * from xiaobaio

select distinct * into lsb from xiaobaio
delete from xiaobaio
insert into xiaobaio select * from lsb
drop table lsb
select * from xiaobaio

约束

–约束 保证数据满足应有的条件
–约束分为 not null(非空) unique(唯一的) primary key foreign key check(自定义) default(默认)
create table biao
( dbbh int primary key,
dbxm varchar(20) unique,
dbmm varchar(20) not null,
nianling int check(nianling>=20 and nianling<=30) default 18,
bmbianhao int foreign key references bumen(bmbianhao) )

数据库分离

将数据库隐藏,防止误操作:
数据库分离:右键想要分离的数据库–任务–分离–直接点确定–打开数据库存储全路径–拷贝数据库(.mdf)和它的日志文件(_log)
数据库恢复:将数据可拷贝到它的存储全路径–右键数据库–附加–添加–确定
数据库备份:右键想要分离的数据库–任务–备份
数据库还原:右键数据库–还原数据库–找到备份路径(备份文件.bak)

数据库备份:backup database sss to disk=‘E:/sss.bak’
数据库还原:restore database sss from disk=‘E:/sss.bak’
数据库的存储路径:
C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页