数据库知识点①
2016-04-06 09:56
162 查看
WHY SQL?
1.SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in languages like C++.
2.What makes SQL viable is that its queries are “optimized” quite well, yielding efficient query executions.
Queries :
(1)SELECT - FROM - WHERE statements
SELECT ... (desired attributes)
From ... (one or more tables)
WHERE ...( condition about tuples of the tables)
EX: Using Beers(name, manf), what beers are made by Busch?
(2)When there is one relation in the FROM clause, SELECT * clause stands for “all attributes of this relation.”
(3)If you want the result to have different attribute names, use “AS <new name>” to rename an attribute.
EX:Example based on Beers(name, manf):
(4)SQL allows duplicates in relations as well as in query results.
To force the elimination of duplicates, insert the keyword distinct after select.
Find the names of all branches in the loan relations, and remove duplicates
The keyword all specifies that duplicates not be removed.
(5)Any expression that makes sense can appear as an element of a SELECT clause.
EX: from Sells(bar, beer, price):
(6)function 查询全体学生的姓名、出生年份和所有系,要求用小写字母表示所有系名。
(7)What we can use in select clause :
expressions
constants
functions
Attribute alias
(8)What you can use in WHERE:
attribute names of the relation(s) used in the FROM.
comparison operators: =, <>, <, >, <=, >=, between, in
apply arithmetic operations: stockprice*2
operations on strings (e.g., “||” for concatenation).
Lexicographic order on strings.
Pattern matching: s LIKE p
Special stuff for comparing dates and times.
(9)Range comparison: between
谓词: BETWEEN … AND … NOT BETWEEN … AND …
查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
(10)Set operator: in
使用谓词: IN <值表>, NOT IN <值表>
<值表>: 用逗号分隔的一组取值
[例]查询信息系(IS)、数学系(MA)和计算机科学系(CS)学生的姓名和性别。
(11)Patterns
WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches.
General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>
Pattern is a quoted string with [b]%[/b] = “any string”; [b]_[/b] = “any character.”
(12)The LIKE operator
s LIKE p: pattern matching on strings
p may contain two special symbols:
% = any sequence of characters
_ = any single character
(13)ESCAPE character
When the string contains ‘%’ or ‘_’, you need to use ESCAPE character
(14)Ordering the Display of Tuples
Use ‘Order by’ clause to specify the alphabetic order of the query result
We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.
Example: order by customer_name desc
Note: Order by can only be used as the last part of select statement
(15)Order by
[例] 查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。
(16)Null Values
Three-Valued Logic
To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½.
AND = MIN; OR = MAX, NOT(x) = 1-x.
Example:
TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ ) = MIN(1, ½ ) = ½.
(17)If x=Null then 4*(3-x)/7 is still NULL
If x=Null then x=“Joe” is UNKNOWN
Three boolean values:
FALSE = 0
UNKNOWN = 0.5
TRUE = 1
(18)Unexpected behavior:
Some Persons are not included !
(19)Testing for Null
Can test for NULL explicitly:
x IS NULL
x IS NOT NULL
Now it includes all Persons
(20)Aggregations
SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column.
Also, COUNT(*) counts the number of tuples.
计数 COUNT([DISTINCT|ALL] *) COUNT([DISTINCT|ALL] <列名>)
计算总和 SUM([DISTINCT|ALL] <列名>)
计算平均值 AVG([DISTINCT|ALL] <列名>)
求最大值 MAX([DISTINCT|ALL] <列名>)
求最小值 MIN([DISTINCT|ALL] <列名>)
–DISTINCT短语:在计算时要取消指定列中的重复值
–ALL短语:不取消重复值
–ALL为缺省值
EX:From Sells(bar, beer, price), find the average price of Bud:
(21)Eliminating Duplicates in an Aggregation
DISTINCT inside an aggregation causes duplicates to be eliminated before the aggregation.
Example: find the number of different prices charged for Bud:
(22)NULL’s Ignored in Aggregation
NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column.
But if there are no non-NULL values in a column, then the result of the aggregation is NULL.
(23)Grouping
We may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes.
The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group.
EX: From Sells(bar, beer, price), find the average price for each beer:
(24)Restriction on SELECT Lists With Aggregation
If any aggregation is used, then each element of the SELECT list must be either:
1.Aggregated, or
2.An attribute on the GROUP BY list.
(25)Illegal Query Example
You might think you could find the bar that sells Bud the cheapest by:
But this query is illegal in SQL.
Why? Note bar is neither aggregated nor on the GROUP BY list.
(26)HAVING Clauses
HAVING <condition> may follow a GROUP BY clause.
If so, the condition applies to each group, and groups not satisfying the condition are eliminated.
The next article:http://www.cnblogs.com/zpfbuaa/p/5479733.html
1.SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in languages like C++.
2.What makes SQL viable is that its queries are “optimized” quite well, yielding efficient query executions.
Queries :
1. Single-relation queries 2. Multi-relation queries 3. Subqueries 4. Grouping and Aggregation
(1)SELECT - FROM - WHERE statements
SELECT ... (desired attributes)
From ... (one or more tables)
WHERE ...( condition about tuples of the tables)
EX: Using Beers(name, manf), what beers are made by Busch?
SELECT name, FROM Beers, WHERE manf = 'Busch'
(2)When there is one relation in the FROM clause, SELECT * clause stands for “all attributes of this relation.”
(3)If you want the result to have different attribute names, use “AS <new name>” to rename an attribute.
EX:Example based on Beers(name, manf):
SELECT name AS beername, manf FROM Beers WHERE manf = ‘Busch’
(4)SQL allows duplicates in relations as well as in query results.
To force the elimination of duplicates, insert the keyword distinct after select.
Find the names of all branches in the loan relations, and remove duplicates
select distinct branch_name from loan
The keyword all specifies that duplicates not be removed.
select all branch_name from loan
(5)Any expression that makes sense can appear as an element of a SELECT clause.
EX: from Sells(bar, beer, price):
SELECT bar, beer, price * 6 AS priceInYuan FROM Sells;
(6)function 查询全体学生的姓名、出生年份和所有系,要求用小写字母表示所有系名。
SELECT Sname,2006-Sage AS 'Year of Birth: ' , LOWER(Sdept) FROM Student;
(7)What we can use in select clause :
expressions
constants
functions
Attribute alias
(8)What you can use in WHERE:
attribute names of the relation(s) used in the FROM.
comparison operators: =, <>, <, >, <=, >=, between, in
apply arithmetic operations: stockprice*2
operations on strings (e.g., “||” for concatenation).
Lexicographic order on strings.
Pattern matching: s LIKE p
Special stuff for comparing dates and times.
(9)Range comparison: between
谓词: BETWEEN … AND … NOT BETWEEN … AND …
查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄。
SELECT Sname,Sdept,Sage FROM Student WHERE Sage BETWEEN 20 AND 23;
(10)Set operator: in
使用谓词: IN <值表>, NOT IN <值表>
<值表>: 用逗号分隔的一组取值
[例]查询信息系(IS)、数学系(MA)和计算机科学系(CS)学生的姓名和性别。
SELECT Sname,Ssex FROM Student WHERE Sdept IN ( 'IS','MA','CS' );
(11)Patterns
WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches.
General form: <Attribute> LIKE <pattern> or <Attribute> NOT LIKE <pattern>
Pattern is a quoted string with [b]%[/b] = “any string”; [b]_[/b] = “any character.”
(12)The LIKE operator
s LIKE p: pattern matching on strings
p may contain two special symbols:
% = any sequence of characters
_ = any single character
(13)ESCAPE character
When the string contains ‘%’ or ‘_’, you need to use ESCAPE character
(14)Ordering the Display of Tuples
Use ‘Order by’ clause to specify the alphabetic order of the query result
select distinct customer_name from borrower order by customer_name
We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.
Example: order by customer_name desc
Note: Order by can only be used as the last part of select statement
(15)Order by
[例] 查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。
SELECT * FROM Student ORDER BY Sdept ASC,Sage DESC;
(16)Null Values
Three-Valued Logic
To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½.
AND = MIN; OR = MAX, NOT(x) = 1-x.
Example:
TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ ) = MIN(1, ½ ) = ½.
(17)If x=Null then 4*(3-x)/7 is still NULL
If x=Null then x=“Joe” is UNKNOWN
Three boolean values:
FALSE = 0
UNKNOWN = 0.5
TRUE = 1
(18)Unexpected behavior:
SELECT * FROM Person WHERE age < 25 OR age >= 2
Some Persons are not included !
(19)Testing for Null
Can test for NULL explicitly:
x IS NULL
x IS NOT NULL
SELECT * FROM Person WHERE age < 25 OR age >= 25 OR age IS NULL
Now it includes all Persons
(20)Aggregations
SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column.
Also, COUNT(*) counts the number of tuples.
计数 COUNT([DISTINCT|ALL] *) COUNT([DISTINCT|ALL] <列名>)
计算总和 SUM([DISTINCT|ALL] <列名>)
计算平均值 AVG([DISTINCT|ALL] <列名>)
求最大值 MAX([DISTINCT|ALL] <列名>)
求最小值 MIN([DISTINCT|ALL] <列名>)
–DISTINCT短语:在计算时要取消指定列中的重复值
–ALL短语:不取消重复值
–ALL为缺省值
EX:From Sells(bar, beer, price), find the average price of Bud:
SELECT AVG(price) FROM Sells WHERE beer = ‘Bud’;
(21)Eliminating Duplicates in an Aggregation
DISTINCT inside an aggregation causes duplicates to be eliminated before the aggregation.
Example: find the number of different prices charged for Bud:
SELECT COUNT(DISTINCT price) FROM Sells WHERE beer = ‘Bud’;
(22)NULL’s Ignored in Aggregation
NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column.
But if there are no non-NULL values in a column, then the result of the aggregation is NULL.
(23)Grouping
We may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes.
The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group.
EX: From Sells(bar, beer, price), find the average price for each beer:
SELECT beer, AVG(price) FROM Sells GROUP BY beer;
(24)Restriction on SELECT Lists With Aggregation
If any aggregation is used, then each element of the SELECT list must be either:
1.Aggregated, or
2.An attribute on the GROUP BY list.
(25)Illegal Query Example
You might think you could find the bar that sells Bud the cheapest by:
SELECT bar, MIN(price) FROM Sells WHERE beer = ‘Bud’;
But this query is illegal in SQL.
Why? Note bar is neither aggregated nor on the GROUP BY list.
(26)HAVING Clauses
HAVING <condition> may follow a GROUP BY clause.
If so, the condition applies to each group, and groups not satisfying the condition are eliminated.
The next article:http://www.cnblogs.com/zpfbuaa/p/5479733.html
相关文章推荐
- mysql查询今天、昨天、上周
- 23个MySQL常用查询语句
- Oracle命令(一):Oracle登录命令
- mysql主从搭建
- oracle查询数据库名、实例名等
- mysql数据库遇到的各种问题
- Win7+wamp+oracle 配置
- SQL Server @@ROWCOUNT 、@@ERROR用法
- 使用T-SQL创建、修改数据库
- SQL2005 触发器简介
- 通过SSH通道跨机访问直接不可达服务(以MySQL为例)
- Python连接Redis连接配置
- oracle自定义异常
- mysql null值问题
- mysql null值问题
- mysql null值问题
- Sql Server数据库之通过SqlBulkCopy快速插入大量数据
- (转)LINUX测试环境部署Redis(四)
- Linux下MongoDB安装和启动配置(转载)
- 网打尽当下NoSQL类型、适用场景及使用公司