时间:2011-09-24 12:14:05
1、SQL:structure query language
2、定义:是结构化查询语言,是关系数据库的标准语言,是一种高度非过程化的语言,是数据库后台操作语言,具有非常强大的查询功能,查询时它的核心。
3、基本构成:select 查询什么 from 从哪来 where 查询条件
4、三大功能:
l 数据查询
①.格式:select 字段名/表名/* from 表1 inner join 表2 on 表1主关键字=表2主关键字 where 筛选条件[group by (按……分组)having(限定分组条件)order by(按……排序)
②.函数
max | min | avg | cnt(count) | sum |
最大值 | 最小值 | 求平均值 | 统计 | 求和 |
③.值域查询: between…and…
④.空值查询:is null
⑤.模糊查询:$ (后包前) *(%) ?like
l 操作功能
①.插入 insert into 表名 value
②. 更新 update 表名 set 字段=表达式 where 条件
③.删除 delete from 表名 where 条件
l 定义功能
①.表的定义 creat ***
②. 删除表 drop table表名
删除视图 drop view
③.表结构的修改
⑴.添加字段:alter table 表名add 字段名 类型(长度)
⑵.删除字段:alter table 表名 drop 字段名
⑶.修改字段:alter table 表名 alter 字段名 新类型(长度)
⑷.重新命名字段:alter table 表名 rename 旧字段名 to 新字段名
⑸.建立有效性规则:alter table 表名(数据库表)alter 字段 set check 字段条件
⑹.删除有效性规则:alter table 表名alter 字段 drop check
⑺.建立默认值:alter table 表名 alter 字段 set default 条件
⑻.删除默认值:alter table 表名 alter 字段 drop default
⑼.建立唯一索引:alter table 表名add unique 字段 tag索引名
⑽.删除唯一索引:alter table 表名drop unique tag 索引名
⑾.建立主索引:alter table 表名add primary key字段 tag索引名
⑿.删除主索引:alter table 表名drop primary key字段 tag索引名
⒀.建立普通索引:alter table 表名add foreign key字段 tag索引名
⒁.删除普通索引:alter table 表名drop foreign key tag索引名
| for语句 | SQL语句 |
主索引 | 黄钥匙表示 | primary key |
候选索引 | candi | unique |
唯一索引 | unique | |
普通索引 | 默认 | foreign key |
5、SQL的六种去向:
⑴.将查询结果保存到永久表:into table/dbf 表名
⑵.将查询结果保存到临时表:into cursor表名
⑶.将查询结果保存到数组:into array 数组名
⑷.将查询结果保存到文本文件:to file 文本文件名
⑸.将查询结果预览显示:to preview
⑹.将查询结果打印:to printer
6、约束规则:
7、量词查询:some any all (any、some是同义词)
* some、any 只有子查询中有一行能使结果为真,则结果为真
* all 子查询中所有记录都使结果为真,则结果为真。
8、谓词查询 exists not exists 用来检查是否有结果返回,即存在/不存在元组