Vous êtes sur la page 1sur 12

You are permitted to specify DUAL as a dummy table name in situations where no tables

are referenced

With INSERT ... SELECT, you can quickly insert many rows into a table from the result of
a SELECT statement, which can select from one or many tables. For example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

SELECT DISTINCT c1, c2 FROM t1;

SELECT c1, SUM(c2) FROM t1 GROUP BY c1;

SELECT COUNT(DISTINCT c1), SUM(DISTINCT c1) FROM t1;

SELECT COUNT(DISTINCT c1, c2), COUNT(DISTINCT c2, c1) FROM t1;

CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),


species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

literal values in MySQL. These include strings, numbers, hexadecimal and bit values, boolean

values, and NULL

Quoted strings placed next to each other are concatenated to a single string. The following
lines are equivalent:

'a string'
'a' ' ' 'string'

The DATE type is used for values with a date part but no time part. MySQL retrieves and
displays DATE values in 'YYYY-MM-DD'format. The supported range is '1000-01-
01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time parts. MySQL
retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported
range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date and time
parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19
03:14:07' UTC.
A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to

microseconds (6 digits) precision.

1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'. The fractional part

should always be separated from the rest of the time by a decimal point; no other fractional

seconds delimiter is recognized.

The TIME Type


may range from '-838:59:59' to '838:59:59'

YEAR Type
The YEAR type is a 1-byte type used to represent year values. It can be declared
as YEAR or YEAR(4) and has a display width of four characters.
As a 4-digit number in the range 1901 to 2155
A DEFAULT value clause in a data type specification explicitly indicates a default value for a
column. Examples:
CREATE TABLE t1 (
i INT DEFAULT -1,
c VARCHAR(10) DEFAULT '',
price DOUBLE(16,2) DEFAULT 0.00
);

The default value specified in a DEFAULT clause can be a literal constant or an expression. With
one exception, enclose expression default values within parentheses to distinguish them from
literal constant default values. Examples:
CREATE TABLE t1 (
-- literal defaults
i INT DEFAULT 0,
c VARCHAR(10) DEFAULT '',
-- expression defaults
f FLOAT DEFAULT (RAND() * RAND()),
b BINARY(16) DEFAULT (UUID_TO_BIN(UUID())),
d DATE DEFAULT (CURRENT_DATE + INTERVAL 1 YEAR),
p POINT DEFAULT (Point(0,0)),
j JSON DEFAULT (JSON_ARRAY())
);

When inserting a new row, the default value for a column with an expression default can be inserted
either by omitting the column name or by specifying the column as DEFAULT (just as for columns
with literal defaults):
mysql> CREATE TABLE t4 (uid BINARY(16) DEFAULT (UUID_TO_BIN(UUID())));
mysql> INSERT INTO t4 () VALUES();
mysql> INSERT INTO t4 () VALUES(DEFAULT);
mysql> SELECT BIN_TO_UUID(uid) AS uid FROM t4;

For a given table, the SHOW CREATE TABLE statement displays which columns have an
explicit DEFAULT clause.
For string types other than ENUM, the default value is the empty string. For ENUM, the default is the
first enumeration value.
For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the
typeThis is also true for TIMESTAMP if the explicit_defaults_for_timestamp system
variable is enabled (see Section 5.1.8, “Server System Variables”). Otherwise, for the
first TIMESTAMP column in a table, the default value is the current date and time.

CREATE TABLE new_tbl LIKE orig_tbl;

If the original table is a TEMPORARY table, CREATE TABLE ... LIKE does not
preserve TEMPORARY. To create a TEMPORARYdestination table, use CREATE TEMPORARY TABLE
... LIKE.

CREATE TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;

CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;

