ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:22.98KB ,
资源ID:12410825      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/12410825.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(SQL语句大全.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

SQL语句大全.docx

1、SQL语句大全SQL语句大全创建数据库创建之前判断该数据库是否存在if exists (select * from sysdatabases where name=databaseName) drop database databaseNamegoCreate DATABASE databasename删除数据库drop database databasename备份sql server- 创建备份数据的 deviceUSE masterEXEC sp_addumpdevice disk, testBack, c:mssql7backupMyNwind_1.dat- 开始备份BACKUP DA

2、TABASE pubs TO testBack创建新表create table tabname(col1 type1 not null primary key,col2 type2 not null,.)根据已有的表创建新表:A:go use 原数据库名go select * into 目的数据库名.dbo.目的表名 from 原表名(使用旧表创建新表)B:create table tab_new as select col1,col2 from tab_old definition only创建序列create sequence SIMON_SEQUENCE minvalue 1 - 最小值

3、 maxvalue 999999999999999999999999999 - 最大值start with 1 - 开始值increment by 1 - 每次加几cache 20;删除新表drop table tabname增加一个列Alter table tabname add colname coltype删除一个列Alter table tabname drop column colname添加主键Alter table tabname add primary key(col)说明:删除主键:Alter table tabname drop primary key(col)创建索引cr

4、eate unique index idxname on tabname(col。)删除索引:drop index idxname on tabname注:索引是不可更改的,想更改必须删除重新建。创建视图create view viewname as select statement删除视图:drop view viewname简单基本的sql语句(1) 数据记录筛选:sql=select * from 数据表 where 字段名=字段值 order by 字段名 descsql=select * from 数据表 where 字段名 like %字段值% order by 字段名 descs

5、ql=select top 10 * from 数据表 where 字段名=字段值 order by 字段名 descsql=select top 10 * from 数据表 order by 字段名 descsql=select * from 数据表 where 字段名 in (值1,值2,值3)sql=select * from 数据表 where 字段名 between 值1 and 值2(2) 更新数据记录:sql=update 数据表 set 字段名=字段值 where 条件表达式sql=update 数据表 set 字段1=值1,字段2=值2 字段n=值n where 条件表达式(

6、3) 删除数据记录:sql=delete from 数据表 where 条件表达式 sql=delete from 数据表 (将数据表所有记录删除)(4) 添加数据记录:sql=insert into 数据表 (字段1,字段2,字段3 ) values (值1,值2,值3 )sql=insert into 目标数据表 select * from 源数据表 (把源数据表的记录添加到目标数据表)(5) 数据记录统计函数:AVG(字段名) 得出一个表格栏平均值COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计MAX(字段名) 取得一个表格栏最大的值MIN(字段名) 取得一个表格栏

7、最小的值SUM(字段名) 把数据栏的值相加引用以上函数的方法:sql=select sum(字段名) as 别名 from 数据表 where 条件表达式set rs=conn.excute(sql)用 rs(别名) 获取统计的值,其它函数运用同上。查询去除重复值:select distinct * from table1(5) 数据表的建立和删除:CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) )(6) 单列求和:SELECT SUM(字段名) FROM 数据表编辑本段最新sql语句查询数据库中含有同一这字段的表:select name from sys

