数据查询、操纵

SQL功能 动词
数据查询 select
数据定义 create、drop、alter
数据操纵 insert、delete、update
数据控制 grant、revoke

 一:select语句的一般格式:

select 【all  distinct】<目标列表达式>【别名】【,<目标列表达式>【别名】】.........

from  <表名/视图名>[别名]    [,<表名/视图名>[别名] ]].................   |   (select语句 )【as】<别名>

【where    <条件表达式>】

【group by  <列名1>    [having   <条件表达式>]】

【order by  <列名1>  [asc|desc] 】

 

1.单表查询

select Sname name,'year of birth:' birth,2014-Sage birthday,lower(Sdept) department       from  Student;

 

 2.where子句常用的查询条件

查询条件 谓词  
比较 =,>,<,>=,<=,!=,<>,!>,!<;NOT+上述比较运算符  
确认范围 between 下限 and 上限,not between and  
确定集合 in ,not in WHERE Sdept in('CS','MA','IS')
字符匹配 like,not like

%不限长度字符串 _单个字符 escape转义字符

where Cname like 'DB\_Design' escape'\';查找DB_Design课程的课程号和学分

空值 is null,is not null  
多处条件(逻辑运算) and ,or,not  

3.order by子句

 order by  列名+desc 降序   asc升序 

4.聚集函数

select /having  +聚类函数

count(*);count(distinct|all  列名)

sum()

avg()

max()

min()

5.group  by 子句

group by  列名   having +聚类函数

 二:插入数据

1.插入元组

insert into  表名(列名)  values(常量,常量,,,,)

2.插入子查询结果

insert into  表名(列名) 子查询;

三:修改数据

1.修改一个元组的值

update  表名   

set  列名=表达式     

 where    条件;

2.带子查询的修改语句

update  表名   

set  列名=表达式   

where  列名 in (子查询);

四:删除数据

delete from  表名

where 条件;

posted @ 2020-11-01 20:12  花花1212  阅读(91)  评论(0编辑  收藏  举报