create table rental4 (myid int auto_increment, primary key (myid)) select customer_id,
staff_id from rental limit 10;
The data type of SELECT columns can be overridden by also specifying the column in the CREATE
TABLE part.
You can precede the SELECT by IGNORE or REPLACE to indicate how to handle rows that duplicate
unique key values.
When creating a table with CREATE TABLE ... SELECT, make sure to alias any function calls or
expressions in the query. If you do not, the CREATE statement might fail or result in undesirable
column names.
CREATE TABLE artists_and_works
SELECT artist.name, COUNT(work.artist_id) AS number_of_works
FROM artist LEFT JOIN work ON artist.id = work.artist_id
GROUP BY artist.id;
You can also explicitly specify the data type for a column in the created table:

CREATE TABLE foo (a TINYINT NOT NULL) SELECT b+1 AS a FROM bar;

If you want to have indexes in the created table, you should specify these before
the SELECT statement:
mysql> CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo;
create table rental3 (unique(customer_id), name varchar(10)) ignore select
customer_id from rental;
mysql> CREATE TABLE t1 (id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO t1 VALUES (1),(3),(1);

A SUBQUERY is a SELECT statement within another statement.


Here is an example of a subquery:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);


In this example, SELECT * FROM t1 ... is the outer query (or outer statement), and (SELECT
column1 FROM t2) is the subquery.
We say that the subquery is nested within the outer query, and in fact it is possible to nest
subqueries within other subqueries, to a considerable depth. A subquery must always appear
within parentheses.
ADVANTAGES: Structured, No need to perform complex joins, easily readable

A subquery's outer statement can be any one of: SELECT, INSERT, UPDATE, DELETE, SET, or DO.

A subquery can contain many of the keywords or clauses that an ordinary SELECT can
contain: DISTINCT, GROUP BY, ORDER BY, LIMIT, joins, index hints, UNIONconstructs, comments,
functions, and so on

you cannot modify a table and select from the same table in a subquery.

A subquery can return a scalar (a single value), a single row, a single column, or a table (one or
more rows of one or more columns).

DELETE FROM t1
WHERE s11 > ANY
(SELECT COUNT(*) /* no hint */ FROM t2
WHERE NOT EXISTS
(SELECT * FROM t3
WHERE ROW(5*t2.s1,77)=
(SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
(SELECT * FROM t5) AS t5)));

. A scalar subquery is a simple operand, and you can use it almost anywhere a single column value
or literal is legal,

SELECT

SELECT is used to retrieve rows selected from one or more tables, and can
include UNION statements and subqueries.

 Each select_expr indicates a column that you want to retrieve. There must be at
least one
 WHERE clause, if given, indicates the condition or conditions that rows must satisfy to
be selected. where_condition is an expression that evaluates to true for each row to
be selected. The statement selects all rows if there is no WHERE clause
 can also be used to retrieve rows computed without reference to any table
E.G. SELECT 1+2;
 clauses used must be given in exactly the order shown in the syntax description. For
example, a HAVING clause must come after any GROUP BY clause and before
any ORDER BY clause.
 single unqualified * can be used as shorthand to select all columns from all tables

SYNTAX
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
[HAVING where_condition]
[WINDOW window_name AS (window_spec)
[, window_name AS (window_spec)] ...]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED]

| LOCK IN SHARE MODE]]

A select_expr can be given an alias using AS alias_name. The alias is used as the
expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For
example:

SELECT CONCAT(last_name,', ',first_name) AS full_name

FROM mytable ORDER BY full_name;

A select_expr can be given an alias using AS alias_name. The alias is used as the
expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For
example:

Comparisons Using Subqueries


comparison_operator is one of these operators:
= > < >= <= <> != <=>
It finds all the rows in table t1 for which the column1 value is equal to a maximum value in
table t2:
SELECT * FROM t1
WHERE column1 = (SELECT MAX(column2) FROM t2);

Here is another example, which again is impossible with a join because it involves aggregating
for one of the tables. It finds all rows in table t1 containing a value that occurs twice in a given
column:
SELECT * FROM t1 AS t
WHERE 2 = (SELECT COUNT(*) FROM t1 WHERE t1.id = t.id);
For a comparison of the subquery to a scalar, the subquery must return a scalar. For a
comparison of the subquery to a row constructor, the subquery must be a row subquery that
returns a row with the same number of values as the row constructor. See Section 13.2.11.5,
“Row Subqueries”.

