您的位置:首页 > 数据库

SQL新手养成日记

2017-06-26 23:14 363 查看
基本的数据库

databases

软件名称 management studio

数据的查询 编辑 添加

ER图

 dbo.EDM

table和table之间有关系

叫做关系型数据库管理系统(RDBMS)

SQL 叫做 C扣

关系型数据库的基本要素是二维表

行,列

主键(PK),外键(FK)

主键不能为空

什么事T SQL结构型的查询语言

用处:通过数据来回答商业运营中的实际问题从而帮助公司企业决策者做出更

好的决策

-----------------------

SELECT(DISTINCT)

FROM

WHERE 

GROUP BY

HAVING

UNION

ORDER BY

select<table fields list>

*表示表格中的所有数据

尽量少用*

desc倒序排列 asc正序排列(可以省略)。

可以用select Top 100 * from [prodcution].[product]也可以不用加框框。

选中一部分select productID,Name,ProductNumber,Color,Size,ListPrice 

from prodcution.product

orde
4000
r by Listprice desc,Name

解释:进行两次排列,一次按照Listprice倒序排列,一次按照Name正序排列。

select productID,Name,ProductNumber,Color,Size,ListPrice from 

prodcution.product

order by 2

解释:按照我们选出的第二个Name进行正序的排列

--------------------------

isnull

null代表空值,但是我们不想在数据中呈现

方法:

select productID,Name,ProductNumber,isnull(Color,''),isnull

(Size,''),ListPrice from prodcution.product

-------------------------------

as

给自己表头FK重命名

select productID,Name,ProductNumber,isnull(Color,'') as Color,

isnull(Size,'') Size123,ListPrice 

from prodcution.product

ps:备注方法:- -东西*************

select BusinessEntityID,rate from HumanResources.EmployeePayHistory 

select BusinessEntityID,

rate*40*52 as AnnualSalary,

round(rate*40*52,1) as AnnualSalary,

round(rate*40*52,0) as AnnualSalary

from HumanResources.EmployeePayHistory

备注 1,0表示 四舍五入的位置 ,但是所有的依旧显示两位小数点。

------------------------

where

select * from sale.salesorderheader

where salespersonID=275

select * from sale.salesorderheader

where TotalDue>5000

select * from sale.salesorderheader

where SalesOrderNumber='so43670'

ps:int整型,不用加‘’,String字符型,需要加‘’。

select * from sale.salesorderheader

where salespersonID=275 and TotalDue>5000

select * from sale.salesorderheader

where salespersonID=275 and TotalDue>5000 and Orderdate between'2005

-08-01' and '1/1/2006'

ps:这两种时间的表示方式都是可以的

select * from sale.salesorderheader

where salespersonID=275 and TotalDue>5000 and Orderdate >= '2005-08-

01' and Orderdate < '1/1/2006'

对于字符串的引用

select * from sale.salesorderheader

where name = 'Mountain-100 Silver,38'

类似于超级查找的方式

select * from sale.salesorderheader

where name like '%Mountain%'  这个前后%,表示mountain前面也可以有东西

,后面也可以有东西。

搜索mountain前面没有东西的模糊查找

select * from sale.salesorderheader

where name like 'Mountain%'

-的使用

select * from sale.salesorderheader

where name like '_ountain%'

表示_里面我不知道是什么单词了。

------------------------------------

in的使用

select * from Producrion.product

where size in('60','61','62')

选择size里面 是60,61,62东西的那个字符。

为什么用字符呢,因为这里的size格式是char  具体问题 具体分析。

----------------

not in

----------------

distinct只选择一次。

进行快速的查询

where size not in('60')

-----------------

select * from Producrion.product

where size is null

select * from Producrion.product

where size is not null***经常使用。

select * from Producrion.product

where color = 'white'or color='black'

select * from Producrion.product

where color = 'white'and color='black'

select * from Producrion.product

where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql 数据库