008-LAMP_MySQL_DML
2016-01-19 17:54
561 查看
008-LAMP_MySQL_DML
DML
insert delete updata selectinsert
注意:字符型需要使用引号,数值型不能使用引号Syntax:
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 inserts new rows into an existing table. The INSERT … VALUES
and INSERT … SET forms of the statement insert rows based on
explicitly specified values. The INSERT … SELECT form inserts rows
selected from another table or tables. INSERT … SELECT is discussed
further in [HELP INSERT SELECT].
插入所有数据
MariaDB [sdb]> DESC students; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | | NULL | | | name | char(30) | NO | | NULL | | | class | varchar(100) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | sex | enum('F','M') | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) MariaDB [sdb]> INSERT INTO students VALUES (201201,'husa','optimize',19,'F'); Query OK, 1 row affected (0.00 sec)
插入不完整数据
MariaDB [sdb]> INSERT INTO students (id,name,age,sex) VALUES (201202,'xizhu',120,'M'); Query OK, 1 row affected, 1 warning (0.00 sec) MariaDB [sdb]> SELECT * FROM students; +--------+-------+----------+------+------+ | id | name | class | age | sex | +--------+-------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 120 | M | +--------+-------+----------+------+------+ 2 rows in set (0.00 sec)
插入多行数据
MariaDB [sdb]> INSERT INTO students VALUES (201203,'wangzhenyi','optimize',18,'M'),(201204,'youzhu','optimize',21,'M'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 120 | M | | 201203 | wangzhenyi | optimize | 18 | M | | 201204 | youzhu | optimize | 21 | M | +--------+------------+----------+------+------+ 4 rows in set (0.00 sec)
select
Syntax: 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}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]] SELECT is used to retrieve rows selected from one or more tables, and can include UNION statements and subqueries. See [HELP UNION], and http://dev.mysql.com/doc/refman/5.5/en/subqueries.html. The most commonly used clauses of SELECT statements are these: o Each select_expr indicates a column that you want to retrieve. There must be at least one select_expr. o table_references indicates the table or tables from which to retrieve rows. Its syntax is described in [HELP JOIN]. o The 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. In the WHERE expression, you can use any of the functions and operators that MySQL supports, except for aggregate (summary) functions. See http://dev.mysql.com/doc/refman/5.5/en/expressions.html, and http://dev.mysql.com/doc/refman/5.5/en/functions.html. SELECT can also be used to retrieve rows computed without reference to any table.
查询id,name并使用别名
MariaDB [sdb]> SELECT id as stuID,name FROM students; +--------+------------+ | stuID | name | +--------+------------+ | 201201 | husa | | 201202 | xizhu | | 201203 | wangzhenyi | | 201204 | youzhu | +--------+------------+ 4 rows in set (0.00 sec)
操作符:
=,>... between and or,and,not like 'pattern' %:任意长度的任意字符 _:任意单个字符 rlike 'pattern' 正则表达式对字符串做模式匹配 is NULL is not NULL 排序 order by ASC升序 DESC降序 \g\G横排竖排显示
从表中查询名字中包含’zh’的所有信息
MariaDB [sdb]> SELECT * FROM students WHERE name LIKE '%zh%'; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201202 | xizhu | | 120 | M | | 201203 | wangzhenyi | optimize | 18 | M | | 201204 | youzhu | optimize | 21 | M | +--------+------------+----------+------+------+ 3 rows in set (0.00 sec)
delete
按行删除,删除的是 记录Syntax: Single-table syntax: 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] For the single-table syntax, the DELETE statement deletes rows from tbl_name and returns a count of the number of deleted rows. This count can be obtained by calling the ROW_COUNT() function (see http://dev.mysql.com/doc/refman/5.5/en/information-functions.html). The WHERE clause, if given, specifies the conditions that identify which rows to delete. With no WHERE clause, all rows are deleted. If the ORDER BY clause is specified, the rows are 8a90 deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted. For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used. where_condition is an expression that evaluates to true for each row to be deleted. It is specified as described in http://dev.mysql.com/doc/refman/5.5/en/select.html. Currently, you cannot delete from a table and select from the same table in a subquery. You need the DELETE privilege on a table to delete rows from it. You need only the SELECT privilege for any columns that are only read, such as those named in the WHERE clause.
删除class为空的记录
MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 120 | M | | 201203 | wangzhenyi | optimize | 18 | M | | 201204 | youzhu | optimize | 21 | M | | 201205 | weige | optimize | NULL | M | +--------+------------+----------+------+------+ 5 rows in set (0.00 sec) MariaDB [sdb]> DELETE FROM students WHERE age IS NULL; Query OK, 1 row affected (0.01 sec) MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 120 | M | | 201203 | wangzhenyi | optimize | 18 | M | | 201204 | youzhu | optimize | 21 | M | +--------+------------+----------+------+------+ 4 rows in set (0.00 sec)
update
Syntax: Single-table syntax: 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] For the single-table syntax, the UPDATE statement updates columns of existing rows in the named table with new values. The SET clause indicates which columns to modify and the values they should be given. Each value can be given as an expression, or the keyword DEFAULT to set a column explicitly to its default value. The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated. If the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated.
将name中有zh字符的记录的age字段全部设置为20
MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 120 | M | | 201203 | wangzhenyi | optimize | 18 | M | | 201204 | youzhu | optimize | 21 | M | +--------+------------+----------+------+------+ 4 rows in set (0.00 sec) MariaDB [sdb]> UPDATE students SET age=20 WHERE name LIKE '%zh%'; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 20 | M | | 201203 | wangzhenyi | optimize | 20 | M | | 201204 | youzhu | optimize | 20 | M | +--------+------------+----------+------+------+ 4 rows in set (0.00 sec)
删除sex字段,使用alter drop而不是delete或者update
MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+------+ | id | name | class | age | sex | +--------+------------+----------+------+------+ | 201201 | husa | optimize | 19 | F | | 201202 | xizhu | | 20 | M | | 201203 | wangzhenyi | optimize | 20 | M | | 201204 | youzhu | optimize | 20 | M | +--------+------------+----------+------+------+ 4 rows in set (0.00 sec) MariaDB [sdb]> ALTER TABLE students DROP sex; Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 MariaDB [sdb]> SELECT * FROM students; +--------+------------+----------+------+ | id | name | class | age | +--------+------------+----------+------+ | 201201 | husa | optimize | 19 | | 201202 | xizhu | | 20 | | 201203 | wangzhenyi | optimize | 20 | | 201204 | youzhu | optimize | 20 | +--------+------------+----------+------+ 4 rows in set (0.00 sec)
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法
- MySQL Server 日志
- MySQL 安全事宜
- MySQL 备份与恢复