Subqueries with ANY, IN, or SOME

The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison
is TRUE for ANY of the values in the column that the subquery returns.” For example:
SELECT s1 FROM t1 WHERE s1 > ANY (SELECT s1 FROM t2);
the word IN is an alias for = ANY. IN can take an expression list, but = ANY cannot

The word SOME is an alias for ANY

select * from rental where customer_id > any (select customer_id from customer where
customer_id>500);

Subqueries with ALL

The word ALL, which must follow a comparison operator, means “return TRUE if the comparison
is TRUE for ALL of the values in the column that the subquery returns.” For example:
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);

NOT IN is an alias for <> ALL.

SUB-QUERY ERRORS

This error occurs for statements where the subquery must return at most one row but returns
multiple rows. Consider the following example:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

If SELECT column1 FROM t2 returns just one row, the previous query will work. If the subquery
returns more than one row, error 1242 will occur. In that case, the query should be rewritten as:
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);

This error occurs in cases such as the following, which attempts to modify a table and select from
the same table in the subquery:
UPDATE t1 SET column2 = (SELECT MAX(column1) FROM t1);

Row Subqueries
A row subquery is a subquery variant that returns a single row and can thus return more than one
column value.
SELECT * FROM t1
WHERE (col1,col2) = (SELECT col3, col4 FROM t2 WHERE id = 10);

the following two statements are semantically equivalent (and are handled in the same way by
the optimizer):

SELECT * FROM t1 WHERE (column1,column2) = (1,1);


SELECT * FROM t1 WHERE column1 = 1 AND column2 = 1;

The following query answers the request, “find all rows in table t1 that also exist in table t2”:
SELECT column1,column2,column3
FROM t1
WHERE (column1,column2,column3) IN
(SELECT column1,column2,column3 FROM t2);

SELECT college, region, seed FROM tournament


ORDER BY region, seed;

SELECT college, region AS r, seed AS s FROM tournament


ORDER BY r, s;

(SELECT ... ORDER BY a) ORDER BY a DESC;

Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using

column names, column aliases, or column positions. Column positions are integers and begin

with 1
SELECT college, region, seed FROM tournament
ORDER BY 2, 3;

SELECT col_name FROM tbl_name WHERE col_name > 0;

The HAVING clause can refer to aggregate functions, which the WHERE clause cannot:
SELECT user, MAX(salary) FROM users
GROUP BY user HAVING MAX(salary) > 10;

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15


13.2.10.2 JOIN Syntax

MySQL supports the following JOIN syntax for the table_references part

of SELECT statements and multiple-table DELETEand UPDATE statements:

in MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace

each other). In standard SQL, they are not equivalent. INNER JOIN is used with

an ON clause, CROSS JOIN is used otherwise.

13.2.11.8 Derived Tables

A derived table is an expression that generates a table within the scope of a

query FROM clause. For example, a subquery in a SELECT statement FROM clause is a derived

table:
SELECT ... FROM (subquery) [AS] tbl_name ...

ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;

CREATE VIEW Syntax


CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]

 It is not possible to create an index on a view.

 Indexes can be used for views processed using the merge algorithm. However, a view
that is processed with the temptable algorithm is unable to take advantage of indexes
on its underlying tables (although indexes can be used during generation of the
temporary tables).
You can use DROP TABLE or ALTER TABLE to drop or alter a table that is used in a view

definition. No warning results from the DROP or ALTER operation,

mysql> CREATE TABLE t (qty INT, price INT);


mysql> INSERT INTO t VALUES(3, 50);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
+------+-------+-------+

DROP VIEW Syntax


DROP VIEW [IF EXISTS]
view_name [, view_name] ...
[RESTRICT | CASCADE]

ALTER VIEW Syntax


ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]

 The SELECT statement cannot refer to system variables or user-defined variables.


 Within a stored program, the SELECT statement cannot refer to program parameters or
