SQL编程集Word格式.docx

上传人:b****1 文档编号:4909577 上传时间:2023-05-04 格式:DOCX 页数:27 大小:25.54KB
下载 相关 举报
SQL编程集Word格式.docx_第1页
第1页 / 共27页
SQL编程集Word格式.docx_第2页
第2页 / 共27页
SQL编程集Word格式.docx_第3页
第3页 / 共27页
SQL编程集Word格式.docx_第4页
第4页 / 共27页
SQL编程集Word格式.docx_第5页
第5页 / 共27页
SQL编程集Word格式.docx_第6页
第6页 / 共27页
SQL编程集Word格式.docx_第7页
第7页 / 共27页
SQL编程集Word格式.docx_第8页
第8页 / 共27页
SQL编程集Word格式.docx_第9页
第9页 / 共27页
SQL编程集Word格式.docx_第10页
第10页 / 共27页
SQL编程集Word格式.docx_第11页
第11页 / 共27页
SQL编程集Word格式.docx_第12页
第12页 / 共27页
SQL编程集Word格式.docx_第13页
第13页 / 共27页
SQL编程集Word格式.docx_第14页
第14页 / 共27页
SQL编程集Word格式.docx_第15页
第15页 / 共27页
SQL编程集Word格式.docx_第16页
第16页 / 共27页
SQL编程集Word格式.docx_第17页
第17页 / 共27页
SQL编程集Word格式.docx_第18页
第18页 / 共27页
SQL编程集Word格式.docx_第19页
第19页 / 共27页
SQL编程集Word格式.docx_第20页
第20页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

SQL编程集Word格式.docx

《SQL编程集Word格式.docx》由会员分享,可在线阅读,更多相关《SQL编程集Word格式.docx(27页珍藏版)》请在冰点文库上搜索。

SQL编程集Word格式.docx

3.向文件组xangroup添加一个文件xan_love

alterdatabasexan-------指定更改的数据库xan

addfile(name=xan_love,--------添加文件xan_love内容

filename=’g:

\xan\xan_love.mdf’,

size=1mb,

maxsize=100mb,

filegrowth=10%)tofilegroupxangroup-----添加到指定的文件组xangroup

go

4.修改数据库xan的名为nax

alterdatabasexan----指定更改的数据库xan

modifyname=nax----更改名为nax

5.修改数据库文件xan_data起始大小为5MB

modifyfile(name=’xan_data’,---指定更改的数据库文件名xan_data

size=5mb)---更改新值为5MB

go-----注意:

每次只能更改一个文件的属性

6.删除文件组xangroup和其中的文件xan_love

removefilexan_love----删除文件xan_love

go

removefilegroupxangroup----删除文件组xangroup

go---注意:

删除文件组必须先删除其中的文件

7.删除数据库xan

dropdatabasexan----删除指定的数据库xan

数据库表

8.在数据库xan创建表为xantable

usexan----指定数据库xan

createtablexantable----创建表为xantable

(学号intnotnull,----添加列名为学号,数据类型为int型,不允许为空

姓名char(20),----添加列名为姓名,数据类型为字符型(长度为20),默认允许为空

性别bit,

年龄smaint,

专业char(20)default‘计算机’,)------添加列名为专业,默认值为’计算机’

9.为表xantable中添加数据信息

insertintoxantable(学号,姓名,性别,年龄)----向表xantable中添加你所需要填写的列名

values(‘00001,’竹子’,0,21)----填写对应的数据信息

10.给表xantable添加标识列xuhao(标识列相当于一个序列,可以自动添加数据)

altertablexantable----指定更改的表为xantable

addxuhaointidentity(1,1)notnull----添加列为xuhao,标识列的起始值为1,增量系数为1,不允许为空

11.添加全球唯一标识符aa(提供全球唯一性)

usexan-----指定数据库xan

altertablexantable-----指定修改的表为xantable

addaauniqueidentifierdefaultnewid()-----标识列为aa,默认为newid()[函数]

12.修改xantable中的竹子的年龄改为20

usexan----指定数据库为xan

updatexantable----修改表xantable中的数据信息

set年龄=20----选择年龄的列,并赋予新值

where姓名=’竹子’----选择需改数据信息的目标列姓名=’竹子’

13.altertable–修改表结构

useugv

altertabletach

