SQl常用语句总结(持续更新……)
创建示例数据库
USE master;
GO
IF DB_ID (N'mytest') IS NOT NULL
DROP DATABASE mytest;
GO
CREATE DATABASE mytest;
GO
USE mytest;
GO
IF OBJECT_ID(N'dbo.Orders') IS NOT NULL
DROP TABLE dbo.Orders;
GO
CREATE TABLE dbo.Orders
(
ProductID INT NOT NULL,
MadeFrom CHAR(20),
Sales MONEY NOT NULL
);
删除数据库
use master -- 设置当前数据库为master,以便访问sysdatabases表
go
if exists(select * from sysdatabases where name='stuDB')
drop database stuDB
go
或者
IF DB_ID (N'mytest') IS NOT NULL
DROP DATABASE mytest;
创建数据表
标准语法
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
示例
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
删除表包含结构
Drop table tablename
视图
create view 视图名字
as
查询代码
demo:
create view view_user
as
select id from user
go
/*以上为创建一个视图,视图里面存储的是user表的ID列*/
/*删除视图*/
if exusts(select * from sysobjects where name='视图名字')/*如果数据库中存在这个视图,就删除该视图,查不查询随便你*/
drop view 视图名字 /*执行删除视图语法*/
复制代码
视图注意事项:注意事项:
1.一个视图可以嵌套另一个视图,但最后不要超过3层
2.视图定义中的select语句不能包括下列内容
*order by排序子句,除非在select语句中带有一个top子句
*into关键字
*引用临时表或表变量
存储过程
存储过程可以说是一个记录集吧,它是由一些T-SQL语句组成的代码块,这些T-SQL语句代码像一个方法一样实现一些功能(对单表或多表的增删改查),然后再给这个代码块取一个名字,在用到这个功能的时候调用他就行了。
存储过程的好处:
1.由于数据库执行动作时,是先编译后执行的。然而存储过程是一个编译过的代码块,所以执行效率要比T-SQL语句高。
2.一个存储过程在程序在网络中交互时可以替代大堆的T-SQL语句,所以也能降低网络的通信量,提高通信速率。
3.通过存储过程能够使没有权限的用户在控制之下间接地存取数据库,从而确保数据的安全。
示例:
复制代码
--查询存储过程
IF OBJECT_ID (N'PROC_SELECT_STUDENTS_COUNT', N'P') IS NOT NULL
DROP procedure PROC_SELECT_STUDENTS_COUNT;
GO
CREATE procedure PROC_SELECT_STUDENTS_COUNT
AS
SELECT COUNT(ID) FROM Students
GO
--执行
EXEC PROC_SELECT_STUDENTS_COUNT
复制代码
带参数
复制代码
--查询存储过程,根据城市查询总数
IF OBJECT_ID (N'PROC_SELECT_STUDENTS_BY_CITY_COUNT', N'P') IS NOT NULL
DROP procedure PROC_SELECT_STUDENTS_BY_CITY_COUNT;
GO
CREATE procedure PROC_SELECT_STUDENTS_BY_CITY_COUNT(@city nvarchar(50))
AS
SELECT COUNT(ID) FROM Students WHERE City=@city
GO
-- 执行
EXEC PROC_SELECT_STUDENTS_BY_CITY_COUNT N'Beijing'
复制代码
带通配符
复制代码
--3、查询姓氏为李的学生信息,含通配符
IF OBJECT_ID (N'PROC_SELECT_STUDENTS_BY_SURNNAME', N'P') IS NOT NULL
DROP procedure PROC_SELECT_STUDENTS_BY_SURNNAME;
GO
CREATE procedure PROC_SELECT_STUDENTS_BY_SURNNAME
@surnName nvarchar(20)='李%' --默认值
AS
SELECT ID,Name,Age FROM Students WHERE Name like @surnName
GO
--执行
EXEC PROC_SELECT_STUDENTS_BY_SURNNAME
EXEC PROC_SELECT_STUDENTS_BY_SURNNAME N'李%'
EXEC PROC_SELECT_STUDENTS_BY_SURNNAME N'%李%'
复制代码
带输出参数
复制代码
--根据姓名查询的学生信息,返回学生的城市及年龄
IF OBJECT_ID (N'PROC_SELECT_STUDENTS_BY_NAME', N'P') IS NOT NULL
DROP procedure PROC_SELECT_STUDENTS_BY_NAME;
GO
CREATE procedure PROC_SELECT_STUDENTS_BY_NAME
@name nvarchar(50), --输入参数
@city nvarchar(20) out, --输出参数
@age int output --输入输出参数
AS
SELECT @city=City,@age=Age FROM Students WHERE Name=@name AND Age=@age
GO
--执行
declare @name nvarchar(50),
@city nvarchar(20),
@age int;
set @name = N'李明';
set @age = 20;
exec PROC_SELECT_STUDENTS_BY_NAME @name,@city out, @age output;
select @city, @age;
复制代码
存储过程实现增删改
1、新增demo
复制代码
--1、存储过程:新增学生信息
IF OBJECT_ID (N'PROC_INSERT_STUDENT', N'P') IS NOT NULL
DROP procedure PROC_INSERT_STUDENT;
GO
CREATE procedure PROC_INSERT_STUDENT
@id int,
@name nvarchar(20),
@age int,
@city nvarchar(20)
AS
INSERT INTO Students(ID,Name,Age,City) VALUES(@id,@name,@age,@city)
GO
--执行
EXEC PROC_INSERT_STUDENT 1001,N'张三',19,'ShangHai'
复制代码
2、修改
复制代码
--修改学生信息
IF OBJECT_ID (N'PROC_UPDATE_STUDENT', N'P') IS NOT NULL
DROP procedure PROC_UPDATE_STUDENT;
GO
CREATE procedure PROC_UPDATE_STUDENT
@id int,
@name nvarchar(20),
@age int,
@city nvarchar(20)
AS
UPDATE Students SET Name=@name,Age=@age,City=@city WHERE ID=@id
GO
--执行
EXEC PROC_UPDATE_STUDENT 1001,N'张思',20,'ShangHai'
复制代码
3、删除
复制代码
--根据id删除记录
IF OBJECT_ID (N'PROC_DELETE_STUDENT_BY_ID', N'P') IS NOT NULL
DROP procedure PROC_DELETE_STUDENT_BY_ID;
GO
CREATE procedure PROC_DELETE_STUDENT_BY_ID
@id int
AS
DELETE FROM Students WHERE ID=@id
GO
--执行
EXEC PROC_DELETE_STUDENT_BY_ID 1001
复制代码
存储过程实现分页
复制代码
-- ROW_NUMBER分页查询
IF OBJECT_ID (N'PROC_SELECT_BY_PAGE', N'P') IS NOT NULL
DROP procedure PROC_SELECT_BY_PAGE;
GO
CREATE procedure PROC_SELECT_BY_PAGE
@startIndex int,
@endIndex int
AS
SELECT * FROM (SELECT ID,Name,Age,City,ROW_NUMBER() OVER(ORDER BY ID DESC) AS RowNumber FROM Students) AS Temp
WHERE Temp.RowNumber BETWEEN @startIndex AND @endIndex
GO
--执行
EXEC PROC_SELECT_BY_PAGE 1,10
--使用TOP分页
IF OBJECT_ID (N'PROC_SELECT_BY_PAGE_WITH_TOP', N'P') IS NOT NULL
DROP procedure PROC_SELECT_BY_PAGE_WITH_TOP;
GO
CREATE procedure PROC_SELECT_BY_PAGE_WITH_TOP
@pageIndex int,
@pageSize int
AS
SELECT TOP(@pageSize) * FROM Students
WHERE ID >=(SELECT MAX(ID) FROM (SELECT TOP(@pageSize*(@pageIndex-1) + 1) ID FROM Students ORDER BY ID) AS Temp)
GO
--执行
EXEC PROC_SELECT_BY_PAGE_WITH_TOP 1,2
我的博客文章地址:http://www.cnblogs.com/hgmyz/p/6755162.html
欢迎关注我的微信公众号:互联网碎片搜集