SQL基础知识回顾整理 清空后让表的id从1开始

20150929~20151016所学SQL基础知识回顾整理,后续完善补充

服务器名称:是指你要连接的安装的数据库服务器所在的那台电脑的ip地址,如果是本机的话,就是  . 

mdf 结尾:数据库数据文件,一个数据库有且只有一个

ldf:数据库日志文件,一个数据库有且至少有一个

数据库中存放数据的结构,是通过表的形式来存储的,一个数据库中有很多个表

基础知识(创建、使用数据库及创建表、添加数据、删除表)

约束

查询

子查询

表连接

视图

各类函数

存储过程

触发器

分页语句

事务

20150929

create database xuankeData--创建数据库
go--连接符

use xuankeData--使用数据库
go

create table student--创建表
(
    code int not null,--列名 类型 是否为空
    name varchar(20),
    sex int,
    height decimal(18,2)
)
go

insert into student values(2,'张三',1,1.85)--输入数据
go

select*from student--查询表中内容
go

drop table student--删除表
go

drop database xuankedata--删除数据库,先要点到别的数据库,被删数据库不能正在使用中
go

update student set name='张三' where code=2--修改表中内容, where 筛选
go

delete from student where name='李四'--删除表中的数据
go
方法1.
清空数据时不用delete from tbname
而是用truncate table tbname
方法2:
先清空数据delete from tbname
再重置自增种子dbcc checkident(tbname,reseed,0)

20150930

--约束:对某一列数据的限制

--主键约束:可以由一列或者两列组合都可以,

--必须是唯一的,不能为空的

--学号,外键,外键表
select*from student--查询表中内容
insert into student values(1,'张三',1,1.85,'32330123')--添加数据
insert into student values(2,'李四',1,1.85,'32330163')--添加数据

delete from student where code=2

create table score
(
   ids int primary key identity(1,1), --设置主键  identity(1,1)自增长列
   scode int references student(code),  --学号,外键,外键表
   course varchar(20),
   degree decimal(18,2) check(degree>=0 and degree<=100)
)

alter table score add cid varchar(50) unique--给表再加一列,===>修改哪个表,添加列
alter table score drop column cid  --修改表,删除列
insert into score values(2,'语文',99)
select *from score

 查询

--查询 msdn select  ,  msdn  like

20151009:
Select 语句的运行先后顺序:
1from     2on    3join    4where     5group  by
6with  cube  or  with  rollup  7having  8select   9distinct 
10order  by   11top

use mydb
select*from car
go
--条件查询:where筛选比较运算符>  <  >=  <=  !=

                     逻辑运算符and  or
select name,oil from car where oil <8  and powers>160
--select  列名  from 
select* from car where oil <8  and powers>160
--模糊查询 like
select* from car where name like '%宝马%'--%通配符
select top 2 *from car where name like '宝马%'--top查询前多少条
select* from car where name like '%宝马系%' and price <45
select* from car where name like '%宝马__%'--下划线_任何单一字
select* from car where name like '%宝马[1-4]_%'--在[]的指定范围内
select* from car where name like '%宝马[^1-4]_%'--^在指定的范围之外
--排序
select *from car order by oil asc --升序
select *from car order by oil desc --降序

select * from car where name like '%宝马%' order by oil desc

--去重显示
select distinct brand from car

子查询

--子查询
--把查询语句查询出的结果当做一个数值使用或一组数值使用
--all所有的,any任意一个,修饰符
--in('','')在数值范围内not in()不在括号的数值范围内
select price from car where name ='宝马3系 325i 时尚型'
select *from car where name like '%奥迪%'and price>=42.38
select *from car where name like '%奥迪%'and price>=(select price from car where name ='宝马3系 325i 时尚型')

select *from car where name like '%奥迪%'and price>(select max(price)from car where name like'%宝马%')
select *from car where name like '%奥迪%'and price>all(select price from car where name like '%宝马%' )
select *from car where name like '%奥迪%'and price>any(select price from car where name like '%宝马%' )

--in('','')在数值范围内not in()不在括号的数值范围内 == or的用法
select * from car where brand ='b001'or brand ='b002'or brand ='b003' 
select * from car where brand in('b001','b002','b003')
select * from car where brand not in('b001','b002','b003')

表连接

--表连接  (inner)join on内连接

select* from car
select*from brand 
select code,name,brand,brand_code  from car
 join brand on car.brand =brand .brand_code 
 select code,name,brand,brand_code  from car
 inner join brand on car.brand =brand .brand_code
 select code,name,brand,brand_code  from car
left join brand on car.brand =brand .brand_code
 select code,name,brand,brand_code  from car
 right join brand on car.brand =brand .brand_code
--子查询
select code,name,(select brand_name from brand where car.brand =brand .brand_code )as brand_name from car 
--表的拼接  用,笛卡尔积,类似for的穷举
select*from car,brand
select*from car,brand where car.brand =brand .brand_code

各类函数

--聚合函数:max  ,  min  , sum ,avg  ,count
select min(oil),max(oil),sum(price) from car--放在select from 中间使用,聚合函数和聚合函数放在一起使用
go
select COUNT(*)from car
go
select avg(price) from car
go
select avg (price) from car where name like '%宝马%'
go
--group by  --分组
select brand,MIN(oil) from car group by brand--按照哪一列进行分组,select  from 中间就只能查询哪一列
go
select brand from car group by brand having COUNT(*)>=3--having 只能跟在group by后面使用,对分组后的数据进行再筛选
go
select*from brand
go
--日期时间函数
select SYSDATETIME()--系统时间
go
select GETDATE()--比sysdatetime 获取的快速
go
print sysdatetime()--把数据输出到消息框
go
select YEAR(GETDATE())  
go
select DATENAME(WEEKDAY,'2015-5-5')
select DATEPART(WEEKDAY,'2015-5-5')