add专业名char(10)----添加一列

--altercolumn姓名char(10)---修改一列

--dropcolumn部门---删除一列

14.删除xantable的所有记录,删除表xantable

usexan

deletefromxantable------------------默认删除所有,有日志可恢复

truncatetablexantable-------------表示删除表中全部记录,不会被日志文件所记录,是无法恢复,慎用!

droptablexantable------删除表

查询

15.查询xan库中xantable表所有内容

select*------*号代表所有

fromxantable

16.查询northwind库products表中指定列(productid、supplierid、birthdate、city)。

usenorthwind

selectproductid,supplierid,birthdate,city-------select后面跟多个列时用逗号隔开

fromproducts

17.查询northwind库products表中指定列(productid、supplierid、birthdate、city)中supplierid>

10的数据

selectproductid,supplierid,birthdate,city

Wheresupplierid>

10-----------------指定条件为supplierid>

10

18.查询products表中productname为tofu的记录

select*

whereproductname=’tofu’

19.查询products表中birthdate比1960-10-1大的

wherebirthdate>

’10-1-1960’

20.模糊查询like

select*

fromcustomers

wherecitylike‘c%’-----查询customers表中city中以c开头的数据

----wherecitynotlike‘c%’-----查询customers表中city中不含以c开头的数据

----wherecitylike‘a_’-----查询customers表中city中以c开头并且只有两个字符的数据

-----“%”号的使用:

%C%,表示包含C;

C%,表示C开头;

%C,表示C结尾

-----“_”的用法:

a_,表示以a开头并只有两个字符;

“_”能有多个,一个代表一个字符

21.and/or/not(优先级顺序为not,and,or)

select*fromproducts

whereproductnamelike‘%e’andunitprice<

40orproduct=73-----列出以e结尾且unitprice或者product=73的数据

22.between…and在…范围之内

whereunitpricebetween10and20--(大于等于10小于等于20)-----包含10和20

-------whereunitpricenotbetween10and20--(小于10大于20)----不包含10和20

23.In==or确定给定的值是否与查询或列表中的值相匹配

select*fromcustomers

wherecityin('

berlin'

'

london'

)-----查询city中’berlin’和’london’的数据

-------wherecitynotin('

)----查询city中除了’berlin’和’london’的数据

24.isnull查询为空的数据

selectcompanyname,fax

fromsuppliers

wherefaxisnull------查询fax为空的数据

-----wherefaxisnotnull----查询fax不为空的数据

25.orderby排序:

asc升(默认);

desc降

Selectproductname,categoryid,unitprice,supplierid

orderbycategoryidasc,unitpricedesc----第一列categoryid为主序,默认为asc,第二列unitprice为desc

26.distinct消除重复的记录

selectdistinctcountry------查询suppliers中消除country重复的记录

orderbycountry

27.AS别名(修改列名)

selectlastnameasa,firstnameb,city,lastname+firstnameasc—修改lastname为a,firstname为b,lastname+firstname为c

fromemployees

Go----------注意as可用空格代替,就如firstnameb一样.

selectlastnameasa,firstnameb,city,

‘我来补一列’asnokown------添加一列nokown并填充”我来补一列”

28.分组排列top限制返回到结果集中的行数

selecttop5orderid,productid,quantity----查询前5个orderid的数据(只有5行,超过的并列一样的舍去)

--selecttop5withtiesorderid,productid,quantity---查询前5个orderid的数据(只要前5个orderid的全部列出来,且与第5个并列的也要返回来)

from[orderdetails]

--orderbyquantitydesc----排序(优先级最高,先做排序),将quantity排成降序

用withties时必须要排序(也就是必须要与orderby连用)

 

聚集(合)函数

29.Avg平均值

selectavg(unitprice)--------求unitprice的平均值

30.count

usenorthwind

selectcount(*)-------count(*)统计表中有多少行

----selectcount(region)-------count(列名)统计列中非空的值行

31.max()/min()最大/最小

selectmax(productid)--------求productid的最大值

--selectmix(productid)-------求productid的最小值

32.Sum()求和

selectsum(productid)-----求productid的和

33.insert表名select------插入结果集

insertintocustomers

selectsubstring(firstname,1,3)+substring(lastname,1,2),lastname,firstname,title,address,city,region,postalcode,country,homephone,null

