1. Есть ли у вас разрешение на выполнение
выражения?
2. Есть ли у вас разрешения на доступ к необходимым
данным?
3. Правилен ли синтаксис выражения?
4. Оптимизация
5. План выполнения
6. Результирующий набор
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT]
[SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE]
[SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
1. Постоянные таблицы
2. Временные таблицы
3. Виртуальные таблицы (представления)
1. =
2. !=, <>
3. >, <, >=, <=
4. BETWEEN
5. IN
6. LIKE (_, %)
1. OR
2. AND
3. NOT
SELECT DISTINCT v.fname, v.lname, i.year, i.location, i.report_file
FROM Incident AS i
LEFT JOIN V(ictim?)... -- presumably v.incident_id = i.id
LEFT JOIN Keyword AS k ON k.incident_id = i.id
WHERE i.year BETWEEN 1947 AND 1966
AND i.type = 'HOMICIDE'
AND v.sex = 'F'
AND i.status = 'UNSOLVED'
AND ...
OR v.fname IN ('Mari', 'Magda')
OR SUBSTR ...
AND (k.keyword IN ('rape', 'decapitation', 'dismemberment', 'fire', 'altar', 'priest', 'prostitute')
...
AND SUBSTR(v.fname, 1, 1) = 'R' AND SUBSTR(v.lname, 1, 1) = 'L');
fname lname year location report_file
Anna Wedin 1956Mark FULL POLICE REPORT NOT DIGITIZED
Linda Janson 1955Mariestad FULL POLICE REPORT NOT DIGITIZED
Simone Grau 1958Goteborg FULL POLICE REPORT NOT DIGITIZED
Lea Persson 1962Uddevalla FULL POLICE REPORT NOT DIGITIZED
Kajsa Severin 1962Dals-Ed FULL POLICE REPORT NOT DIGITIZED
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
Or:
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE
IF (EXP1, EXP2, EXP3)
1. Группировка по одному столбцу
2. Группировка по нескольким столбцам
3. Группировка по средствам выражений
4. WITH ROLLUP
Name Description
AVG() Return the average value of the argument
BIT_AND() Return bitwise and
BIT_OR() Return bitwise or
BIT_XOR() Return bitwise xor
COUNT(DISTINCT) Return the count of a number of different values
COUNT() Return a count of the number of rows returned
GROUP_CONCAT() Return a concatenated string
MAX() Return the maximum value
MIN() Return the minimum value
STD() Return the population standard deviation
STDDEV() Return the population standard deviation
SUM() Return the sum
VARIANCE() Return the population standard variance
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
ORDER BY {col_name | expr | position}
[ASC | DESC], ...
LIMIT {[offset,] row_count | row_count OFFSET offset}
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
Or:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
Or:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
INSERT INTO tbl_temp2 (fld_id)
SELECT
tbl_temp1.fld_order_id
FROM
tbl_temp1
WHERE
tbl_temp1.fld_order_id > 100;
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
Multiple-table syntax:
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
Multiple-table syntax:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*]] ...
FROM table_references
[WHERE where_condition]
Or:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM tbl_name[.*] [, tbl_name[.*]] ...
USING table_references
[WHERE where_condition]
DELETE
t1, t2
FROM
t1
INNER JOIN t2
INNER JOIN t3
WHERE
t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM
t1, t2
USING
t1
INNER JOIN t2
INNER JOIN t3
WHERE
t1.id=t2.id AND t2.id=t3.id;
СУБД осень 2012 Лекция 3

СУБД осень 2012 Лекция 3

  • 2.
    1. Есть лиу вас разрешение на выполнение выражения? 2. Есть ли у вас разрешения на доступ к необходимым данным? 3. Правилен ли синтаксис выражения? 4. Оптимизация 5. План выполнения 6. Результирующий набор
  • 3.
    SELECT [ALL | DISTINCT| DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}]
  • 4.
    1. Постоянные таблицы 2.Временные таблицы 3. Виртуальные таблицы (представления)
  • 5.
    1. = 2. !=,<> 3. >, <, >=, <= 4. BETWEEN 5. IN 6. LIKE (_, %) 1. OR 2. AND 3. NOT
  • 6.
    SELECT DISTINCT v.fname,v.lname, i.year, i.location, i.report_file FROM Incident AS i LEFT JOIN V(ictim?)... -- presumably v.incident_id = i.id LEFT JOIN Keyword AS k ON k.incident_id = i.id WHERE i.year BETWEEN 1947 AND 1966 AND i.type = 'HOMICIDE' AND v.sex = 'F' AND i.status = 'UNSOLVED' AND ... OR v.fname IN ('Mari', 'Magda') OR SUBSTR ... AND (k.keyword IN ('rape', 'decapitation', 'dismemberment', 'fire', 'altar', 'priest', 'prostitute') ... AND SUBSTR(v.fname, 1, 1) = 'R' AND SUBSTR(v.lname, 1, 1) = 'L');
  • 7.
    fname lname yearlocation report_file Anna Wedin 1956Mark FULL POLICE REPORT NOT DIGITIZED Linda Janson 1955Mariestad FULL POLICE REPORT NOT DIGITIZED Simone Grau 1958Goteborg FULL POLICE REPORT NOT DIGITIZED Lea Persson 1962Uddevalla FULL POLICE REPORT NOT DIGITIZED Kajsa Severin 1962Dals-Ed FULL POLICE REPORT NOT DIGITIZED
  • 8.
    CASE case_value WHEN when_valueTHEN statement_list [WHEN when_value THEN statement_list] ... [ELSE statement_list] END CASE Or: CASE WHEN search_condition THEN statement_list [WHEN search_condition THEN statement_list] ... [ELSE statement_list] END CASE IF (EXP1, EXP2, EXP3)
  • 9.
    1. Группировка поодному столбцу 2. Группировка по нескольким столбцам 3. Группировка по средствам выражений 4. WITH ROLLUP
  • 10.
    Name Description AVG() Returnthe average value of the argument BIT_AND() Return bitwise and BIT_OR() Return bitwise or BIT_XOR() Return bitwise xor COUNT(DISTINCT) Return the count of a number of different values COUNT() Return a count of the number of rows returned GROUP_CONCAT() Return a concatenated string MAX() Return the maximum value MIN() Return the minimum value STD() Return the population standard deviation STDDEV() Return the population standard deviation SUM() Return the sum VARIANCE() Return the population standard variance
  • 11.
    SELECT column_name, aggregate_function(column_name) FROMtable_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value
  • 12.
    ORDER BY {col_name| expr | position} [ASC | DESC], ... LIMIT {[offset,] row_count | row_count OFFSET offset}
  • 13.
    INSERT [LOW_PRIORITY |DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] Or: INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name SET col_name={expr | DEFAULT}, ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] Or: INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] SELECT ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ]
  • 14.
    INSERT INTO tbl_temp2(fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100; INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
  • 15.
    UPDATE [LOW_PRIORITY] [IGNORE]table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] Multiple-table syntax: UPDATE [LOW_PRIORITY] [IGNORE] table_references SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condition]
  • 16.
    DELETE [LOW_PRIORITY] [QUICK][IGNORE] FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] Multiple-table syntax: DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name[.*] [, tbl_name[.*]] ... FROM table_references [WHERE where_condition] Or: DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name[.*] [, tbl_name[.*]] ... USING table_references [WHERE where_condition]
  • 17.
    DELETE t1, t2 FROM t1 INNER JOINt2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id; Or: DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;