数据库常用知识
--是注释的意思
--新建数据库
create database shuguo
--建表的同时建字段
create table renwu
( bianhao int,
xingming nvarchar(5),
xingbie nchar(1),
zhiwu nvarchar(20),
nianning int,
gongzi numeric(8,2)
)
--选中哪执行哪
--删除整个表
drop table renwu
--查询语句
--搜索显示中添加小名或者别名
select bianhao 编号,gongzi 工资 from renwu
select *fromrenwu
--添加纪录
insert into renwu values(1,'刘备','男','主公',40,9999)
insert into renwu(bianhao,xingming,xingbie,zhiwu) values(2,'甘夫人','女','夫人')--删除了年龄,工资添加部分数据
insert into renwu values(3,'诸葛亮','男','军师',30,8000)
insert into renwu values(4,'关羽','男','将军',37,7000)
insert into renwu values(5,'张飞','男','将军',35,6000)
insert into renwu values(5,'赵云','男','将军',45,7000)
--删除表中全部记录
delete from renwu
--删除一条记录
delete from renwu where bianhao=2
--按条件查询
select bianhao,xingming,gongzi from renwu where gongzi>7000
--按条件删除
delete from renwu where xingming='甘夫人'
--修改记录
--将工资低于八千的加上%
update renwu set gongzi=gongzi*1.02 where gongzi<8000
--数据类型
--字符型:char 、vachar nchar、nvchar
-- (char varchar变长字符串) 在字符串长度固定的情况下用char,因为char速度更快。(用于英文)
-- nchar与nvchar是在前者的基础上采用了unicode编写。(用于中文,日文等)
-- unicode编写的特点是字节与汉字占用的空间一样。
-- 固定长度:char nchar
-- 可变长度:varchar nvchar
--数值类型:
-- bit(字节,不常用) int(数值在十亿) bigint(长int类型) float(浮点型,不常用) numeric(浮点型,更加精确好控制位数)
-- timestamp(不常用)
-- datetime_______getdate()得到当前时间
create table aaa(
shijian datetime
)
insert into aaa values(getdate())
select * fromaaa
--图片类型image与视频类型binary一般都放在专门的服务器上,因为占用资源。只有当需要安全时才直接放入数据,而且要下。
--主键(表中不可重复,性质不冲突,如ID。但是有地域等限制。)
create table gsyg
( bianhao int primary key,
xingming nvarchar(10),
nianling int)
insert into gsyg values(1,'悟空',30)
insert into gsyg values(2,'八戒',26)
insert into gsyg(bianhao,xingming) values(3,'沙僧')
insert into gsyg values(4,'沙僧',26)
--查询
select *fromgsyg
delete from gsyg
drop table gsyg
--修改部分字段
update gsyg set nianling=26 where nianling=26
update gsyg set xingming='唐僧' where bianhao=3
update gsyg set xingming='悟空' where nianling is null
--修改
delete from gsyg where nianling=22 or xingling='悟空'
delete from gsyg where nianling=26 and xingming='悟空'
--外键必须指向另一个表的主键并且类型要一致
create database shuihu
create table bumen
( bianhao int primary key,
mingcheng nvarchar(20),
didian nvarchar(20) )
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 *frombumen
create table renwu
(
paihang int primarykey,
xingming nvarchar(20),
zhiwu nvarchar(20),
shangji int,
ruzhishijian datetime,
gongzi numeric(6,1),
buzhu numeric(5,1),--加小数点一共位,最多上千
bianhao int foreignkey references bumen(bianhao) )
--外键int foreign key references 连接到部门表的编号主键
insert into renwu(paihang,xingming,zhiwu,ruzhishijian,gongzi,buzhu,bianhao)
values(101,'宋江','寨主','2017-03-15',20000,3000,1)
insert into renwu values (102,'卢俊义','寨主',101,'2017-03-16',18000,3000,1)
insert into renwu values (103,'吴用','军师',101,'2017-03-17',17000,2800,2)
insert into renwu values (104,'公孙胜','军师',103,'2017-03-18',16000,2600,2)
insert into renwu values (105,'关胜','大将',102,'2017-03-19',15000,2300,3)
insert into renwu(paihang,xingming,zhiwu,shangji,ruzhishijian,gongzi,bianhao)
values(106,'徐宁','小将',105,'2017-03-20',14000,3)
insert into renwu values (107,'鲁智深','大将',102,'2017-03-21',13000,2000,4)
insert into renwu values (108,'武松','大将',107,'2017-03-22',12000,1800,4)
insert into renwu(paihang,xingming,zhiwu,shangji,ruzhishijian,gongzi,bianhao)
values(109,'李逵','小将',107,'2017-03-23',10000,4)
insert into renwu values (110,'李俊','大将',102,'2017-03-23',13000,3000,5)
insert into renwu(paihang,xingming,zhiwu,shangji,ruzhishijian,gongzi,bianhao)
values(111,'阮小二','小将',110,'2017-03-24',13000,5)
insert into renwu(paihang,xingming,zhiwu,shangji,ruzhishijian,gongzi,bianhao)
values(112,'阮小七','小将',110,'2017-03-25',12000,5)
insert into renwu values (113,'柴进','总管',101,'2017-03-26',11000,1000,6)
insert into renwu values (114,'李英','总管',113,'2017-03-27',10000,600,6)
insert into renwu(paihang,xingming,zhiwu,shangji,ruzhishijian,gongzi,bianhao)
values(115,'时迁','小头目',113,'2017-03-28',8000,6)
delete from renwu where xingming='宋江'
select *fromrenwu
select *frombumen
--查询()<*少用>
--鲁智深的工资、工作和所在部门编号
select xingming,gongzi,zhiwu,bianhao from renwu where xingming='鲁智深'
--查询一共有多少个部门(distinct除去重复的编号)
select distinct bianhao from renwu
--显示每个员工的年薪并且把字段以中文方式显示
select xingming'姓名',gongzi*12'年工资' from renwu
--查询实例(1)
--计算年总收入 和空值做四则运算(isnull判断后面是不是null,是用代替) null什么都没有,不分配内存地址
select xingming'姓名',gongzi*12+ISNULL(buzhu,0)*12'总收入' from renwu
--查询工资在-03-20以后入职的员工
select xingming'姓名',ruzhishijian'入职时间' from renwu where ruzhishijian>'2017-03-20'
--查询工资在到之间的员工
select xingming'姓名',gongzi'工资'from renwu where gongzi>13000 and gongzi<18000
--(模糊查询)查询姓李的员工 查询第二个字是进的员工%是无线字符
select xingming 姓名 from renwuwhere xingming like'李%'
select xingming 姓名 from renwuwhere xingming like'_俊%'
--批量查询
select xingming 姓名,paihang 排行 from renwuwhere paihang in(101,105,108,114)
--显示一把手的姓名和职务
select xingming 姓名,zhiwu 职务 from renwuwhere shangji is null
--排序 默认升序 desc是降序 中文排序按照拼音和音序
select xingming 姓名, gongzi 工资 from renwu orderby gongzi desc
select xingming 姓名 from renwuorder by xingming
--查询实例(2)
--按部门编号升序,按工资降序
select xingming 姓名,bianhao 编号,gongzi 工资 from renwuorder by bianhao,gongzi desc
--用起临时名的方法算年总收入并降序排序
select xingming 姓名,gongzi*12+ISNULL(buzhu,0)*12 年总收入 from renwuorder by gongzi*12+ISNULL(buzhu,0) desc
select xingming 姓名,gongzi*12+ISNULL(buzhu,0)*12 年总收入 from renwuorder by 年总收入 desc
--查询(4)
--显示最高工资和最低工资
select MAX(gongzi) from renwu
select MIN(gongzi) from renwu
--显示工资和平均工资
select SUM(gongzi) 总工资, avg(gongzi) 平均工资 from renwu
--给最大工资加上姓名工资
select xingming 姓名,gongzi 工资 from renwuwhere gongzi=(select min(gongzi) from renwu)
--显示高于平均工资的员工和工资,带有函数的要有()包含起来
select xingming 姓名,gongzi 工资 from renwuwhere gongzi>(select AVG(gongzi) from renwu)
select xingming 姓名,gongzi 工资,(select AVG(gongzi) from renwu) 平均工资 from renwu wheregongzi>(select AVG(gongzi) from renwu)
--查询(3)
--统计有多少记录
select COUNT(*) 统计有多少记录 from renwu
select COUNT(*) 统计工资大于记录条数 from renwu wheregongzi>10000
--统计每一个部门的平均工资和总工资 group by 后面的字段必须出现在显示字段中
select bianhao 编号,AVG(gongzi) 平均工资,SUM(gongzi) 总工资 from renwugroup by bianhao
--显示每个部门每个职务的平均工资和最低工资
select bianhao,zhiwu,AVG(gongzi),MIN(gongzi),zhiwu from renwu group by bianhao,zhiwu order by bianhao
--显示平均工资低于的部门编号和平均工资 排序一定在最后
select bianhao,AVG(gongzi) from renwu group by bianhao having AVG(gongzi)<15000
--多表查询
--笛卡尔集现象
select * fromrenwu,bumen
select *fromrenwu,bumen where bumen.mingcheng='水军'
select *fromrenwu,bumen where bumen.mingcheng='水军' and renwu.bianhao=bumen.bianhao
select *fromrenwu,bumen where renwu.bianhao=bumen.bianhao
--显示姓名和所在部门及部门编号
select xingming,mingcheng,bumen.bianhao from bumen,renwu where renwu.bianhao=bumen.bianhao
--显示部门号为的部门名称、职员姓名和工资
select xingming,mingcheng,gongzi,bumen.bianhao from bumen,renwu where bumen.bianhao=4
--复杂查询(1)
--显示员工、部门名和工资,并按部门编号排序
select xingming,mingcheng,gongzi,renwu.bianhao from renwu,bumen where renwu.bianhao=bumen.bianhao order by bumen.bianhao desc
--显示李逵的上级
select shangji from renwu where xingming='李逵'
select xingming 姓名 from renwuwhere paihang=(select shangji from renwu where xingming='李逵')
--显示所有员工及其上级
select a.xingming 姓名,b.xingming 上级 from renwu a,renwu b where a.shangji=b.paihang
--复杂查询(2)
--子查询(查询嵌套)单行子查询:返回结果为单行的子查询称为单行子查询
--显示与鲁智深同部门的员工
select xingming 姓名 from renwuwhere bianhao=(select bianhao from renwu where xingming='鲁智深')
--多行子查询:返回结果为多行的子查询称为多行子查询
--返回和号部门相同职务的员工姓名、工资和部门编号
--多行子查询如果不能一次写对,就两行写 先写子查询在主查询
--distinct 去除相同记录
select xingming,gongzi,bianhao,zhiwu from renwu where zhiwu in
(select distinct zhiwu from renwu where bianhao=5) and(bianhao!=5)
--分页查询
--显示高于部门平均工资的员工姓名和工资(同一个部门之间员工比较)、部门编号以及部门的平均工资
--首先得到每个部门的平均工资
select AVG(gongzi),bianhao from renwu group by bianhao
select xingming,gongzi,pjgz,renwu.bianhao from renwu,(select AVG(gongzi) pjgz,bianhao from renwu group by bianhao) lsb
where (renwu.bianhao=lsb.bianhao) and(renwu.gongzi>lsb.pjgz)
--分页查询
--显示第个到第个入职的员工
select top 5 xingming,zhiwu,ruzhishijian from renwu order by ruzhishijian
--显示第个到第个入职的员工(子查询里面只有一个表达式)
select top 8 xingming,ruzhishijian,zhiwu,paihang from renwu where paihang not in
(select top 5 ruzhishijian from renwu order by ruzhishijian)order by ruzhishijian
--外连接
--删除重复的记录
create table xiaobiao(
xbbh int,
xbxm varchar(10)
)
select *fromxiaobiao
insert into xiaobiao values(1,'xb1')
insert into xiaobiao values(2,'xb2')
select distinct *into lsb from xiaobiao
select *fromlsb
delete from xiaobiao
insert into xiaobiao select *from lsb
drop table lsb
--内连接与外连接
select a.xingming 姓名, b.xingming 上级 from renwu a, renwu b where a.shangji=b.paihang
--左外连接与右外连接(a中全部数据显示,对比上一个头没有上级)
select a.xingming 姓名, b.xingming 上级 from renwu aleft join renwu b on a.shangji=b.paihang
select a.xingming 姓名, b.xingming 上级 from renwu aright join renwu b on a.shangji=b.paihang
--约束:保证数据满足应有的条件
--约束分为not null unique(唯一的)primarykey foreign key check
create table biao(
bbh int primarykey,
bxm varchar(20) not null,
bmm varchar(20) not null,
nianling int )
select *frombiao
drop table biao
--错误示范
insert into biao(bxm,bmm) values ('贾宝玉','jiabaoyu') --错误
insert into biao(bxm,nianling) values('贾宝玉',20)--错误
create table dabiao
( dbbh int,
dbxm varchar(20),
nianling int check (nianling>=20 and nianling<=30 ) )
select *fromdabiao
drop table dabiao
insert into dabiao values (1,'贾宝玉',25 ) --年龄是就会报错
select distinct * from dabiao
--default 默认 在不写数据的情况下采用已定义好的默认值default
create table abiao(
abbh int primarykey,
nianling int check(nianling>=18 and nianling<=80) default 50
)
select *fromabiao
drop table abiao
insert into abiao(abbh) values(1001)
insert into abiao values(1002,25)
insert into abiao values(1003,50)
--数据库的备份(1)(鼠标备份)
--鼠标备份:、分离附加(异地工作)点击sss右键————任务————分离————确定
--还原数据库 点击数据库—————附加————添加————找到附加的数据库(放在安装目录下的 .mdf文件)————确定
create database sss
drop database sss
--鼠标备份:、备份 点击sss右键—————任务————备份————该一个好找的路径————文件名 .bak(备份文件)
--还原数据库 点击数据库—————还原
--数据库的备份(2)(命令行备份)
create database sss1
--备份
backup database sss1 to disk='E:\backup\sss1.bak'
drop database sss1
--还原
restore database sss1 from disk='E:\backup\sss1.bak'