fromemployees------注意:

字符类型要一一对应,不足的用null补齐

34.groupby分组汇总排列

selectproductid,sum(quantity)as'

quantity的总和'

whereproductid<

groupbyproductid----分组汇总排列必须要有groupby----可以和groupby连用,但要放在groupby下面

---havingsum(quantity)>

=30

---orderbyproductid-----having必须与groupby联用;

groupby与聚集函数连用,但子句不能包含聚集函数.

----------注意:

groupby,orderby,having连用时顺序为groupby,having,orderby-----having与where的区别在于having后面可以跟聚合函数,而where不能

35.compute[--by]生成统计栏

selectorderid,productid,quantity

----orderbyproductiddesc----对productid进行降序排列

computesum(quantity)

----computesum(quantity)byproductid----必须与orderby连用

go------将quantity的所有的值合计出来,分栏显示出来

36.向customers插入结果集到employees中

insertcustomers

selectsubstring(firstname,1,3)+substring(lastname,1,2),

lastname,firstname,title,address,city,region,

postalcode,country,homephone,null---插入的列要与customers的数据类型一致,不够用null补齐

fromemployees

go----substring(取子符串,起始位,取几位)

37.select…into表名----将结果集生成新表(无日志操作)

selectorderidasproducts,freightasprice,shipcityastax

into#pree---#pree代表临时表pree

fromorders

38.将上下两张表中两列的内容合并到一起

selectemployeeid

union-----union消除重复值

----nuionall-----unionall不消除重复值

selectemployeeid----注意:

列名一定要相同

fromemployeeterritories

39.OSPL实用工具

在DOS命令行下输入:

osql-Uusernamepassword

-E本地登陆

表联接

联接查询

40.内联接innetjoin(交集)

selectproductid,suppliers.supplierid,suppliers.fax-----suppliers.supplierid代表suppliers表中的supplierid

fromproductsinnerjoinsuppliers----innerjoin代表联接products和suppliers这两张表(inner可以省略不写)

onproducts.supplierid=suppliers.supplierid-----指定两张表中的supplierid要一样

wheresuppliers.faxisnotnull----指定suppliers中的fax不为空

41.外联接leftouterjoin(左外连接)/rightouterjoin(右外连接)/fullouterjoin(全外连接)

usepubs

selecta.au_fname,a.au_lname,p.pub_name

fromauthorsaleftouterjoinpublishersp--leftouter为左外连接(outer可以不写),包括满足连接条件的行外,还包括左表的所有行

----rightouterjoin----rightouter为右外连接,包括满足连接条件的行外,还包括右表的所有行;

----fullouterjoin----fullouter为全外连接,结果集中除了包括满足连接条件的行外,还包括两个表的所有行;

ona.city=p.city

Go注意:

只连接两个表,因为显示的全部信息,所以不与where连用

42.多表查询(联接)------用逗号相隔

selectproductid,suppliers.supplierid,suppliers.fax

fromproducts,suppliers--------查询两张表,中间用逗号隔开

whereproducts.supplierid=suppliers.supplierid-----指定条件

Go-----注意:

select查询语句,不能算联接语句(不推荐)

43.crossjoin交叉连接(笛卡尔集)

selectpanyname,panyname

fromsupplierscrossjoinshippers

go----也就是将panyname和panyname的所有能组合的全部排列出来

44.自联接(特殊的内联接)

selectdistincta.type

fromtitlesajointitlesb------连接自己本身

ona.type=b.type

wherea.pub_id<

>

b.pub_id

go--------显示的是表中pub_id不同的type

45.子表查询

selectt.orderid,t.customerid

from(selectorderid,customeridfromorders)ast

go---------也就是将括号里面的内容生成了一张新表,然后在新表中查询

46.子列查询

selecttitle,price,

(selectavg(price)fromtitles)asaverage,

price-(selectavg(price)fromtitles)

asdifference

fromtitles

go------也就是将括号里面的内容生成一个新列

47.条件where子查询

selectorderid,customerid

fromordersor1

where20<

(selectquantityfrom[orderdetails]od

whereor1.orderid=od.orderidandod.productid=23)

go----查询的是orderdetails中的quantity>

20并且orders和orderdetails两张表中的orderid一样

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 成人教育 > 远程网络教育

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2