导航

MSSQL 基础

Posted on 2013-09-24 16:11  超申  阅读(199)  评论(0)    收藏  举报
 

SQL分类:

DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)

DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)

DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

 

在sql server management studio 中查询语句添加注释方式:

-- words

/** words **/ 

 

创建表:

create table dbo.test

( 
Id int identity(1,1) primary key,
Name varchar(50) not null,
StartTime datetime,
Balance money null ,
AvailableHours int default 0,
Invoices Decimal (12,4) default 0,
Picture image null 

)

 

增加一列:

alter table test_table add  Descrip_column varchar(50) null;

删除列:

alter table test_table drop column Descrip_column

 

表复制语句:

 

      1.INSERT INTO SELECT

 

      Insert into table_b(field1,field2,...) select value1,value2,... from table_a

 

      要求目标表Table2必须存在。

 

      2.SELECT INTO FROM

      语句形式为:SELECT value1, value2 into table_b from  table_a

 

      要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中

 

 

创建索引(非聚集) :

create nonclustered index index_name on testTable(column name)

删除索引:

drop index index_name on testTable

 

创建视图

create view view_name as select * from test_table

删除视图:

drop view view_name

 

集合运算:

EXCEPT 和 INTERSECT

比较两个查询的结果,返回非重复值。                       

EXCEPT 从左查询中返回右查询没有找到的所有非重复值(EXCEPT ALL 返回所有值)。                       

INTERSECT 返回 INTERSECT 操作数左右两边的两个查询都返回的所有非重复值(INTERSECT ALL 返回所有值)。                       

以下是将使用 EXCEPT 或 INTERSECT 的两个查询的结果集组合起来的基本规则:                       

  • 所有查询中的列数和列的顺序必须相同。                               

  • 数据类型必须兼容。

 

USE AdventureWorks2012;
GO
SELECT ProductID 
FROM Production.Product
INTERSECT
SELECT ProductID 
FROM Production.WorkOrder


USE AdventureWorks2012;
GO
SELECT ProductID 
FROM Production.Product
EXCEPT
SELECT ProductID 
FROM Production.WorkOrder
View Code


UNION

将两个或更多个查询的结果合并为单个结果集,该结果集包含联合查询中的所有查询的全部行。              UNION 运算不同于使用联接合并两个表中的列的运算。

下面列出了使用 UNION 合并两个查询结果集的基本规则:                       

  • 所有查询中的列数和列的顺序必须相同。                               

  • 数据类型必须兼容。

USE AdventureWorks2012;
GO
IF OBJECT_ID ('dbo.ProductResults', 'U') IS NOT NULL
DROP TABLE dbo.ProductResults;
GO
IF OBJECT_ID ('dbo.Gloves', 'U') IS NOT NULL
DROP TABLE dbo.Gloves;
GO
-- Create Gloves table.
SELECT ProductModelID, Name
INTO dbo.Gloves
FROM Production.ProductModel
WHERE ProductModelID IN (3, 4);
GO

USE AdventureWorks2012;
GO
SELECT ProductModelID, Name
INTO dbo.ProductResults
FROM Production.ProductModel
WHERE ProductModelID NOT IN (3, 4)
UNION
SELECT ProductModelID, Name
FROM dbo.Gloves;
GO

SELECT ProductModelID, Name 
FROM dbo.ProductResults;
View Code

 

 

常用的SQL 语句查询:

1,子查询

select a,b,c from table_a where a (not) IN (select d from b ) 或者: select a,b,c from table_a where a IN (4,5,12)

 2,在线视图查询

select * from (SELECT a,b,c FROM table_a) as T where T.a  == 1;

3,between的用法,between限制查询数据范围时包括了边界值,not between不包括

4, 两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

(可以转化为in 或者 Not in 查询,效率会好点)

5, 选择从10到15的记录

SELECT  top 5 *
FROM Production.ProductModel
where ProductModelID not in 
(select top 10 ProductModelID from Production.ProductModel order by ProductModelID asc)
View Code

 或者

select top 5 * 
from (select top 15 * from Production.ProductModel order by ProductModelID asc) as T
order by ProductModelID desc
View Code

 

待续...