- 0
- 0
- 约6.03千字
- 约 7页
- 2020-02-26 发布于陕西
- 举报
PAGE 3
第三章 使用SQL查询
§3.1再议SQL语言的分类:DML/DDL/DCL
DML(Data Manipulation or Data Modifiction Language)数据维护或数据修改语言命令是Oracle数据库中用于插入、更新与删除表记录行的SQL命令。DML命令包括Insert、Update与Delete命令。
Insert into sales.parts (id,unit_price,description)
Values (45,1000.00,’Pentium 166 CPU’);
Insert into archive.customers
(Select * From sales.customers);
Update sales.parts
Set unit_price=250.00
Where id=492;
Delete From sales.customers
Where last_name=’Gates’ And first_name=’William’;
DDL(Data Definition Language)命令创建、修改与删除数据库对象。大多数数据库对象的类型具有相应的Create、Alter与Drop命令。
Create Table、Alter Table、Create View、Create Index、Alter Index、Drop Index、Create Sequence
Create Table sales.parts
( id integer Primary Key,
unit_price Number(10,2),
description Varchar2(150));
Alter Table sales.parts
Modify (unit_price Default 0.00,description Not Null);
Create View cust_usa As
Select last_name,first_name,phone
From sales.customers
Where country=’USA’
With Check Option;
Check约束以限制视图的插入或更新功能。
Create Index sales.part_descriptions on sales.parts (description);
Create Index sales.part_decriptions Rebuild;
Drop Index sales.part_decriptions;
DCL(Data Control Language)命令控制用户对Oracle数据库的访问。常用的命令有Grant、Revoke命令。
Grant Select On sales.part To Public;
Revoke Select On sales.part From Public;
§3.2基本SQL查询语句
列列表与选择条件
查询结果集是查询从数据库服务器所请求的行与列的集合。
Select * From sales.orders;
Select id,order_date
From sales.orders
Where cust_id=1;
Select * From sales.orders
Where cust_id=(
Select id From sales.customers
Where last_name=’Ellison’ Or last_name=’White’);
使用通配符“*”查询出指定表的所有列。
缺少任何查询条件将查询出指定表的所有行。
联合查询
查询连接多个表信息的查询。
Select i.id lineid,p.description,i.quantity,p.unit_price
From sales.items I,sales.parts p
Where i.order_id=1732
And i.part_id=p.id;
查询多表的列表内容可以定义表的使用的别名,这将大大提高书写SQL的速度和清晰度。
列列表在每个列名前缀表别名等同于表名。
通过表的别名,可以消除涉及两不同表有相同列的模糊性。
可通过Where子句来定义表列表之间的联合关系。
排序查询
查询可以将结果集中的记录行以长序或降序进行排序。
Select last_name,first_name
From sales.customers
Order by last_name ASC first_name DESC;
§3.3聚合查询与聚合函数
使用Sum、Count、Avg、Min、Max等函
原创力文档

文档评论(0)