项目开发中常用到的SQL语句

项目开发中常用到的SQL语句
项目开发中常用到的SQL语句

项目开发中常用到的SQL语句1、循环示例

循环示例代码:

?

1 2 3 4 5 6 7 8 9

10

11

12

13

14

15

16

17

18 DECLARE @i int

DECLARE @name varchar(10)

DECLARE @password varchar(10)

Set @i = 1000

WHILE @i < 1200

BEGIN

Set @i =@i +1

SET @name = RIGHT('0000' + CAST(@i AS varchar(10)),4)

set @password = @name

select @name

insert into dbo.LocomotiveTeminalBase

(li_ID,t_ID,lt_IDNumber,lt_MiM,lt_FuWQIP,lt_FuWQDKH,lt_CreatedBy)

values('d82575c0-2d21-4c47-a406-7771d7d2c80a','fb5d9a7b-9cd6-4a55-9e90-881706eaf @name,@password,'192.168.1.187','2000','9015c234-e185-4e15-96c6-f53426dd6690')

END

2、数据库缓存依赖中用到的SQL语句代码示例:

?

1 2 3 4 5 6 7 8 --查看状态

Select DATABASEpRoPERTYEX('soft_LocomotiveRM_DB','IsBrokerEnabled')

--启用broker

ALTER DATABASE soft_LocomotiveRM_DB SET NEW_BROKER WITH ROLLBACK IMMEDIATE ALTER DATABASE soft_LocomotiveRM_DB SET ENABLE_BROKER

--添加用户

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43 --创建一个新的登录

EXEC sp_addlogin 'test','123456'

--赋予新用户SA(System Administrator)权限,添加服务器角色

EXEC sp_addsrvrolemember 'test','sysadmin'

--授予对自己数据库的所有权限?

exec sp_addrolemember N'db_owner', N'test'

--赋予权限

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO test

--检查错误?

USE [master]

GO

ALTER DATABASE soft_LocomotiveRM_DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO

ALTER DATABASE soft_LocomotiveRM_DB SET SINGLE_USER

GO

ALTER DATABASE soft_LocomotiveRM_DB SET ENABLE_BROKER

GO

ALTER DATABASE soft_LocomotiveRM_DB SET MULTI_USER

GO

use soft_LocomotiveRM_DB

go

--新增用户

exec sp_addlogin 'test' --添?加登录

exec sp_grantdbaccess N'test' --使其成为当前数据库的合法用户

exec sp_addrolemember N'db_owner', N'test' --授予对自己数据库的所有权限

exec sp_revokedbaccess N'test' --移除对数据库的访问权限

exec sp_droplogin N'test' --删除登录

SQL查询语句大全集锦(超经典)

SQL查询语句大全集锦 MYSQL查询语句大全集锦 一、简单查询 简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句。它们分别说明所查询列、查询的 表或视图、以及搜索条件等。 例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。 复制内容到剪贴板 代码:SELECT `nickname`,`email`FROM `testtable`WHERE `name`='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以是一组列名列表、星号、表达式、变量(包括局部变量和全局变量)等构成。 1、选择所有列 例如,下面语句显示testtable表中所有列的数据: 复制内容到剪贴板 代码:SELECT * FROM testtable 2、选择部分列并指定它们的显示次序 查询结果集合中数据的排列顺序与选择列表中所指定的列名排列顺序相同。 例如: 复制内容到剪贴板 代码:SELECT nickname,email FROM testtable 3、更改列标题 在选择列表中,可重新指定列标题。定义格式为: 列标题=列名 列名列标题 如果指定的列标题不是标准的标识符格式时,应使用引号定界符,例如,下列语句使用汉字显示列 标题:

复制内容到剪贴板 代码:SELECT 昵称=nickname,电子邮件=email FROM testtable 4、删除重复行 SELECT语句中使用ALL或DISTINCT选项来显示表中符合条件的所有行或删除其中重复的数据行,默认 为ALL。使用DISTINCT选项时,对于所有重复的数据行在SELECT返回的结果集合中只保留一行。 5、限制返回的行数 使用TOP n [PERCENT]选项限制返回的数据行数,TOP n说明返回n行,而TOP n PERCENT时,说明n是 表示一百分数,指定返回的行数等于总行数的百分之几。 例如: 复制内容到剪贴板 代码:SELECT TOP 2 * FROM `testtable` 复制内容到剪贴板 代码:SELECT TOP 20 PERCENT * FROM `testtable` (二) FROM子句 FROM子句指定SELECT语句查询及与查询相关的表或视图。在FROM子句中最多可指定256个表或视图, 它们之间用逗号分隔。 在FROM子句同时指定多个表或视图时,如果选择列表中存在同名列,这时应使用对象名限定这些列 所属的表或视图。例如在usertable和citytable表中同时存在cityid列,在查询两个表中的cityid时应使用下面语句格式加以限定: 复制内容到剪贴板 代码:SELECT `username`,citytable.cityid FROM `usertable`,`citytable` WHERE usertable.cityid=citytable.cityid在FROM子句中可用以下两种格式为表或视图指定别名: 复制内容到剪贴板 代码:表名 as 别名 表名别名例如上面语句可用表的别名格式表示为: 复制内容到剪贴板

SQL经典习题及答案(新手必看)

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002'); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname='叶平')); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#='002') score2 from Student,SC where Student.S#=SC.S# and C#='001') S_2 where score2 60);

经典SQL语句大全

一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1. dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 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 only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的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

Sql语句经典难题

1.找出工资最高的员工。 2.部门中工资最高的人的名单 3.求部门的平均工资的等级 4.不用组函数求工资最高的人的信息。 5.部门工资平均的等级。 6.找出那些人不是经理 7.找出员工和他的领导 8.部门平均工资最高的部门的编号。 9.部门平均工资最高的部门的名称 10.比员工最高工资还高的经理 --select * from emp where sal =(select max(sal) from emp) --select * from emp where sal in( select deptno,max(sal) from emp group by deptno) /* select * from emp e join (select deptno,max(sal) maxsal from emp group by deptno)t on (e.deptno=t.deptno and e.sal=t.maxsal) */ /* select t.deptno ,s.grade from salgrade s join (select deptno,avg(sal) avgsal from emp group by deptno) t on ( t.avgsal between s.losal and s.hisal ) */

select deptno,avg(grade) from ( select e.deptno ,s.grade from emp e join salgrade s on (e.sal between s.losal and s.hisal) ) group by deptno */ --select * from emp where empno not in (select mgr from emp where mgr is not null) --select w.ename , m.ename from emp w join emp m on (w.mgr=m.empno) /* select dname from dept where deptno=( select deptno from (select deptno,avg(sal) avgsal from emp group by deptno) where avgsal= ( /* select max(avgsal) from ( select deptno,avg(sal) avgsal from emp group by deptno ) ) ---select max(avg(sal)) from emp group by deptno ) select * from ( select * from emp where empno in (select mgr from emp) ) where sal >( select max(sal) from

数据库经典SQL语句大全

数据库经典SQL语句大全 篇一:经典SQL语句大全 下列语句部分是Mssql语句,不可以在access中使用。 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk','testBack', 'c:mssql7backupMyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表

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 only 5、说明: 删除新表: tabname 6、说明: 增加一个列:Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明: 添加主键:Alter table tabname add primary key(col) 说明: 删除主键:Alter table tabname drop primary key(col) 8、说明: 创建索引:create [unique] index idxname on tabname(col?.) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。

精妙SQL语句收集

精妙SQL语句收集 摘要:一、基础 二、提升 三、技巧 正文: SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作。 一、基础 1、说明:创建数据库 Create DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 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 only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加var char类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col)

50个经典sql语句总结

一个项目涉及到的50个Sql语句(整理版) --1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2.课程表 Course(C,Cname,T) --C --课程编号,Cname 课程名称,T 教师编号 --3.教师表 Teacher(T,Tname) --T 教师编号,Tname 教师姓名 --4.成绩表 SC(S,C,score) --S 学生编号,C 课程编号,score 分数 */ --创建测试数据 create table Student(S varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10)) insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男') insert into Student values('02' , N'钱电' , '1990-12-21' , N'男') insert into Student values('03' , N'孙风' , '1990-05-20' , N'男') insert into Student values('04' , N'李云' , '1990-08-06' , N'男') insert into Student values('05' , N'周梅' , '1991-12-01' , N'女') insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女') insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女') insert into Student values('08' , N'王菊' , '1990-01-20' , N'女') create table Course(C varchar(10),Cname nvarchar(10),T varchar(10)) insert into Course values('01' , N'语文' , '02') insert into Course values('02' , N'数学' , '01') insert into Course values('03' , N'英语' , '03') create table Teacher(T varchar(10),Tname nvarchar(10)) insert into Teacher values('01' , N'张三') insert into Teacher values('02' , N'李四') insert into Teacher values('03' , N'王五') create table SC(S varchar(10),C varchar(10),score decimal(18,1)) insert into SC values('01' , '01' , 80) insert into SC values('01' , '02' , 90) insert into SC values('01' , '03' , 99) insert into SC values('02' , '01' , 70) insert into SC values('02' , '02' , 60) insert into SC values('02' , '03' , 80) insert into SC values('03' , '01' , 80) insert into SC values('03' , '02' , 80) insert into SC values('03' , '03' , 80) insert into SC values('04' , '01' , 50) insert into SC values('04' , '02' , 30) insert into SC values('04' , '03' , 20) insert into SC values('05' , '01' , 76) insert into SC values('05' , '02' , 87)

