码迷,mamicode.com
首页 > 数据库 > 详细

数据库知识点

时间:2016-04-06 11:11:54      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

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. 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 ( ISMACS );

 

数据库知识点

标签:

原文地址:http://www.cnblogs.com/zpfbuaa/p/5358035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!