sql基础知识
SQL语言由命令、子句、运算和集合函数等构成。在SQL中,数据定义语言DDL(用来建立及定义数据表、字段以及索引等数据库结构)包含的命令有CREATE、DROP、ALTER;数据操纵语言DML(用来提供数据的查询、排序以及筛选数据等功能)包含的命令有SELECT、INSERT、UPDATE、DELETE。
一、SQL语句
(1)Select 查询语句
语法:SELECT [ALL|DISTINCT] <目标列表达式> [AS 列名]
[,<目标列表达式> [AS 列名] ...] from <表名> [,<表名>…]
[WHERE <条件表达式> [AND|OR <条件表达式>...]
[GROUP BY 列名 [HAVING <条件表达式>>
[ORDER BY 列名 [ASC | DESC>
解释:[ALL|DISTINCT] ALL:全部; DISTINCT:不包括重复行
<目标列表达式> 对字段可使用AVG、COUNT、SUM、MIN、MAX、运算符等
<条件表达式>
查询条件 谓词
比较 =、>,<,>=,<=,!=,<>,
确定范围 BETWEEN AND、NOT BETWEEN AND
确定集合 IN、NOT IN
字符匹配 LIKE(“%”匹配任何长度,“_”匹配一个字符)、NOT LIKE
空值 IS NULL、IS NOT NULL
子查询 ANY、ALL、EXISTS
集合查询 UNION(并)、INTERSECT(交)、MINUS(差)
多重条件 AND、OR、NOT
<GROUP BY 列名> 对查询结果分组
[HAVING <条件表达式>] 分组筛选条件
[ORDER BY 列名 [ASC | DESC> 对查询结果排序;ASC:升序 DESC:降序
例1: select student.sno as 学号, student.name as 姓名, course as 课程名, score as 成绩 from score,student where student.sid=score.sid and score.sid=:sid
例2:select student.sno as 学号, student.name as 姓名,AVG(score) as 平均分 from score,student where student.sid=score.sid and student.class=:class and (term=5 or term=6) group by student.sno, student.name having count(*)>0 order by 平均分 DESC
例3:select * from score where sid like '9634'
例4:select * from student where class in (select class from student where name='陈小小')
(2)INSERT插入语句
语法:INSERT INTO <表名> [(<字段名1> [,<字段名2>, ...])] VALUES (<常量1> [,<常量2>, ...])
语法:INSERT INTO <表名> [(<字段名1> [,<字段名2>, ...])] 子查询
例子:INSERT INTO 借书表(
相关文档:
将b表中caller列的值插入a表中call列中。
create table a
(
fid int,
call varchar(20),
age int
)
create table b
(
fid int,
caller varchar(20),
parentId int
)
select * from a
select * from b
insert into a values(1,null,19)
insert into a values(2,n ......
ALTER DATABASE AdventureWorks ADD FILEGROUP [2003Q3]
创建文件组后,使用 ALTER DATABASE 将文件添加到该文件组中。
ALTER DATABASE AdventureWorks
ADD FILE
(NAME = N'2003Q3',
FILENAME = N'C:\AdventureWorks\2003Q3.ndf',
SIZE = 5MB,
MAXSIZE = 100MB,
FILEGROWTH = 5MB)
TO FILEGR ......
刚刚安装的数据库系统,按照默认安装的话,很可能在进行远程连接时报错,通常是错误:"在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) "搜MSDN,上面有一篇机器翻译的文章,实在让人难以明白,现在 ......
Comparison of SQL and MDX Syntax
The Multidimensional Expressions (MDX) syntax is similar to the syntax of Structured Query Language (SQL). In many ways, the functionality supplied by MDX is also similar to that of SQL; with effort, you can eve ......
安装前准备:
前言:
[学习SQL SERVER 2005系列]准备把学习2005的一些心得整理出来,和大家分享,共同学习一起提高。
安装准备:
1、SQL Server 2005 的各版本之间选择
大多数企业都在三个 SQL Server 版本之间选择:SQL Server 2005 Enterprise Edition、SQL Server 2005 Standard Edition 和 SQL Server 2 ......