8、objects where xtype = u and id in(select id from syscolumns where name = s3)根据出生日期可以算出年龄:select datediff(year,scrq,2013) as 年龄 from page_shsjgrgl根据当前年份自动算出年龄select datediff(year,csny,cast(YEAR(GETDATE() as char) 年select year(djsj) from page_shsjgrgl月select month(djsj) from page_shsjgrgl日select day(d

9、jsj) from page_shsjgrgl在同一数据库中复制表结构:select * into a from b where 11当 IDENTITY_INSERT 设置为 OFF 时,不能为表 aa 中的标识列插入显式值。set identity_insert aa ON-设置打开,批量插入:insert into aa(Customer_ID, ID_Type, ID_Number) select Customer_ID, ID_Type, ID_Number from TCustomer;set identity_insert aa OFF关闭不同数据库之间的复制:复制结构:sele

10、ct * into test.dbo.b from GCRT.dbo.page_shsjgrgl where 11复制内容:insert into test.dbo.b(xm,ssdq) select xm,ssdq from GCRT.dbo.page_shsjgrgl 查看数据库中所有的数据表表名:select name from SysObjects where type=u查看数据库中所有表含有同一字段的表:select name from sysobjects where xtype = u and id in(select id from syscolumns where name

11、 = 同一字段)查看数据表中的所有字段:select name from Syscolumns where id=object_id(表名)查询数据库时随机10条记录:select top 10 * from td_areacode order by newid()修改字段类型:ALTER TABLE 表名 ALTER COLUMN 字段名 varchar(30) NOT NULL use ZHJIANGJGYLdeclare temp nvarchar(30)set temp = ZWI4select hllx from page_yljg_zyry where hllx not in(se

12、lect case temp when then else b1 end from (select * from TD_Code where page_en=page_yljg_zyry and B2=ZWI) s where s.b1 != case temp when then else temp end)更改数据库表字段类型:alter table page_shsjgrgl alter column s1 int几个高级查询运算词A:UNION 运算符UNION 运算符通过组合其他两个结果表(例如TABLE1 和TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随UNI

13、ON 一起使用时(即UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自TABLE1 就是来自TABLE2。B: EXCEPT 运算符EXCEPT 运算符通过包括所有在TABLE1 中但不在TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随EXCEPT 一起使用时(EXCEPT ALL),不消除重复行。C:INTERSECT 运算符INTERSECT 运算符通过只包括TABLE1 和TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当ALL 随INTERSECT 一起使用时(INTERSECT ALL),不消除重复行。注:使用运算词的几个查询结果行

14、必须是一致的。使用外连接A、left outer join:左外连接(左连接):结果集既包括连接表的匹配行,也包括左连接表的所有行。SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.cB:right outer join:右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。C:full outer join:全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。编辑本段判断对象是否存在判断数据库是否存在if exists (select* from sysd

15、atabases where name= 数据库名) dropdatabase数据库名判断表是否存在if not exists (select * from sysobjects where name = 表名 and xtype=U)begin-这里创建表end判断存储过程是否存在if exists (select* from sysobjects whereid = object_id(N存储过程名) and OBJECTPROPERTY(id, NIsProcedure) = 1) dropprocedure存储过程名判断临时表是否存在if object_id(tempdb.#临时表名)

16、 isnot null droptable#临时表名判断视图是否存在-SQL Server 2000IF EXISTS (SELECT* FROM sysviews WHEREobject_id = dbo.视图名-SQL Server 2005IF EXISTS (SELECT* FROM sys.views WHERE object_id = dbo.视图名判断函数是否存在if exists (select* from dbo.sysobjects where id = object_id(Ndbo.函数名) and xtype in (NFN, NIF, NTF) dropfunctio

17、ndbo.函数名获取创建信息SELECTname,id,crdate FROM sysobjects where xtype=U /* xtype 的表示参数类型,通常包括如下这些 C = CHECK约束 D = 默认值或DEFAULT约束 F = FOREIGNKEY约束 L = 日志 FN = 标量函数 IF = 内嵌表函数 P = 存储过程 PK = PRIMARYKEY约束(类型是K) RF = 复制筛选存储过程 S = 系统表 TF = 表函数 TR = 触发器U = 用户表 UQ = UNIQUE约束(类型是K) V = 视图 X = 扩展存储过程 */判断列是否存在if exis

18、ts(select* from syscolumns whereid=object_id(表名) and name=列名) altertable表名dropcolumn列名判断列是否自增列if columnproperty(object_id(table),col,IsIdentity)=1 print 自增列elseprint 不是自增列 SELECT* FROM sys.columns WHEREobject_id=OBJECT_ID(表名) AND is_identity=1判断表中是否存在索引if exists(select* from sysindexes whereid=objec

19、t_id(表名) and name=索引名) print 存在 elseprint 不存在查看数据库中对象SELECT* FROM sysobjects WHEREname=对象名select * from table(所要查询的表名) where coloum(条件)编辑本段提升复制表(只复制结构,源表名:a 新表名:b) (Access可用)法一:select * into b from a where 11法二:select top 0 * into b from a拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)insert into b(x, y, z) selec

20、t d,e,f from a;跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)insert into b(x, y, z) select d,e,f from a in 具体数据库 where 条件例子:。.from b in &Server.MapPath(.&data.mdb & where.子查询(表名1:a 表名2:b)select a,b,c from a where a IN (select d from b 或者: select a,b,c from a where a IN (1,2,3)显示文章最后时间select a.title,a.username,b.

21、adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b外连接查询(表名1:a 表名2:b)select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c在线视图查询(表名1:aselect * from (Select a,b,c FROM a) T where t.a 1;between的用法between为查询某字段的指定范围,限制查询数据范围时包括了边界值,not between不包括边

22、界值select * from table1 where time between time1 and time2select a,b,c, from table1 where a not between 数值1 and 数值2in 的使用方法select * from table1 where a not in (值1,值2,值4,值6)删除主表没有的信息两张关联表delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1四表联查问题select * from a l

23、eft inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .日程安排提前五分钟SQL: select * from 日程安排 where datediff(minute,f开始时间,getdate()5一条sql 搞定数据库页select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段= a.主键字段 order by a.排序字段前10条记录sel

24、ect top 10 * from table1 where 范围选择排名选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等。)select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)派生结果表包括所有在TableA 中但不在TableB和TableC 中的行并消除所有重复行而派生出一个结果表(select a from tableA except (select a from tableB)

25、 except (select a from tableC)随机取出10条数据select top 10 * from tablename order by newid()随机选择记录select newid()删除重复记录Delete from tablename where id not in (select max(id) from tablename group by col1,col2,.)列出数据库里的表名select name from sysobjects where type=U列出表里的所有的select name from syscolumns where id=obje

26、ct_id(TableName)列示排列列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。select type,sum(case vender when A then pcs else 0 end),sum(case vender when C then pcs else 0 end),sum(case vender when B then pcs else 0 end) FROM tablename group by type显示结果:type vender pcs电脑A 1电脑A 1光盘B 2光盘A 2手机B 3手

27、机C 3初始化表table1TRUNCATE TABLE table1选择从10到15的记录select top 5 * from (select top10 * from (select top 15 * from table order by id asc) table_别名 order by id desc) table_2 order by id数据类型转换declare numid intdeclare id varchar(50)set numid=2005set id=convert(varchar,numid)通过上述语句完成数据类型Int转换成varchar,其他转换类似,可

28、参看convert函数编辑本段技巧1=1,1=2的使用在SQL语句组合时用的较多“where 1=1”是表示选择全部 “where 1=2”全部不选,如:if strWhere !=beginset strSQL = select count(*) as Total from + tblName + where + strWhereendelsebeginset strSQL = select count(*) as Total from + tblName + end我们可以直接写成set strSQL = select count(*) as Total from + tblName +

29、where 1=1 and + strWhere收缩数据库-重建索引DBCC REINDEXDBCC INDEXDEFRAG-收缩数据和日志DBCC SHRINKDBDBCC SHRINKFILE压缩数据库dbcc shrinkdatabase(dbname)转移数据库给新用户以已存在用户权限exec sp_change_users_login update_one,newname,oldnamego检查备份集RESTORE VERIFYONLY from disk=E:dvbbs.bak修复数据库Alter DATABASE dvbbs SET SINGLE_USERGODBCC CHECK

30、DB(dvbbs,repair_allow_data_loss) WITH TABLOCKGOAlter DATABASE dvbbs SET MULTI_USERGO日志清除SET NOCOUNT ONDECLARE LogicalFileName sysname,MaxMinutes INT,NewSize INTUSE tablename - 要操作的数据库名Select LogicalFileName = tablename_log, - 日志文件名MaxMinutes = 10, - Limit on time allowed to wrap log.NewSize = 1 - 你想设定的日志文件的大小(M)- Setup / initializeDECLARE Ori

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1