您的位置:首页 > 数据库

SQL语句的语法-Data Manipulation Statements

2015-07-02 20:52 309 查看

2.1 CALL Syntax

CALL [code]sp_name
([
parameter
[,...]])CALL
sp_name
[()][/code]
CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
# Set value of OUT parameter
SELECT VERSION() INTO ver_param;
# Increment value of INOUT parameter
SET incr_param = incr_param + 1;
END;
mysql> [code]SET @increment = 10;
mysql>
CALL p(@version, @increment);
mysql>
SELECT @version, @increment;
+--------------+------------+| @version | @increment |+--------------+------------+| 5.5.3-m3-log | 11 |+--------------+------------+[/code]

2.2 DELETE Syntax

Single-Table Syntax

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM [code]tbl_name
[PARTITION (
partition_name
,...)][WHERE
where_condition
][ORDER BY ...][LIMIT
row_count
][/code]

Multiple-Table Syntax

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
[code]tbl_name
[.*] [,
tbl_name
[.*]] ...FROM
table_references
[WHERE
where_condition
][/code]Or:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM [code]tbl_name
[.*] [,
tbl_name
[.*]] ...USING
table_references
[WHERE
where_condition
][/code]
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;

2.3 DO Syntax

DO [code]expr
[,
expr
] ...[/code]
mysql> [code]SELECT SLEEP(5);
+----------+| SLEEP(5) |+----------+| 0 |+----------+1 row in set (5.02 sec)[/code]
DO
,on the other hand,pauses without producing a result set.:
mysql> [code]DO SLEEP(5);
Query OK, 0 rows affected (4.99 sec)[/code]

2.4 HANDLER Syntax

HANDLER [code]tbl_name
OPEN [ [AS]
alias
]HANDLER
tbl_name
READ
index_name
{ = | <= | >= | < | > } (
value1
,
value2
,...)[ WHERE
where_condition
] [LIMIT ... ]HANDLER
tbl_name
READ
index_name
{ FIRST | NEXT | PREV | LAST }[ WHERE
where_condition
] [LIMIT ... ]HANDLER
tbl_name
READ { FIRST | NEXT }[ WHERE
where_condition
] [LIMIT ... ]HANDLER
tbl_name
CLOSE[/code]

2.5 INSERT Syntax

2.5.1 INSERT ... SELECT Syntax

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] [code]tbl_name
[PARTITION (
partition_name
,...)][(
col_name
,...)]SELECT ...[ ON DUPLICATE KEY UPDATE
col_name
=
expr
, ... ][/code]

2.5.2 INSERT DELAYED Syntax

INSERT DELAYED ...

2.5.3 INSERT ... ON DUPLICATE KEY UPDATE Syntax

2.6 LOAD DATA INFILE Syntax

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE '[code]file_name
'[REPLACE | IGNORE]INTO TABLE
tbl_name
[PARTITION (
partition_name
,...)][CHARACTER SET
charset_name
][{FIELDS | COLUMNS}[TERMINATED BY '
string
'][[OPTIONALLY] ENCLOSED BY '
char
'][ESCAPED BY '
char
']][LINES[STARTING BY '
string
'][TERMINATED BY '
string
']][IGNORE
number
{LINES | ROWS}][(
col_name_or_user_var
,...)][SET
col_name
=
expr
,...][/code]

2.7 LOAD XML Syntax

LOAD XML [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE '[code]file_name
'[REPLACE | IGNORE]INTO TABLE [
db_name
.]
tbl_name
[PARTITION (
partition_name
,...)][CHARACTER SET
charset_name
][ROWS IDENTIFIED BY '<
tagname
>'][IGNORE
number
{LINES | ROWS}][(
column_or_user_var
,...)][SET
col_name
=
expr
,...][/code]

2.8 REPLACE Syntax

REPLACE [LOW_PRIORITY | DELAYED]
[INTO] [code]tbl_name
[PARTITION (
partition_name
,...)][(
col_name
,...)]{VALUES | VALUE} ({
expr
| DEFAULT},...),(...),...[/code]Or:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] [code]tbl_name
[PARTITION (
partition_name
,...)]SET
col_name
={
expr
| DEFAULT}, ...[/code]Or:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] [code]tbl_name
[PARTITION (
partition_name
,...)][(
col_name
,...)]SELECT ...[/code]

2.9 SELECT Syntax

2.9.1 SELECT ... INTO Syntax

SELECT ... INTO var_list
 selectscolumn values and stores them into variables.
SELECT ... INTO OUTFILE
 writes the selectedrows to a file. Column and line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE
 writes a singlerow to a file without any formatting.
SELECT id, data INTO @x, @y FROM test.t1 LIMIT 1;
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;

2.9.2 JOIN Syntax

table_references:
escaped_table_reference
[,
escaped_table_reference
] ...
escaped_table_reference
:
table_reference
| { OJ
table_reference
}
table_reference
:
table_factor
|
join_table
table_factor
:
tbl_name
[PARTITION (
partition_names
)][[AS]
alias
] [
index_hint_list
]|
table_subquery
[AS]
alias
| (
table_references
)
join_table
:
table_reference
[INNER | CROSS] JOIN
table_factor
[
join_condition
]|
table_reference
STRAIGHT_JOIN
table_factor
|
table_reference
STRAIGHT_JOIN
table_factor
ON
conditional_expr
|
table_reference
{LEFT|RIGHT} [OUTER] JOIN
table_reference
join_condition
|
table_reference
NATURAL [{LEFT|RIGHT} [OUTER]] JOIN
table_factor
join_condition
:ON
conditional_expr
| USING (
column_list
)
index_hint_list
:
index_hint
[,
index_hint
] ...
index_hint
:USE {INDEX|KEY}[FOR {JOIN|ORDER BY|GROUP BY}] ([
index_list
])| IGNORE {INDEX|KEY}[FOR {JOIN|ORDER BY|GROUP BY}] (
index_list
)| FORCE {INDEX|KEY}[FOR {JOIN|ORDER BY|GROUP BY}] (
index_list
)
index_list
:
index_name
[,
index_name
] ...[/code]

2.9.3 UNION Syntax

SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]
mysql> [code]SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10);
+---------------+| REPEAT('a',1) |+---------------+| a || bbbbbbbbbb |+---------------+[/code]

2.10 Subquery Syntax

2.10.1 The Subquery as Scalar Operand

2.10.2 Comparisons Using Subqueries

2.10.3 Subqueries with ANY, IN, or SOME

the word 
IN
 isan alias for 
= ANY
.  
SOME
 isan alias for 
ANY

2.10.4 Subqueries with ALL

2.10.6 Subqueries with EXISTS or NOT EXISTS

2.11 UPDATE Syntax

Single-table syntax:
UPDATE [LOW_PRIORITY] [IGNORE] [code]table_reference
SET
col_name1
={
expr1
|DEFAULT} [,
col_name2
={
expr2
|DEFAULT}] ...[WHERE
where_condition
][ORDER BY ...][LIMIT
row_count
][/code]Multiple-table syntax:
UPDATE [LOW_PRIORITY] [IGNORE] [code]table_references
SET
col_name1
={
expr1
|DEFAULT} [,
col_name2
={
expr2
|DEFAULT}] ...[WHERE
where_condition
][/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: