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_nameOPEN [ [AS]
alias]HANDLER
tbl_nameREAD
index_name{ = | <= | >= | < | > } (
value1,
value2,...)[ WHERE
where_condition] [LIMIT ... ]HANDLER
tbl_nameREAD
index_name{ FIRST | NEXT | PREV | LAST }[ WHERE
where_condition] [LIMIT ... ]HANDLER
tbl_nameREAD { FIRST | NEXT }[ WHERE
where_condition] [LIMIT ... ]HANDLER
tbl_nameCLOSE[/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 OUTFILEwrites the selectedrows to a file. Column and line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILEwrites 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_referenceSTRAIGHT_JOIN
table_factor|
table_referenceSTRAIGHT_JOIN
table_factorON
conditional_expr|
table_reference{LEFT|RIGHT} [OUTER] JOIN
table_reference
join_condition|
table_referenceNATURAL [{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 wordINisan alias for
= ANY.
SOMEisan 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_referenceSET
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_referencesSET
col_name1={
expr1|DEFAULT} [,
col_name2={
expr2|DEFAULT}] ...[WHERE
where_condition][/code]
相关文章推荐
- mysql5.6 忘记root密码
- Introduction to SQL
- NodeJS利用mongoose模糊查询MongoDB
- mysql读写分离(主从复制)实现
- mysql登录报错 ERROR 1045 (28000)
- Linq To SQL LEFT OUTER JOIN (Left Join)
- mysql 端口监听问题
- mysql innodb 奔溃问题
- MYSQL远程访问设置
- mysql重连,连接丢失:The last packet successfully received from the server
- Redis在LNMP一键安装下到的安装和使用
- SQL 2000 修改SA密码,提示错误2812,未能找到存储过程'sp_password'
- oracle 11GR2 dataguard SWITCHOVER FAILOVER
- ORACLE DATAGUARD 11G R2 RAC TO RAC
- mongodb经验
- Redis数据持久化
- Nginx+Memcached+Tomcat集群配置
- mongoDB的基本操作
- Mongodb索引
- mysql 实现排名及中文排序实例[分页累加行号]