Friday, November 13, 2015

Normal Form in Database Design

Wednesday, November 11, 2015

View Sequence Index and Synonyms

Monday, November 9, 2015

ER Diagram转化为关系映射的步骤

Saturday, November 7, 2015

SQL注意细节

1. Cross Join 就是 Cartesian Product
    Natural Join 就是基于相同列名的Left Outer Join Right Outer Join 或者Inner Join

2. Count(列名) 会忽略NULL的情况, 它一定会返回一个0到无穷大之间的数,不会返回NULL.
    COUNT(*) 很特殊,它会记录NULL的情况。

3. JOIN 分为Equi-Join, Non-Equi-Join以及Inner Join 和Outer Join

    On Column Between A and B

4. SELECT TO_CHAR(SUM(ALL(SALARY)), '$9999.99') FROM EMPLOYEE;
    将数字转为字符格式。

5. Median AVG 会忽略NULL的情况。

6. Select Can only include Expressions in the select list that are defined at the same level of detail as each other.

7. When Group by is involved order by can only include group by columns.

8. Nest Aggregate Function can be up to 2 level deep.

9. Select Required
    from Required
    where Optional
    Group by Optional
    Having Optional
    Order By Optional

10. Having only take Group by Clause and Aggregate functions

11. Sub Query will return NULL if not found.

12. Without Group by sub query will return Single Value

13. Find all products with a price that’s greater than all of the products in the ‘Luxury’ category:              
SELECT * FROM PRODUCTS
    WHERE PRICE > ALL (SELECT PRICE FROM PRODUCTS
    WHERE CATEGORY = ‘Luxury’);

14. < > 表示不等于

15. A scalar subquery returns one row with one column.
      Scalar subquery expressions can ONLY be used in the following locations: SELECT AND WHERE

Wednesday, November 4, 2015

生产者消费者模型队列设计

Monday, November 2, 2015

Trips and Users

The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).
+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id |        Status      |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1  |     1     |    10     |    1    |     completed      |2013-10-01|
| 2  |     2     |    11     |    1    | cancelled_by_driver|2013-10-01|
| 3  |     3     |    12     |    6    |     completed      |2013-10-01|
| 4  |     4     |    13     |    6    | cancelled_by_client|2013-10-01|
| 5  |     1     |    10     |    1    |     completed      |2013-10-02|
| 6  |     2     |    11     |    6    |     completed      |2013-10-02|
| 7  |     3     |    12     |    6    |     completed      |2013-10-02|
| 8  |     2     |    12     |    12   |     completed      |2013-10-03|
| 9  |     3     |    10     |    12   |     completed      |2013-10-03| 
| 10 |     4     |    13     |    12   | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+
The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).
+----------+--------+--------+
| Users_Id | Banned |  Role  |
+----------+--------+--------+
|    1     |   No   | client |
|    2     |   Yes  | client |
|    3     |   No   | client |
|    4     |   No   | client |
|    10    |   No   | driver |
|    11    |   No   | driver |
|    12    |   No   | driver |
|    13    |   No   | driver |
+----------+--------+--------+
Write a SQL query to find the cancellation rate of requests made by unbanned clients between Oct 1, 2013 and Oct 3, 2013. For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.
+------------+-------------------+
|     Day    | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 |       0.33        |
| 2013-10-02 |       0.00        |
| 2013-10-03 |       0.50        |
+------------+-------------------+

Department Top Three Salaries

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.
+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+
The Department table holds all departments of the company.
+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+
Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+