强化-SQL语句强化训练(史上最全最经典,呕血推荐) sql语句练习

2010/7 1. 有4个关系模式如下:出版社(出版社编号,出版社名称);图书(图书编号,书名,出版社编号,定价);作者(作者编号,姓名);著书(图书编号,作者编号,作者排序) 注:作者排序=1表示第一作者,依此类推。用SQL语句,完成第36~39题。 (1).检索所有定价超过20元的书名。 答案:SELECT书名(1分) FROM图书(1分) WHERE定价>20(2分) (2).统计每个出版社图书的平均定价。 答案:SELECT出版社编号,A VG(定价)(2分) FROM图书(1分) GROUP BY出版社编号(1分) (3).将科学出版社的所有图书定价下调5%。 答案:UPDATE图书SET定价=定价*0.95(1分) WHERE出版社编号IN(1分) (SELECT出版社编号FROM出版社(1分) WHERE出版社名称="科学")(1分) 【说明】WHERE出版社名称LIKE"科学"也正确。 (4).列出所有图书的书名、第一作者姓名和出版社名称。 答案:SELECT书名,姓名,出版社名称(1分) FROM出版社A,图书B,作者C,著书D(1分) WHEREA.出版社编号=B.出版社编号ANDB.图书编号=D.图书编号(1分) ANDC.作者编号=D.作者编号AND作者排序=1。(1分) S(SNO,SNAME,AGE,SEX,SDEPT) SC(SNO,CNO,GRADE) C(CNO,CNAME,CDEPT,TNAME) 1.试用SQL的查询语句表达下列查询: ①检索LIU老师所授课程的课程号和课程名。 ②检索年龄大于23岁的男学生的学号和姓名。 ③检索至少选修LIU老师所授课程中一门课程的女学生姓名。 ④检索W ANG同学不学的课程的课程号。 ⑤检索至少选修两门课程的学生学号。 ⑥检索全部学生都选修的课程的课程号与课程名。 ⑦检索选修课程包含LIU老师所授课程的学生学号。 2.试用SQL查询语句表达下列对教学数据库中三个基本表S、SC、C的查询: ①统计有学生选修的课程门数。 ②求选修C4课程的学生的平均年龄。 ③求LIU老师所授课程的每门课程的学生平均成绩。 ④统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。 ⑤检索学号比WANG同学大,而年龄比他小的学生姓名。 ⑥检索姓名以WANG打头的所有学生的姓名和年龄。 ⑦在SC中检索成绩为空值的学生学号和课程号。 ⑧求年龄大于女同学平均年龄的男学生姓名和年龄。 ⑨求年龄大于所有女同学年龄的男学生姓名和年龄。 3.试用SQL更新语句表达对教学数据库中三个基本表S、SC、C的各个更新操作: ①往基本表S中插入一个学生元组('S9','WU',18)。

50个经典SQL查询语句

--一个题目涉及到的50个Sql语句 --(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID) ------------------------------------表结构-------------------------------------- --学生表tblStudent(编号StuId、姓名StuName、年龄StuAge、性别StuSex) --课程表tblCourse(课程编号CourseId、课程名称CourseName、教师编号TeaId) --成绩表tblScore(学生编号StuId、课程编号CourseId、成绩Score) --教师表tblTeacher(教师编号TeaId、姓名TeaName) --------------------------------------------------------------------------------- --1、查询“001”课程比“002”课程成绩高的所有学生的学号; Select StuId From tblStudent s1 Where (Select Score From tblScore t1 Where t1.StuId=s1.stuId And t1.CourseId='001')> (Select Score From tblScore t2 Where t2.StuId=s1.stuId And t2.CourseId='002') --2、查询平均成绩大于60分的同学的学号和平均成绩; Select StuId,Avg(Score) as AvgScore From tblScore Group By StuId Having Avg(Score)>60 --3、查询所有同学的学号、姓名、选课数、总成绩; Select StuId,StuName, SelCourses=(Select Count(CourseId) From tblScore t1 Where t1.StuId=s1.StuId), SumScore=(Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId) From tblStudent s1 --4、查询姓“李”的老师的个数; Select Count(*) From tblTeacher Where TeaName like '李%' --5、查询没学过“叶平”老师课的同学的学号、姓名; Select StuId,StuName From tblStudent Where StuId Not In ( Select StuID From tblScore sc Inner Join tblCourse cu ON sc.CourseId=cu.CourseId Inner Join tblTeacher tc ON cu.TeaId=tc.TeaId Where tc.TeaName='叶平' ) --6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; Select StuId,StuName From tblStudent st Where (Select Count(*) From tblScore s1 Where s1.StuId=st.StuId And

经典EXCEL VBA SQL语句

EXCEL(VBA)~SQL经典写法范本汇集2008年03月30日星期日下午07:21EXCEL(VBA)~SQL经典写法范本汇集 **************************************************************** A、根据本工作簿的1个表查询求和写法范本 Sub查询方法一() Set CONN=CreateObject("ADODB.Connection") CONN.Open"provider=microsoft.jet.oledb.4.0;extended properties=excel8.0;data source="& ThisWorkbook.FullName sql="select区域,存货类,sum(代销仓入库数量),sum(代销仓出库数量),sum(日报数量)from[sheet4$a:i]where区域='"&[b3]&"'and month(日期)='"&Month(Range("F3"))&"'group by区域,存货类" Sheets("sheet2").[A5].CopyFromRecordset CONN.Execute(sql) CONN.Close:Set CONN=Nothing End Sub ----------------- Sub查询方法二() Set CONN=CreateObject("ADODB.Connection") CONN.Open"dsn=excel files;dbq="&ThisWorkbook.FullName sql="select区域,存货类,sum(代销仓入库数量),sum(代销仓出库数量),sum(日报数量)from[sheet4$a:i]where区域='"&[b3]&"'and month(日期)='"&Month(Range("F3"))&"'group by区域,存货类" Sheets("sheet2").[A5].CopyFromRecordset CONN.Execute(sql) CONN.Close:Set CONN=Nothing End Sub ************************************************************************************************** B、根据本工作簿2个表的不同类别查询求和写法范本 Sub根据入库表和回款表的区域名和月份分别求存货类发货数量和本月回款数量查询() Set conn=CreateObject("adodb.connection") conn.Open"provider=microsoft.jet.oledb.4.0;"&_ "extended properties=excel8.0;data source="&ThisWorkbook.FullName Sheet3.Activate Sql="select a.存货类,a.fh,b.hk from(select存货类,sum(本月发货数量)"_ &"as fh from[入库$]where存货类is not null and区域='"&[b2]_ &"'and month(日期)="&[d2]&"group by存货类)as a"_ &"left join(select存货类,sum(数量)as hk from[回款$]where存货类"_ &"is not null and区域='"&[b2]&"'and month(开票日期)="&[d2]&""_ &"group by存货类)as b on a.存货类=b.存货类" Range("a5").CopyFromRecordset conn.Execute(Sql) End Sub ******************************************************************* C、根据本文件夹下其他工作簿1个表区域的区域求和 Sub在工作表1汇总本文件夹下001工作薄的表1分数列查询汇总() Set conn=CreateObject("ADODB.Connection") conn.Open"dsn=excel files;dbq="&ThisWorkbook.Path&"\001.xls" sql="select sum(分数)from[sheet1$]"

50个经典SQL语句

50个经典SQL语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname

from Student where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002'); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname='叶平')); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#='002') score2 from Student,SC where Student.S#=SC.S# and C#='001') S_2 where score2 60);

数据库SQL经典语句(包含几乎所有的经典操作语言)

1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用) 法一:select * into b from a where 1<>1 法二:select top 0 * into b from a 2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用) insert into b(a, b, c) select d,e,f from b; 3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用) insert into b(a, b, c) se lect d,e,f from b in …具体数据库? where 条件 例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where.. 4、说明:子查询(表名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) 5、说明:显示文章、提交人和最后回复时间 select a.title,https://www.360docs.net/doc/1a12364018.html,ername,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 6、说明:外连接查询(表名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 7、说明:在线视图查询(表名1:a ) select * from (SELECT a,b,c FROM a) T where t.a > 1; 8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 数值1 and 数值2 9、说明:in 的使用方法 select * from table1 where a [not] in (…值1?,?值2?,?值4?,?值6?) 10、说明:两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 11、说明:四表联查问题: select * from a left 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 wher e ..... 12、说明:日程安排提前五分钟提醒 sql: select * from 日程安排where datediff('minute',f开始时间,getdate())>5

经典实用SQL语句大全总结

经典实用SQL语句大全总结 [编辑语言]2015-05-26 19:56 本文导航 1、首页 2、11、说明:四表联查问题: 本文是经典实用SQL语句大全的介绍,下面是该介绍的详细信息。 下列语句部分是Mssql语句,不可以在access中使用。 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack

4、说明:创建新表 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 only 5、说明: 删除新表:drop table tabname 6、说明: 增加一个列:Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明: 添加主键:Alter table tabname add primary key(col) 说明: 删除主键:Alter table tabname drop primary key(col) 8、说明: 创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:

经典SQL语句大全

经典SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not nul l],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加var char类型的长度。

7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的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 table1 11、说明:几个高级查询运算词 A:UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

相关文档
最新文档