--字符串函数
select LTRIM('       123213     ')--去除左空格
select RTRIM('  asder         ')--去除右空格
select LEFT('abcdef',3)--返回从左边开始指定长度的字符
select RIGHT('123123adf',3)--返回从右边开始指定长度的字符
select LEN('afdfasfd')--返回字符串长度
select LOWER('AcDFdd')--小写
select upper('AcDFdd')--大写
select REPLACE('ac123123123ac123123123','ac','haha')--替换
select REPLICATE('abc',10)
select REVERSE('abc')--翻转字符串
select STR(1.567,3,2)--第一个是数值类型,是长度,是小数点后有几位
select SUBSTRING('abcdefg',2,3)--截取字符串,索引从开始

--数学函数:abs ceiling floor square sqrt round pi 
select ABS(-2.1)--绝对值(负数变正数)
select RAND()--随机数(返回一个介于-1之间的伪随机数)
select ROUND(4.1,0)--四舍五入(代表小数点后几位数)
select FLOOR(5.56)--下限
select CEILING(5.2)--上限(返回大于或等于制定数值表达式的最小数)
select PI()--圆周率
select SQRT(4)--开平方
select SQUARE(5)--平方

--转换函数
select CAST('123' as int)
select CONVERT(int,'123')
--字符串也是可以相加的
select '123'+'456'--结果是123456

视图

20151013:
--视图:就是一个虚拟的表
select degree,sname,cname from 
(select score.sno,score.cno,score.degree,
student.sname,ssex,sbrithday,class,course.cname,tno from score 
join student on student.sno = score.sno
join course on course.cno = score.cno) table1
--一个查询语句可以当做是子查询,查询出一列或者一个作为数据的比对参数,
--作为一个数据源,当做一个数值或一组数值使用,当放在from后面,还可作为一个表来使用

create view tableview--创建视图
as
 select score.sno,score.cno,score.degree,
student.sname,ssex,sbrithday,class,course.cname,tno from score 
join student on student.sno = score.sno
join course on course.cno = score.cno
go

select *from tableview
select DEGREE,sname,cname from tableview--在查询使用时就是一个表

alter view tableview--修改视图
as
 select *from student--修改的内容,相当于删了重建
go

drop view tableview--删除视图

--应用:如果这几个表的连接经常要用,那么建好视图之后就不用每次都写了

--范式理论:
--1、每个表都要有主键
--2、每个表的列都要直接跟主键相关
--3、每一列都要是单独的数据列

存储过程

20151015:
--存储过程
create proc JiaFa
--参数
@a int,
@b int 
as
    --存储过程内容
    declare @c int
    set @c=@a+@b
    return @c
go

declare @f int
exec @f=JiaFa 3,5
print @f
alter proc Zhishuhe @num int as declare @i int--定义起点从开始到输入的数挨个判断 declare @sum int set @i=1 set @sum=0 while @i<=@num--开始循环判断是否是质数 begin declare @j int--定义判断质数的起点,从到自身循环一遍,能被整除两次的就是质数 set @j=1 declare @count int set @count=0 while @j<=@i--判断@i是否为质数 begin if @i%@j=0 begin set @count=@count+1 end set @j=@j+1 end if @count=2--如果是质数 begin set @sum=@sum+@i--累加求和 end set @i=@i+1 end return @sum go declare @jieguo int exec @jieguo = zhishuhe 100 print @jieguo

触发器

20151016:
--触发器:一种特殊的存储过程,通过对数据库表操作的
--动作,来触发,增删改

alter trigger Fruit_Insert_After  --修改用alter
on fruit  --对哪个表操作的时候执行触发器
instead of insert -- for  --(update,delete) for的意思是动作之后触发after等同
as
    select *from inserted  --inserted临时表,就是增加的数据
go

select *from student
select *from score

delete from student where sno=101--有主外键关系

alter trigger student_delete_instead
on student
instead of delete
as
    --先删除外键表数据
    delete from score where sno in( select sno from deleted )
    --再删主键表
    delete from student where sno in (select sno from deleted)
Go

分页语句

--分页语句:查询-10的数据(尽量用主键唯一查询)
select top 5 *from car order by price desc
go
select top 5 *from car where code not in (select top 5 code from car)
go
select top 5 * from car where code not in (select top 10 code from car)
go
use shujuData
go
select * from score
go
select top 5 a.sno,a.cno,a.degree from Score as a
left join (select top 5 *from Score) as b on a.sno=b.sno and a.cno=b.cno
where b.sno is null--判断是否为空is

事务

--事务:出现错误时,会返回最开始的过程;
--保障流程的完整执行,如果一步失败就会返回到起点

select *From student
select *From score

begin tran --在流程开始的位置

    delete from student where sno='304'
    delete from Student where Sno='303'

if @@ERROR>0--看全局变量里是否有错误记录
begin
    rollback tran--回滚事务
end
else
begin
    commit tran--提交事务
end

 数据库表设计后不允许保存更改

在修改 SQL Server 2012 数据库的表设计后,保存表设计时,出现如下错误提示:

不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了“阻止保存要求重新创建表的更改”选项。

解决方法

打开“工具”->“选项”,展开“设计器”->“表设计器和数据库设计器”,

去掉“阻止保存要求重新创建表的更改”前面的勾。

SQL基础知识回顾整理
清空后让表的id从1开始