什么是DML?常用SQL举例,每个命令至少1个例子,最多不超过3个例子
DML:Data Manipulation,数据操纵语言,主要用管理表中的数据,实现数据的增、删、改、查;INSERT, DELETE, UPDATE, SELECT
,是mysql数据库服务器端的命令。
服务器端的命令除了DML,还有DDL,Data Definition,数据定义语言,主要用于管理数据库组件,例如表、索引、视图、用户、存储过程CREATE、ALTER、DROP
DDL命令:(数据库管理)
-
创建:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name;
[DEFAULT] CHARACTER SET [=] charset_name #设定数据库使用的字符集
[DEFAULT] COLLATE [=] collation_name #设定排序规则
查看支持的所有字符集:SHOW CHARACTER SET
查看支持的所有排序规则:SHOW COLLATION
例: MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS testdb ; Query OK, 1 row affected, 1 warning (0.00 sec) CREATE DATABASE IF NOT EXISTS testdb2 CHARACTER SET 'utf8'; Query OK, 1 row affected (0.04 sec)
-
修改:
ALTER {DATABASE | SCHEMA } {db_name}
[DEFAULT] CHARACHTER SET [=] charset_name
[DEFAULT] COLLATE [=] collation_name
例: MariaDB [(none)]> ALTER DATABASE testdb CHARACTER SET 'gb2312'; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> ALTER DATABASE testdb COLLATE 'binary'; Query OK, 1 row affected (0.00 sec)
-
删除:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
注意:mariadb数据库没有回收站,在生产环境中删除操作很危险;MariaDB [(none)]> DROP DATABASE IF EXISTS testdb;
-
查看:
SHOW DATABASES LIKE 'pattern';
LIKE 'pattern':通配模式 %:任意长度字符 _:任意单个字符 例: MariaDB [(none)]> SHOW DATABASES LIKE '%db'; +----------------+ | Database (%db) | +----------------+ | testdb | +----------------+ 1 row in set (0.01 sec) MariaDB [(none)]> SHOW DATABASES LIKE '_estdb'; +-------------------+ | Database (_estdb) | +-------------------+ | testdb | +-------------------+ 1 row in set (0.01 sec)
(表管理)
-
创建:
CREATE TABLE [IF NOT EXISTS] tb1_name (create_defination) [tabke_options]
create_defination: 字段:col_name data_type 键: PRIMARY KEY (col1, col2, ...) UNIQUE KEY (col1, col2,...) FOREIGN KEY (column) 索引: KEY|INDEX [index_name] (col1, col2,...) table_options: ENGINE [=] engine_name 查看数据库支持的所有存储引擎类型: mysql> SHOW ENGINES; 查看数据库表状态: mysql> SHOW STATUS\G;
例:
MariaDB [testdb]> USE students; Database changed MariaDB [students]> CREATE TABLE tbl1 (id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(60) NOT NULL); Query OK, 0 rows affected (0.07 sec) MariaDB [students]> SHOW TABLE STATUS LIKE 'tbl2'\G *************************** 1. row *************************** Name: tbl2 Engine: InnoDB Version: 10 Row_format: Compact Rows: 0 Avg_row_length: 0 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 0 Auto_increment: 1 Create_time: 2018-07-22 14:16:11 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci Checksum: NULL Create_options: Comment: 1 row in set (0.01 sec)
-
添加、修改:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name [alter_specification [, alter_specification] ...]
alter_specification: 字段: 添加:ADD [COLUMN] col_name data_type [FIRST | AFTER col_name ] 删除:DROP [COLUMN] col_name 修改: HANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name] MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name] 键: 添加:ADD {PRIMARY|UNIQUE|FOREIGN} KEY (col1, col2,...) 删除: 主键:DROP PRIMARY KEY 外键:DROP FOREIGN KEY fk_symbol 索引: 添加:ADD {INDEX|KEY} [index_name] (col1, col2,...) 删除:DROP {INDEX|KEY} index_name 表选项: ENGINE [=] engine_name 查看表上的索引的信息: mysql> SHOW INDEXES FROM tbl_name;
...
示例:
MariaDB [students]> DESC tbl1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(60) | NO | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
#添加新的gender字段
MariaDB [students]> ALTER TABLE tbl1 ADD gender ENUM('F','M') after id;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [students]> DESC tbl1;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| gender | enum('F','M') | YES | | NULL | |
| name | varchar(60) | NO | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
#删除主键
MariaDB [students]> ALTER TABLE tbl1 DROP PRIMARY KEY;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [students]> DESC tbl1;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| gender | enum('F','M') | YES | UNI | NULL | |
| name | varchar(60) | NO | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
#添加主键
MariaDB [students]> ALTER TABLE tbl1 ADD PRIMARY KEY(gender);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [students]> DESC tbl1;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | UNI | NULL | auto_increment |
| gender | enum('F','M') | NO | PRI | F | |
| name | varchar(60) | NO | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
#创建索引
MariaDB [students]> ALTER TABLE tbl1 ADD INDEX(id);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
#查看索引
MariaDB [students]> DESC tbl1;
MariaDB [students]> SHOW INDEXES FROM tbl1;
#s删除索引
MariaDB [students]> ALTER TABLE tbl1 DROP INDEXES id;
- 删除:
DROP TABLE [IF EXISTS] tbl_name [, tbl_name] ...
注意:危险操作;
-
另外两种创建方式:
[1]. 复制表结构MariaDB [testdb]> CREATE TABLE tbl4 LIKE mysql.user; Query OK, 0 rows affected (0.02 sec)
[2]. 复制表数据
MariaDB [testdb]> CREATE TABLE tbl5 SELECT Host,User,Password FROM mysql.user; Query OK, 5 rows affected (0.06 sec) Records: 5 Duplicates: 0 Warnings: 0
MariaDB [testdb]> SELECT * FROM tbl5; +-------------+--------+-------------------------------------------+ | Host | User | Password | +-------------+--------+-------------------------------------------+ | localhost | root | *89367322F7FE6F2C97B8BF805585800F2E90EB76 | | 127.0.0.1 | root | *89367322F7FE6F2C97B8BF805585800F2E90EB76 | | ::1 | root | *89367322F7FE6F2C97B8BF805585800F2E90EB76 | | % | wpuser | *C9B2DB1CA193280B971CA3602D5174A5D637D2BF | | 192.168.%.% | myuser | *85E26B8AB29FEE8453201A3511DAE24A24059109 | +-------------+--------+-------------------------------------------+
-
索引管理:
索引是特殊的数据结构;
索引:要有索引名称;创建: CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [BTREE|HASH] ON tbl_name (col1, col2,,...) UNIQUE:创建多键索引; 删除: DROP INDEX index_name ON tbl_name
DDL:INSERT, DELETE, UPDATE, SELECT
-
INSERT INTO:
INSERT [INTO] tbl_name [(col1,...)] {VALUES|VALUE} (val1, ...),(...),...
注意:
字符型:引号;
数值型:不能用引号;示例: MariaDB [students]> SELECT * FROM tbl1; Empty set (0.01 sec) MariaDB [students]> INSERT INTO tbl1(name,gender) VALUES('GUO JING','M'),('Ding Dian','F'); Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [students]> SELECT * FROM tbl1; +----+--------+-----------+ | id | gender | name | +----+--------+-----------+ | 2 | F | Ding Dian | | 1 | M | GUO JING | +----+--------+-----------+ 2 rows in set (0.00 sec) #替换表中数据 MariaDB [students]> REPLACE INTO tbl1 VALUE(3,'F','DONGFANGBUBAI'); Query OK, 2 rows affected (0.01 sec)
-
SELECT:
(1)SELECT * FROM tbl_name;
(2)SELECT col1, col2, ... FROM tbl_name;
显示时,字段可以显示为别名;col_name AS col_alias
(3)SELECT col1, ... FROM tbl_name WHERE clause;
WHERE clause:用于指明挑选条件;
col_name 操作符 value:
age > 30;
操作符(1) :
>, <, >=, <=, ==, !=
组合条件:
and 、 or 、not
操作符(2) :
BETWEEN ... AND ...
LIKE 'PATTERN'
通配符:
%:任意长度的任意字符;
_:任意单个字符;
RLIKE 'PATTERN': 正则表达式对字符串做模式匹配;
IS NULL
IS NOT NULL
(4)SELECT col1, ... FROM tbl_name [WHERE clause] ORDER BY col_name, col_name2, ... [ASC|DESC];
ASC: 升序;
DESC: 降序;
示例:
MariaDB [students]> SELECT * FROM tbl1;
+----+--------+---------------+---------+
| id | gender | name | age |
+----+--------+---------------+---------+
| 1 | M | Guo Jing | NULL |
| 2 | M | Ding Dian | NULL |
| 3 | F | DONGFANGBUBAI | 1001-01 |
| 4 | F | DING DANG | NULL |
+----+--------+---------------+---------+
MariaDB [students]> SELECT id,name FROM tbl1;
+----+---------------+
| id | name |
+----+---------------+
| 1 | Guo Jing |
| 2 | Ding Dian |
| 3 | DONGFANGBUBAI |
| 4 | DING DANG |
+----+---------------+
4 rows in set (0.01 sec)
MariaDB [students]> SELECT id,gender,name FROM tbl1 WHERE gender='M';
+----+--------+-----------+
| id | gender | name |
+----+--------+-----------+
| 1 | M | Guo Jing |
| 2 | M | Ding Dian |
+----+--------+-----------+
- DELETE:
DELETE FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
(1)DELETE FROM tbl_name WHERE where_condition
(2)DELETE FROM tbl_name [ORDER BY ...] [LIMIT row_count]
注意:不加WHERE 会将删除整个表
示例:
MariaDB [students]> SELECT * FROM tbl1;
+----+--------+---------------+---------+
| id | gender | name | age |
+----+--------+---------------+---------+
| 1 | M | Guo Jing | NULL |
| 2 | M | Ding Dian | NULL |
| 3 | F | DONGFANGBUBAI | 1001-01 |
| 4 | F | DING DANG | NULL |
+----+--------+---------------+---------+
4 rows in set (0.00 sec)
MariaDB [students]> DELETE FROM tbl1 WHERE id=3;
Query OK, 1 row affected (0.01 sec)
MariaDB [students]> SELECT * FROM tbl1;
+----+--------+-----------+------+
| id | gender | name | age |
+----+--------+-----------+------+
| 1 | M | Guo Jing | NULL |
| 2 | M | Ding Dian | NULL |
| 4 | F | DING DANG | NULL |
+----+--------+-----------+------+
3 rows in set (0.00 sec)
-
UPDATE:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1=value1 [, col_name2=value2] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
示例: MariaDB [students]> UPDATE tbl1 SET age=33 WHERE id=2; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [students]> SELECT * FROM tbl1; +----+--------+-----------+------+ | id | gender | name | age | +----+--------+-----------+------+ | 1 | M | Guo Jing | NULL | | 2 | M | Ding Dian | 33 | | 4 | F | DING DANG | NULL | +----+--------+-----------+------+
用户账号及权限管理:
- 用户账号:
'username'@'host'
- host:此用户访问当前mysql服务器时,允许其通过哪些主机远程创建连接;
- 表示方式:IP,网络地址、主机名、通配符(%和_);
- 禁止检查主机名:my.cnf
[mysqld] skip_name_resolve = ON
- 创建用户账号:
CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
- 删除用户账号:
DROP USER ’user‘@’host' [, user@host] ...
- 授权:
-
权限级别:管理权限、数据库、表、字段、存储例程;
GRANT priv_type,... ON [object_type] db_name.tbl_name TO 'user'@'host' [IDENTIFIED BY 'password'];
priv_type: ALL [PRIVILEGES] db_name.tbl_name: *.*:所有库的所有表; db_name.*:指定库的所有表; db_name.tbl_name:指定库的特定表; db_name.routine_name:指定库上的存储过程或存储函数; [object_type] TABLE FUNCTION PROCEDURE
-
查看指定用户所获得的授权:
SHOW GRANTS FOR 'user'@'host'
SHOW GRANTS FOR CURRENT_USER;
-
回收权限:
REVOKE priv_type, ... ON db_name.tbl_name FROM 'user'@'host';
- 注意:MariaDB服务进程启动时,会读取mysql库的所有授权表至内存中;
(1) GRANT或REVOKE命令等执行的权限操作会保存于表中,MariaDB此时一般会自动重读授权表,权限修改会立即生效;
(2) 其它方式实现的权限修改,要想生效,必须手动运行FLUSH PRIVILEGES命令方可;
- 加固mysql服务器,在安装完成后,运行
mysql_secure_installation
命令;
网友评论