local variables.
 The SELECT statement cannot refer to prepared statement parameters.
 Any table or view referred to in the definition must exist. If, after the view has been
created, a table or view that the definition refers to is dropped, use of the view results
in an error. To check a view definition for problems of this kind, use the CHECK
TABLE statement.
 The definition cannot refer to a TEMPORARY table, and you cannot create
a TEMPORARY view.
 You cannot associate a trigger with a view.

 Aliases for column names in the SELECT statement are checked against the maximum
column length of 64 characters (not the maximum alias length of 256 characters).
ORDER BY is permitted in a view definition, but it is ignored if you select from a view using a

statement that has its own ORDER BY.


CREATE EVENT Syntax
CREATE
[DEFINER = { user | CURRENT_USER }]
EVENT
[IF NOT EXISTS]
event_name
ON SCHEDULE schedule
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'string']
DO event_body;

schedule:
AT timestamp [+ INTERVAL interval] ...
| EVERY interval
[STARTS timestamp [+ INTERVAL interval] ...]
[ENDS timestamp [+ INTERVAL interval] ...]

interval:
quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

This statement creates and schedules a new event. The event will not run unless the Event

Scheduler is enabled.

CREATE INDEX Syntax


CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
[index_type]
ON tbl_name (key_part,...)
[index_option]
[algorithm_option | lock_option] ...

key_part: {col_name [(length)] | (expr)} [ASC | DESC]

index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
| {VISIBLE | INVISIBLE}

index_type:
USING {BTREE | HASH}

algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}

lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
Optimization and Indexes

The best way to improve the performance of SELECT operations is to create indexes on one
or more of the columns that are tested in the query. The index entries act like pointers to
the table rows, allowing the query to quickly determine which rows match a condition in
the WHERE clause, and retrieve the other column values for those rows. All MySQL data types
can be indexed.
Although it can be tempting to create an indexes for every possible column used in a query,
unnecessary indexes waste space and waste time for MySQL to determine which indexes to
use. Indexes also add to the cost of inserts, updates, and deletes because each index must
be updated. You must find the right balance to achieve fast queries using the optimal set of
indexes.

Indexes are used to find rows with specific column values quickly. Without an index, MySQL

must begin with the first row and then read through the entire table to find the relevant rows.

The larger the table, the more this costs. If the table has an index for the columns in question,

MySQL can quickly determine the position to seek to in the middle of the data file without

having to look at all the data. This is much faster than reading every row sequentially.

Primary Key Optimization


The primary key for a table represents the column or set of columns that you use in your
most vital queries. It has an associated index, for fast query performance. Query
performance benefits from the NOT NULL optimization, because it cannot include
any NULL values. With the InnoDB storage engine, the table data is physically organized to
do ultra-fast lookups and sorts based on the primary key column or columns.
If your table is big and important, but does not have an obvious column or set of columns to
use as a primary key, you might create a separate column with auto-increment values to use
as the primary key. These unique IDs can serve as pointers to corresponding rows in other
tables when you join tables using foreign keys.

Foreign Key Optimization


If a table has many columns, and you query many different combinations of columns, it
might be efficient to split the less-frequently used data into separate tables with a few
columns each, and relate them back to the main table by duplicating the numeric ID column
from the main table. That way, each small table can have a primary key for fast lookups of
its data, and you can query just the set of columns that you need using a join operation.
Depending on how the data is distributed, the queries might perform less I/O and take up
less cache memory because the relevant columns are packed together on disk. (To
maximize performance, queries try to read as few data blocks as possible from disk; tables
with only a few columns can fit more rows in each data block.)
CREATE INDEX Syntax
CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
[index_type]
ON tbl_name (key_part,...)
[index_option]
[algorithm_option | lock_option] ...

key_part: {col_name [(length)] | (expr)} [ASC | DESC]

index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
| {VISIBLE | INVISIBLE}

index_type:
USING {BTREE | HASH}

algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}

lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}

Suppose that a table has the following specification:

CREATE TABLE test (


id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name)
);

Vous aimerez peut-être aussi