




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
name varchar(10),value int)你的sql应该这么写(用存储过程):create proc p_ins_alm_msgmsg_count int outasmsg_countif rowcount = 0return -1000insert into table1 (field0)values(msg_count)return 0go在sql server中如何使用sql语句修改表中的字段名称?a. 重命名表下例将表 customers 重命名为 custs。exec sp_rename customers, custsb. 重命名列下例将表 customers 中的列 contact title 重命名为 title。exec sp_rename customers.contact title, title, column怎么那么不喜欢查看联机帮助呢怎么用sql语句 在sql2008中的表中 添加数据字段 而且不是空字段 添加字段:alert tablename add 字段名 属性添加带默认值的字段alert tablename add 字段名 属性 default 值例如:alert mytable add name varchar(20) default wangmin篇二:用sql语句表与字段的基本操作、数据库备份等用sql语句添加删除修改字段1. 增加字段alter table docdsp add dspcode char(200)2. 删除字段alter table table_name drop column column_name3. 修改字段类型alter table table_name alter column column_name new_data_type4. sp_rename 改名更改当前数据库中用户创建对象(如表、列或用户定义数据类型)的名称。语法sp_rename objname = object_name , newname = new_name , objtype = object_type 如:exec sp_rename newname,partstock5. sp_help 显示表的一些基本情况sp_help object_name 如:exec sp_help partstock6. 判断某一表partstock中字段partvelocity是否存在if exists (select * from syscolumns where id=object_id(partstock) and name=partvelocity)print partvelocity existselse print partvelocity not exists另法:判断表的存在性:select count(*) from sysobjects where type=u and name=你的表名判断字段的存在性:select count(*) from syscolumnswhere id = (select id from sysobjects where type=u and name=你的表名)and name = 你要判断的字段名一个小例子-假设要处理的表名为: tb-判断要添加列的表中是否有主键if exists(select 1 from sysobjects where parent_obj=object_id(tb) and xtype=pk)beginprint 表中已经有主键,列只能做为普通列添加-添加int类型的列,默认值为0alter table tb add 列名 int default 0endelsebeginprint 表中无主键,添加主键列-添加int类型的列,默认值为0alter table tb add 列名 int primary key default 0end7. 随机读取若干条记录access语法:select top 10 * from 表名 order by rnd(id)sql server:select top n * from 表名 order by newid()mysql select * from 表名 order by rand() limit n8. 说明:日程安排提前五分钟提醒sql: select * from 日程安排 where datediff(minute,f开始时间,getdate()>59. 前10条记录select top 10 * form table1 where 范围10. 包括所有在 tablea 中但不在 tableb和tablec 中的行并消除所有重复行而派生出一个结果表(select a from tablea ) except (select a from tableb) except (select a from tablec)11. 说明:随机取出10条数据select top 10 * from tablename order by newid()12. 列出数据库里所有的表名select name from sysobjects where type=u13. 列出表里的所有的字段名select name from syscolumns where id=object_id(tablename)14. 说明:列示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 type15. 说明:初始化表table1truncate table table116. 说明:几个高级查询运算词a: union 运算符union 运算符通过组合其他两个结果表(例如 table1 和 table2)并消去表中任何重复行而派生出一个结果表。当 all 随 union 一起使用时(即 union all),不消除重复行。两种情况下,派生表的每一行不是来自 table1 就是来自 table2。b: except 运算符except 运算符通过包括所有在 table1 中但不在 table2 中的行并消除所有重复行而派生出一个结果表。当 all 随 except 一起使用时 (except all),不消除重复行。c: intersect 运算符intersect 运算符通过只包括 table1 和 table2 中都有的行并消除所有重复行而派生出一个结果表。当 all 随 intersect 一起使用时 (intersect all),不消除重复行。注:使用运算词的几个查询结果行必须是一致的。17. 说明:在线视图查询(表名1:a )select * from (select a,b,c from a) t where t.a > 1;18. 说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括select * from table1 where time between time1 and time2select a,b,c, from table1 where a not between 数值1 and 数值219. 说明:in 的使用方法select * from table1 where a not in (值1,值2,值4,值6)20. 说明:两张关联表,删除主表中已经在副表中没有的信息delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )21. 说明:复制表(只复制结构,源表名:a 新表名:b) (access可用)法一:select * into b from a where 1<>1法二:select top 0 * into b from a22. 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (access可用)insert into b(a, b, c) select d,e,f from b;23. 说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (access可用)insert into b(a, b, c) select d,e,f from b in 具体数据库 where 条件例子:.from b in &server.mappath(.)&data.mdb & where.24. 创建数据库create database database-name25. 说明:删除数据库drop database dbname26. 说明:备份sql server- 创建 备份数据的 deviceuse masterexec sp_addumpdevice disk, testback, c:mssql7backupmynwind_1.dat- 开始 备份backup database pubs to testback27. 说明:创建新表create table tabname(col1 type1 not null primary key,col2 type2 not null,.)根据已有的表创建新表:a:create table tab_new like tab_old (使用旧表创建新表)b:create table tab_new as select col1,col2 from tab_old definition only28. 说明:删除新表:drop table tabname29. 说明:增加一个列:alter table tabname add column col type注:列增加后将不能删除。db2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。30. 说明:添加主键:alter table tabname add primary key(col)说明:删除主键:alter table tabname drop primary key(col)31. 说明:创建索引:create unique index idxname on tabname(col.)删除索引:drop index idxname注:索引是不可更改的,想更改必须删除重新建。32. 说明:创建视图:create view viewname as select statement删除视图:drop view viewname33. 说明:几个简单的基本的sql语句选择:select * from table1 where 范围插入:insert into table1(field1,field2) values(value1,value2)删除:delete from table1 where 范围更新:update table1 set field1=value1 where 范围查找:select * from table1 where field1 like %value1% -like的语法很精妙,查资料!排序:select * from table1 order by field1,field2 desc总数:select count * as totalcount from table1求和:select sum(field1) as sumvalue from table1平均:select avg(field1) as avgvalue from table1最大:select max(field1) as maxvalue from table1最小:select min(field1) as minvalue from table134. 数据库备份:use db_ndmspmasterdb;godeclare path varchar(500)set path=d:ndm_datadb_ndmspmasterdb+convert(varchar, getdate(), 105)+.bak select pathbackup database db_ndmspmasterdbto disk = pathwith format,medianame = z_sqlserverbackups,name = full backup of db_ndmspmasterdb;go篇三:sql语句(有答案)一. sql语句【1】(c226)下列关于sql语言特点的描述中,错误的是( )。a) 语言非常简洁b)是一种一体化语言c)是一种高度过程化的语 d)可以直接以命令方式交互使用,也可以程序方式使用【2】1103(7)负责数据库中查询操作的数据库语言是a) 数据定义语言 b)数据管理语言 c)数据操作语言 d)数据控制语言【3】y3(18)sql语言的核心是( )。a) 数据操纵 b)数据定义 c)数据查询 d)数据定义【4】y1(24)visualfoxpro在sql方面,不支持的功能是( )。a) 数据控制 b)数据操纵 c)数据查询 d)数据定义【5】y4(30)下列选项中,不属于sql特殊运算符的是( )。a) group b)on c)all d)empty二. sql查询语句【1】0904(12)sql语句的查询语句是a) insert b)update c) delete d) select【2】(c126)标准的sql基本查询语句的格式是( )。a) select?from?where b)select?where?from c)select?where?group by d)select?from?order by【3】y3 (25)sql语句中,select语句中的join是用来建立表间的联系短语应放在下列哪个短语之后( )。a) fromb)where c)on d)group by【4】y2(20)sql语句中,select命令中的join是用来建立表间的联系短语,连接条件应出现在下列哪个短语中a) whereb)on c)havingd)inner【5】y4(17)sql select语句中的where用于说明( )。a) 查询数据 b)查询条件 c)查询分组 d)查询排序【6】y4(18)sql语句可以进行多个查询的嵌套,但visual foxpro中只支持( )层嵌套。a) 1 b)2 c)3 d)无穷【7】(c128)在visual foxpro中,嵌套查询是基于( )的查询a) 2个关系 b)3个关系 c)多个关系 d)2个或3个关系【8】0704 (17)以下有关select语句的叙述中错误的是a) select语句中可以使用别名 b)select语句中只能包含表中的列及其构成的表达式c)select语句规定了结果集中的顺序d)如果from短语引用的两个表有同名的列,则select短语引用它们时必须使用表名前缀加以限定【9】0504 (31) 在visual foxpro中,以下有关sql的select语句的叙述中,错误的是 ( )。a) select子句中可以包含表中的列和表达式b)select子句中可以使用别名c)select子句规定了结果集中的列顺序d)select子句中列的顺序应该与表中列的顺序一致【10】0904(10) 在visual foxpro中,select语句能够实现投影、选择和【 】三种专门的关系运算。【11】0909(14) 学生表中有“学号”、“姓名”和“年龄”三个字段,下面sql语句完成的操作称为select 学号 from 学生a) 选择 b) 投影 c) 连接 d) 并【17】y1(29)在sql语句中,distinct短语的作用是( )。a) 对查询结果进行分组b)消除重复出现的查询记录c)按条件显示部分查询记录 d)删除查询结果中符合条件的记录【18】y5(14)设有学生表xs(学号,课程号,成绩),用sql语句检索每个学生的成绩总和的语句是:select 学号,sum(成绩) from xs【19】0709(7)在sql的select查询中,having字句不可以单独使用,总是跟在【 】子句之后一起使用。【20】0504(32)下列关于sql中having子句的描述,错误的是( )。a) having子句必须与group by子句同时使用b)having子句与group by子句无关c)使用where子句的同时可以使用having子句d)使用having子句的作用是限定分组的条件【21】0704(19)在select语句中,以下有关having语句的正确叙述是a) having短语必须与group by短语同时使用 b) 使用having短语的同时不能使用where短语c) having短语可以在任意的一个位置出现 d) having短语与where短语功能相同【22】0704(15) 0904(24)sql的select语句中,“having<条件表达式>”用来筛选满足条件的a) 列 b) 行 c) 关系 d) 分组和成绩4个字段。请将下列sql语句补充完整。select学号,sum(成绩) from 成绩表 where成绩>=60 group by 学号_count(*)>=3【26】0804(13)在select语句中使用order by是为了指定a查询的表 b查询结果的顺序 c查询的条件 d查询的字段【27】0809(18) (y727)在sql select查询中,为了使查询结果排序应该使用短语a) asc b) desc c) group by d) order by【28】0609(19)0909(27)在sql select语句的order by短语中如果指定了多个字段,则_。a) 无法进行排序 b)只按第一个字段排序 c)按从左至右优先依次排序 d)按字段排序优先级依次排序【29】y5(29)sql用于显示部分查询结果的top短语,必须与下列哪个短语同时使用才有效( )。 a)havingb)distinct c)order by d)group by【30】1103(9)sql语句“select top 10 percent * from 订单 order by 金额 desc”的查询结果是订单中金额【 】的10%的定单信息。【31】0704(11)y6(13) “歌手”表中有“歌手号”、“姓名”、和“最后得分”三个字段,“最后得分”越高名次越靠前,查询前10名歌手的sql语句是:select *【 】from 歌手 order by 最后得分【 】。【32】1009(33)假设所有的选课成绩都已确定,显示“101”号课程成绩中最高的10%的记录信息,正确的sql命令是a) select * top 10 from 选课 order by 成绩 desc where课程号“101”b) select * percent 10 from 选课 order by 成绩 desc where课程号“101”c) select * top 10 percent from 选课 order by 成绩 where课程号“101”d) select * top 10 percent from 选课 order by 成绩desc where课程号“101”【33】y4(19)在成绩表中要求按“物理”降序排列,并查询前两名的学生姓名,正确的命令是( )。a) select 姓名 top 2 from 成绩表 where 物理 descb) select 姓名 top 2 from 成绩表 for 物理 descc) select 姓名 top 2 from 成绩表 group by 物理 descd) select 姓名 top 2 from 成绩表 order by 物理 desc【34】y1(28) 下列对sql的嵌套查询排序的描述中,说法正确的是( )。a) 既能对外层查询排序,也能对内层查询排序 b)只能对外层查询排序,不能对内层查询排序c)只能对内层查询排序,不能对外层查询排序 d)既不能对外层查询排序,也不能对内层查询排序【35】y3(20)下列短语中,与排序无关的短语是a) asc b)desc c)group by d)order by【36】0609(9)在sql select语句中为了将查询结果存储到永久表应该使用【 】短语。【37】y4(12)检索学生信息表中“籍贯”为“海南”的学生记录,将结果保存到表xx中,sql语句为:select * from 学生信息表 wher e籍贯=”海南”_xx【38】0809(21)sql的select语句中,与into table等价的短语是a) into dbf b) to table c) into form d) into file【39】0809(9) 0509(11) y2(29)在sql delete语句中为了将查询结果存储到临时表中应该使用【 】短语。【40】0709(26)在sql select 语句中为了将查询结果存储到临时表应该使用短语a) to cursor b)into cursorc)into dbf d)to dbf【41】(c115)检索学生成绩表中,总分在600分以上的学生记录,将结果保存到临时文件score中,sql语句为:select * from 学生成绩表 where总分>=600_score【42】1003 (32) 0909(31) 0904(32)查询“读者”表的所有记录并存储于临时表文件one中的sql语句是a) select* from读者into cursor one b)select* from读者to cursor onec)select* from读者into cursor dbf one d)select* from读者to cursor dbf one【43】0909(30)与“select * from 教师表 into dbf a”等价的语句是a) select * from 教师表to dbf a b)select * from教师表to table ac)select * from 教师表into table a d)select * from 教师表into a【44】y1(13)检索学生表中“性别”为“男”的学生记录,将结果保存到文本文件xb中,sql语句为:select * from 学生表 where 性别=“男”_xb【45】0809(6)select * from student【 】file student命令将查询结果存储在student.txt文本文件中。【46】y5(13)将sql的查询结果如果要追加到文本文件的尾部,应使用短语加以说明,【50】0609(10)在sql语句中空值用 【 】表示。【51】y6(30)sql语句中进行空值运算时,需要使用到的短语是( )。a) null b)=null c)is null d)is not null【52】0709(6)如下命令查询雇员表中“部门号“字段为空值的记录select * from 雇员where部门号【 】 。【53】y3(12)设有学生表文件,要查找学生表中还没有输入姓名的记录,则sql语句为: select * from 学生表 where 姓名_【54】0804(11)在sql语句中要查询表s在age字段上取空值的记录,正确的sql语句为:select * from s where 【 】 。【55】0904(34) 查询有选课记录,但没有考试成绩的学生的学号和课程号,正确的sql语句是a) select 学号,课程号from sc where成绩=“”b) select 学号,课程号from sc where成绩=nullc) select 学号,课程号from sc where成绩 is nulld) select 学号,课程号from sc where成绩【56】y4(28)检索尚未确定的供应商的订单号,正确的命令是( )。a) select * from 订购单 where 供应商号 null b)select * from 订购单 where 供应商号=nullc)select * from 订购单 where 供应商号 is null d)select * from 订购单 where 供应商号 is not null【57】(y712)在sql语句的select中,字符串匹配运算符用表示,可用来表示0个或多个字符。【58】0804(8)在sql的where子句的条件表达式中,字符串匹配(模糊查询)的运算符是_。【59】y5(15)检索当前表“学生”表中,全部姓“王”的学生记录,sql语句为:select * from 学生 where 姓名“王%”【60】y1(14)查询“学生成绩”表中所有不是姓“李”的学生记录,完成下列sql语句:select*from学生成绩where姓名_“李”【61】0904(15)设有sc(学号,课程号,成绩)表,下面sql的select语句检索成绩高于或等于平均成绩的学生的学号select 学号 from sc where 成绩 >=( select【 】from sc)【62】1003 (12)在sql语言中,用于对查询结果计数的函数是【 】【63】y1(10)设有学生表(姓名,班级名称),用sql语句检索每个班级的学生总人数的语句是:select 班级名称,_ as 人数 from 学生表 group by 班级名称【64】y3(15)利用sql语句统计选修了“日语”课程的学生人数请将下列语句补充完整select_from 选课表 where 课程名=“日语”【65】1009(13)将“学生”表中学号左4位为“2010” 的记录存储到新表new中的命令是:select * from 学生 where【13】=“2010”【14】dbf new。【66】0609(33)与:select distinct 歌手号from歌手where最后得分all(select 最后得分from;歌手where substr(歌手号,1,1)=“2”)等价的sql语句是_。a)select distinct歌手号from歌手where最后得分=;(select max(最后得分)from歌手where substr (歌手号,1,1)=“2”)b)select distinct歌手号from歌手where最后得分=;(select min(最后得分)from歌手where substr (歌手号,1,1)=“2”)c)select distinct歌手号from歌手where最后得分=;any(select max(最后得分)from歌手where substr (歌手号,1,1)=“2”)d)select distinct歌手号from歌手where最后得分=;some(select max(最后得分)from歌手where substr (歌手号,1,1)=“2”)【67】y3(19)在成绩表中,查找物理分数最高的学生记录,下列sql语句的空白处应填入的是( )select * from 成绩表where 物理>=_(select 物理 from 成绩表)a) some b)exists c)any d)all【68】y4(14)设有职工表文件,在职工表中查找奖金最高的职工记录,完成下列sql语句。select * from 职工表 where奖金>=_(select奖金from职工表)【69】(c112)嵌套查询命令中的_,相当于集合运算符号。【70】y2(19)当前目录下有xuesh.dbf和chji.dbf两个表文件,要求查找同时选修了课程号为“9801”和“9802”的学生姓名,下列sql语句的空白处应填入的语句为( )。select 姓名 from xuesh,chji where xuesh学号=chji学号and 课程号=“9801”and 姓名_;(select 姓名 from xuesh,chji where xuesh学号=chji学号 and 课程号=9802)a) inb)exists c)like d)all【71】(c215)sql语句:select * from 仓库表 where not exists (select * from 职工表 where 仓库号=仓库.仓库号)该语句等价于:select
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论