`

SQL-Advance

阅读更多
》打开外围配置代码

--让sqlserver可以执行'xp_cmdshell'
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

》调用Dos命令创建文件夹
exec xp_cmdshell 'mkdir(md) 本地磁盘:\\文件夹'
》调用Dos命令查看文件夹
exec xp_cmdshell 'dir 本地磁盘:\\文件夹'

》建立数据库

--在数据库(master)中检查要建立数据库是否存在
use master
go
if exists (select * from sysdatabases where name ='数据库名')
drop database 数据库名
--建立数据库
create database 数据库名

on primary --主数据文件
(
name= '数据库名_data', --数据库的物理名
fileName='本地磁盘:\\文件夹\数据库名_data.mdf',--数据的逻辑名
size = 3mb,--数据库起始大小
filegrowth = 10%--数据的增长率
)
,--次数据库文件
(
--数据库的物理名
name= '数据库名1_data',
--数据的逻辑名
fileName='本地磁盘:\\文件夹\数据库名_data.ndf',
--数据库起始大小
size = 3mb,
--数据的增长
filegrowth = 10%,
)
log on
(
name= '数据库名_log', --数据库的物理名
fileName='本地磁盘:\\文件夹\数据库名_data.ldf',--数据的逻辑名
size = 3mb,--数据库起始大小
filegrowth = 10%,--数据的增长率
)
go


--设置是否显示影响行数
set nocount on

--切换数据库
use 数据库名
GO
--在(sysobjects)中查找是否存在表(shuInfo)
if exists(select * from sysobjects where name = '表名')
drop table 表名

go
--建立表

create table 表名
(
--列名 数据类型 特征(如非空,约束)
stuName varchar(20) not null, -- 学生姓名 
stuNo varchar(20) not null, --学生学好可在后面直接建立约束  如(stuNo varchar(20) primary key check(stuNo like 's253__')  )
stuSex varchar(1) not null ,--学生性别
stuAge smallint not null, -- 年龄
stuSeat int identity(1,1) not null, -- 学生座位   可在后面直接建立约束  如(stuSeat int identity(1,1) )
stuAddress varchar(50) --学生住址 , 可在后面直接建立约束  如(stuAddress varchar(50)  default('住址不详') )
)
go
---建约束

--主键约束
alter table stuInfo
  add constraint pk_stuNo  primary key (stuNo)
--检查约束
alter table stuInfo
add constraint ck_stuAge check(stuAge between 15 and 40)
--默认约束
alter table stuInfo
add constraint df_stuAddress default('住址不详') for stuAddress
--唯一约束
alter table stuInfo
  add constraint uq_stuNo unique (stuNo)
--外键约束....假设stuMarks(学员成绩表)表中stuNo 引用 主表stuInfo表stuNo
alter table stuMarks
add constraint fk_stuNo foreign key (stuNo) references stuInfo(stuNo)

/*删除constraint语法*/
alter table stuInfo
  drop constraint <约束名称>
/* 建立用户*/
--1
use master
go
exec sp_addlogin '账号','密码' --sql用户 ,在系统数据库下建立
--2
use stuDB
go
exec sp_grantdbaccess '账号','数据库用户'--建立数据库用户,在要操作的数据库下建立
--3
grant 权限 on stuInfo to 数据库用户 --分贝权限


--删除登录用户
1-window exec sp_revokelogin
2-Sql    exec sp_droplogin
--删除数据库用户
revokedbaccess


--给表中添加新列:
alter table 表名
add 列名 数据类型
--删除列:
alter table 表名
drop column 列名

注意:查找的数值必须转换为字符串输出
convert(varchar(字符串长度),变量)

/*局部变量*/
--声明变量
declare @变量名 类型

--变量赋值
set @变量名 = 值 -- 静态

select @变量名=列 from  stuInfo where stuNo='S25300' -- 动态赋值(保证查询的结果只有一个值不然结果不准确)

/*全局变量*/
@@error  -- sql 的错误号码
@@identity --sql 最后插入的标识列
@@language --sql 当前语言
@@max_connections --sql 可以创建的最大连接
@@servername --sql 本地服务器的名称
@@servicename --该计算机上的服务名称
@@timeticks --当前计算机上的每刻度的微妙数
@@trancount -- 当前打开事物数
@@version --sql 版本信息

--变量叠加:
declare @sum int
declare @count int
set @sum=0
set @count=0
while(1=1)
begin
set @count=@count+1
set @sum=@sum+@count
if(@count>=10)
break
end
print 'sum:='+Convert(varchar(20),@sum)


/*if——else*/--条件结构
if (条件)
begin
--语句1
--语句2
end
else
begin
--语句1
--语句2
end


/*while循环*/
while(条件)
begin
--语句
end

/*case语句分支*/
case
when 条件1 then 结果1
when 条件2 then 结果2
when 条件3 then 结果3
else 结果
end

--事务的属性-ACID
》A—atomicity   原子性
》C—consistency 一致性
》I—isolation   隔离性         
》D—durability  永久性

调试(逐步):print 变量名(输出变量值)

--创建事务:
》开始事务-begin transaction
》提交事务-commit transaction
》回滚(撤销)事务-rollback transaction
--(sql中有三种事务 1.显示事务 2.隐士事务 3.自动事务)
--在t-sql开始前开启事务
begin transaction --开始事务

--使用@@error跟踪错误号码,判断是否有错误 如果有就回滚事务,没有则提交事务
commit transaction --提交事务
rollback transaction --回滚事务


set implicit_transactions on --开启隐士事务
set implicit_transactions off --关闭隐士事务

--唯一索引
create unique index_new on stuInfo(id)  with(fillfactor = 30)           --with为可选 其中fillfactor为填充因子数子为百分比
--非聚集索引
create nonclustered index_new on stuInfo (id) with (fillfactor = 30)--with为可选 其中fillfactor为填充因子数子为百分比
--聚集索引
create clustered index_new on stuInfo (id) with (fillfactor = 30)--with为可选 其中fillfactor为填充因子数子为百分比

》创建索引:
if exists(select * from sysindexes where name='ix_**)
drop index 表名.ix_**
create [unique][clustered|nonclustered]index index_name
on table _name (column_name[,column_name]...)
[with fillfactor=x]
--fillfactor:填充因子  X:为1-100的值
*****按索引查询
select * from 表名 with(index=索引名) [where<条件>]
*****创建视图:
if exists(select * from sysobjects where name='view_name')
drop view view_name
create view view_name  as   <select语句>
*****使用视图查询
select * from 视图名

/*存储方法*/ 可有返回值  返回值只能是int型数据且只有1个(返回值和输出参数的区别)
if exists (select *from sysobjects where name = 'proc_new')
drop procedure proc_new
go

create procedure proc_new
--输出参数(@count int=0 output, )
--输入参数(@stuName varchar(20) ,@stuid int )
as
--sql语句
go


exec proc_new  --传出参数是可显示传参 , 可隐士传参 ,output参数传入是后面跟上output-在调用前声明变量


分享到:
评论

相关推荐

    mybatis-plus-advance插件

    在`mybatis-plus-advance-master`这个压缩包中,可能包含了Mybatis-Plus-Advance的源码,供开发者研究和学习其内部实现。通过阅读源码,你可以更深入地理解这个插件的工作原理,以及如何根据自己的需求进行二次开发...

    SQL+Server数据库设计和高级查询(SQL+Advance)2_1

    在IT领域,数据库设计是构建高效、稳定的信息系统的关键环节,而SQL Server作为一款广泛应用的关系型数据库管理系统,其在数据库设计和高级查询方面具有丰富的功能。本章节将深入探讨SQL Server中的数据库创建、管理...

    advance sql and sql plus instructor guide

    advance sql and sql plus instructor guide

    java72-java-advance.rar

    a)java.sql b) java.util c) java.math d) java.io 2.不属于java.io包中的接口的一项是(C) a)DataInput b) DataOutput c) DataInputStream d) ObjectInput 3. ByteArrayOutputStream将下列哪一项作为输出流 C a)...

    SQL Server数据库设计和高级查询(SQL Advance).part2.rar

    ACCP 北大青鸟 5.0 S2 SQL Server数据库设计和高级查询(SQL Advance) 全部作业答案及PPT 包括案例、指导学习 资源太大,分为两部分,需要全部下载完才能解压缩。。。 这是第二部分~

    SQL Server advance

    • 了解设计数据库的基本步骤 • 熟练使用T-SQL实现建库、建表、加约束 • 掌握T-SQL编程,实现功能强大的查询 • 掌握创建索引、视图,快速访问数据库 • 掌握创建存储过程,实现复杂的业务规则

    北大青鸟 SQL Server数据库设计和高级查询(SQL Advance) 06

    在本课程"北大青鸟 SQL Server数据库设计和高级查询(SQL Advance) 06"中,我们将深入探讨SQL Server中的数据库设计原则以及高级查询技术。这一部分涵盖了数据库系统的关键概念,特别是针对T-SQL(Transact-SQL)的...

    Exam Ref 70-761 Querying Data with Transact-SQL

    Designed for experienced IT professionals ready to advance their status, Exam Ref focuses on the critical-thinking and decision-making acumen needed for success at the MCSA level. Focus on the ...

    北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part1.rar

    北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part1.rar 北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part1.rar

    北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part3.rar

    北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part3.rar 北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part3.rar

    北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part2.rar

    北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part2.rar 北大青鸟ACCP5.0 S2 SQL Server数据库设计和高级查询(SQL Advance).part2.rar

    精典oracle PL-SQL编程-最好教材

    Oracle PL/SQL编程是数据库开发领域中的核心技术,尤其在Oracle数据库系统中扮演着核心角色。本书《精典Oracle PL-SQL编程》被视为该领域的优秀教材,涵盖了PL/SQL的全面入门知识,旨在帮助读者深入理解和熟练掌握这...

    accp5.0 SQL Server数据库设计和高级查询(SQL Advance)

    《accp5.0 SQL Server数据库设计和高级查询(SQL Advance)》是一门深入探讨SQL Server在数据库设计与高级查询技术的课程。这门课程旨在帮助学员掌握SQL Server的核心功能,提升在实际工作中的数据处理能力。课程内容...

    ACCP5。0 SQL Server数据库设计和高级查询(SQL Advance)

    在IT领域,数据库设计与高级查询是至关重要的技能,尤其在使用像SQL Server这样的大型关系型数据库管理系统时。ACCP5.0课程专门针对这些关键主题进行深入探讨,旨在提升学员在数据库管理和数据处理方面的专业能力。...

    SQL Server数据库设计和高级查询(SQL Advance).part1.rar

    ACCP 北大青鸟 5.0 S2 SQL Server数据库设计和高级查询(SQL Advance) 全部作业答案及PPT 包括案例、指导学习 资源太大,分为两部分,需要全部下载完才能解压缩。。。 这是第一部分~

    SQL Server数据库设计和高级查询(SQL Advance)

    SQL Server作为一款广泛应用的关系型数据库管理系统,其设计和高级查询能力对于程序员来说至关重要。本文将深入探讨SQL Server数据库设计的基本原则以及如何进行复杂的查询操作。 首先,我们来了解数据库设计的基础...

    北大青鸟 ACCP5.0 S2 完整课件SQL Server数据库设计和高级查询(SQL Advance)

    ├─chapter1 │ ├─上机部分 │ │ ├─教学幻灯片 │ │ └─源代码 │ │ └─LG1 │ ├─教学部分 │ │ ├─补充案例 │ │ └─附加题答案 │ └─理论部分 │ └─教学幻灯片 ├─chapter2 ...

    course-advance-java:Java高级课程2天

    工作坊可能包括ORM(对象关系映射)概念,SQL查询优化,事务管理和连接池的配置。 8. **网关**:在微服务架构中,API网关作为客户端与后端服务之间的单一入口点。它负责路由请求,身份验证,限流,以及对跨服务的...

Global site tag (gtag.js) - Google Analytics