数据库知识


数据库的基本操作:

一、创建

1.1 创建数据库
create database 库名
1.2 切换数据库
use 库名
1.3 创建表
create table 表名(
		
		)

二、关于数据的类型

数据类型:int、float、numeric\decimal、char(n)/varchar(n)
money、date、datetime、binary(n)/varbinary(n)
bit ......

1.4 约束
主键:primary key   (不能重复,不能出现空值)
自增:identity(m,n) 整型列才可以用。 m代表自增开始的数 , n代表每次自增的数量
非空:not null
唯一:unique   不能重复,允许出现一个空值
默认值:default '值'
检查:check(条件表达式)
外键:foreign key references 表名(列名)

三、添加数据

		--自增列不需要指定数据;
		--非空列一定要指定数据;
		--在insert语句中使用default表示默认值
- - 1)最常用的:
insert into 表名 values(...),(...)
1.5 实例
insert into Department values('外联部')
insert into Department values('信息部'),('外交部')
- - 2)变化写法:可以指定列名
insert into Worker(wid,wname)
values('1004','赵六')
go
- - 3)用insert ....select语句添加数据(不能用default)
insert into Worker
select '1005','王小五','女','555555','119','2019-1-1',3  union
select '1006','王小六','女','444444','119','2019-10-15',3  union
select '1007','王小七','女','666666','119','2019-10-15',3
go
- - 4)select into语句:常用于备份
select *
into worker_new
from worker

四、查询数据

- - 1)
select * from product  --*表示所有列

--查部分列
select pid,pname,price from product

--查询给列取别名
select pid as 商品编号,pname as 商品名称,price as 价格
from product   --as可以省略

select 商品编号=pid,商品名称=pname,价格=price 
from product   -- =取别名时,别名在前
- - 2)distinct修饰列:去除重复数据
select distinct pstime from product
- - 3) top n 修饰列:查询前n条数据
select top 2 * from product

select top 30 percent * from product  --percent表示百分比

五、修改数据

		--update 表名 set 列名1=值1,列名2=值2
		--where 条件
- - 1) 实例
update Worker
set wsex='女',did=3
go
- - 2) 实例二:加入where条件
update Worker
set wsex='女',did=3
where age = 1000
go

六、删除数据

- - 1)
delete from Worker
where did=1  --删除编号为1的员工
go
- - 2) 关于删除主键表的数据
update Worker
set did=null
where did=2  --将部门编号为2的员工的部门清空
go

delete from Department
where did=2
go

思路:先将外键修改成null, 再删除主键表数据
- - 3) 附加知识:清空表 (不能加where)
truncate table Worker
go

七、group by 子句: 分组

   -- 常与聚合函数配合使用
   --就是分组后要进行统计
   	--如果有分组语句,select后只可以写两种列:
		--	1)用于分组的列
		--	2 ) 聚合函数的列
- - 1) AVG() :统计均价
--1)按照商品类型编号ptid进行分组,统计均价
select ptid 类型,AVG(price) 均价 from product
group by ptid
- - 2) COUNT(): 统计数量
select adid 产地编号,COUNT(*) 数量  from product
group by adid
go
- - 3) having子句:分组后的再次筛选,紧跟group by
select adid 产地编号,COUNT(*) 数量  from product
group by adid
having COUNT(*)>1  --数量大于1的组
go

注:having跟where不同,where用于分组前的筛选
- - 4) 实例演示
select ptid 类型,AVG(price) 均价 from product
where pid>3
group by ptid
having AVG(price)>5  --不能用别名
order by 均价 desc  --排序可以用别名
go

八、order by子句:排序,一定要写在最后面

- - 1) 默认
select * from product
order by price   --默认升序
go
-------------------------------
--也可以根据多个列进行排序
select * from product
order by price desc,pname desc
go

- - 2) 实例
select * from product
order by price desc  --desc表示降序,asc表示升序
go

九、where子句:筛选数据

实例
select * from product
where pname like '%旺%'  -- 包含'旺'关键字的商品
go

select * from product
where pid not in(1,3,5)  --不在(1,3,5)里面
go

十、聚合函数:用于统计的函数

- - 1)聚合函数
计数函数:COUNT(列名) 
求和函数:SUM(列名)
平均值函数:AVG(列名)
最大值函数:MAX(列名)
最小值:MIN(列名)    
   -- 注:不分大小写!

十一、运算符

赋值运算符:
--关系运算符
 	= 		  等于
	!=或者<>  都表示不等于
--逻辑运算符:and 、or、not   
	  check(age>=18 and age<=30)     and:与,并且
      check(gender='男' or gender='女')    or:或者,
      not null 非空        				not : 非,反的
      	like :像...一样
通配符:
   % :匹配0个或者多个任意字符
   		where name like '%小%'  --小明、黄小明、小时候、小
   _ :下划线,匹配一个任意字符
   		where username like '_abc'   --xabc、yabc  
   []:匹配[]里面出现的某一个字符
   		where username  like 'aa[xyz]'  -- aax、aay、aaz
		where username  like 'aa[^xyz]'  --aaa、aab、.. (匹配xyz以外的任意一个字符)
		where username like 'aa[0-9]'  --aa0 、aa1、aa2..

十二、多表连接

- - 1)内连接:innser join .... on
设有 StudentInfo表 与 ScoreInfo表 ,
--内连接:inner join ... on
--返回两张表匹配的数据
select A.StuCode,StuName, SubjectName,Score
from StudentInfo A
inner join ScoreInfo B
on A.StuCode=B.StuCode 
go
- - 2) 左连接: left join .... on
--左连接
--一般,在join左边的表为主表,也就是StudentInfo,从表为ScoreInfo
--即使数据不匹配,join左边的表中数据也将被保留,为主表,
--不满足匹配结果的结果集在相应列中添加null值
 select * from StudentInfo 
 		left join ScoreInfo      ---- 关键词:left
 		on coun = coun
- - 3) 右连接:right join .... on
 
 --右连接
 --相反,在join右边的表为主表,右边的表中数据将被保留,
  select * from StudentInfo 
 		right join ScoreInfo     -- 关键词:right 
 		on coun = coun
- - 4) 全外连接:full join ... on
 --全外连接
 --返回连接表中所有行的数据
 --在整个全外连接的返回结果中,包含了两个完全连接表的数据
    select * from StudentInfo 
 		full join ScoreInfo     -- 关键词:full 
 		on coun = coun
- - 5) 交叉连接: cross join ... on
 	--除此之外:还有交叉连接
 	-- 形成的数据是没有意思的,因为很多数据是不对的
-- 但是,它却是其它有意义的连接的内在基础  
	select * from emp, dept;
	select * from emp
  		[cross] join dept;

​ 十五

​ 初稿于2020/8/27写

posted @ 2020-08-28 10:29  十五十五  阅读(120)  评论(0)    收藏  举报