MYSQL语句:有2种
DDL, DML
DDL: CREATE, ALTER, DROP
DML: INSERT, DELETE, UPDATE, SELECT
DCL: GRANT(授权), REVOKE(撤销授权)
mysql命令中不区分字符大小写,但命令要与操作对象区分大小写;一般命令用大写,操作对象用小写;
例如:在centos 7
> help create:查看使用创建的命令帮助;
> HELP CREATE DATABASE
> show collation; 查看支持的所有排序规则集;
> show character set; 查看支持的所有字符集;
> CREATE SCHEMA IF NOT EXISTS testdb; 创建名为testdb的数据库,且如果不存在就创建;
数据库管理:
创建数据库:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name;
指明默认字符集:[DEFAULT] CHARACTER SET [=] charset_name
指明默认排序规则:[DEFAULT] COLLATE [=] collation_name
查看支持的所有字符集:SHOW CHARACTER SET
查看支持的所有排序规则:SHOW COLLATION
同一种字符集有不同的排序规则;
修改数据库:
ALTER {DATABASE | SCHEMA} [db_name]
[DEFAULT] CHARACTER SET [=] charset_name
[DEFAULT] COLLATE [=] collation_name
例如:
> help alter database 查看修改数据库命令帮助;
删除数据库:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
注意:mysql数据库没有回收站,谨慎删除操作;
例如:
> help drop database; 查看删除数据库命令帮助;
]# cd /var/lib/mysq:数据库在文件系统上是以文件形式存储的;
可在此目录下看见,自己创建的数据库testdb;所以,也可在linux文件系统上创建一个数据库文件,在登录mysql也能看见,但里面不能存储数据;
注意:两个数据库名不要不区分大小写的相同名称;
查看数据库:
SHOW DATABASES LIKE ‘PAT’; 查看模式中匹配的库;
SHOW DATABASES; 查看所有库;
表管理:
创建表:有3种方式
CREATE TABLE [IF NOT EXISTS] tbl_name (create_defination) [table_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 TABLES STATUS [LIKE 'tbl_name']
查看表
SHOW TABLES LIKE ‘PAT’; 查看模式中匹配的库;
SHOW TABLES; 查看所有表;
例如:
> help create table 查看创建表命令帮助;
> use testdb 先进入指定数据库中;
> CREATE TABLE students(id INT UNSIGNED NOT NULL, name CHAR(30) NOT NULL, age TINYINT UNSIGNED, gender ENUM('f','m'));
创建表student,字段分别为id字段类型为无符号、整型、,不能为空,name字段定长为30字节、不能为空,age字段为微小整型、无符号,gender字段枚举只能使用f或m;
注意:没有说明不能为空的字段,就可以为空;
> DESC students; 查看表中的字段;
> CREATE TABLE students(id INT UNSIGNED NOT NULL PRIMARY KEY, name CHAR(30) NOT NULL, age TINYINT UNSIGNED, gender ENUM('f','m'));
其中:指定单独的一个字段为主键,即在id字段后设置PRIMARY KEY即可;
> DESC students
> CREATE TABLE students(id INT UNSIGNED NOT NULL, name CHAR(30) NOT NULL, age TINYINT UNSIGNED, gender ENUM('f','m'), PRIMARY KEY(id,name));
其中:指定多个字段为主键,即指定id和name字段为主键,在命令最后设置PRIMARY KEY(id,name)即可;
> DESC students;
注意:UNSIGNED在修饰类型的,必须跟在INT数据类型的后面设置;
> show table statusG 查看默认的表存储引擎类型,垂直显示;
> SHOW TABLE STATUS like 'students'G 查看指定表存储引擎类型;
修改表:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name [alter_specification [, alter_specification] ...]
alter_specification:修改规范
字段修改:
添加字段:ADD [COLUMN] col_name data_type [FIRST | AFTER col_name ]
FIRST:放在表的第一个字段;
AFTER col_name:放在指定字段的后面,不指明默认放在最后字段;
删除字段:DROP [COLUMN] col_name
修改:
CHANGE [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
唯一键:DROP {INDEX|KEY} index_name
索引修改:
添加索引:ADD {INDEX|KEY} [index_name] (col1, col2,...)
删除索引:DROP {INDEX|KEY} index_name
注意:索引可以有名字;
表选项修改:
ENGINE [=] engine_name
注意:改引擎比较危险,是先创建表,再按照目标存储引擎,把原数据导入,需要大量时间;
查看表上的索引的信息:
mysql> SHOW INDEXES FROM tbl_name;
可修改的内容有create_defination中定义的内容、table_options中定义的内容等;
> help alter table 查看修改命令帮助
> show tables; 查看所有表
> DESC students; 查看表中字段
> ALTER TABLE students ADD class VARCHAR(100) NOT NULL; 在students表中添加class字段,类型为可变长字段、长度最大100个字节、此字段不能为空;
> ALTER TABLE students DROP class; 删除表中的class字段
> ALTER TABLE students ADD class VARCHAR(100) NOT NULL AFTER name; 指定把class字段添加在name字段后面
> DESC students;
> ALTER TABLE students MODIFY class VARCHAR(100) AFTER age; 小改,把class字段改在age字段后面
> DESC students;
> ALTER TABLE students DROP PRIMARY KEY; 删除主键
> DESC students;
> ALTER TABLE students ADD UNIQUE KEY (name); 添加name字段为主键,如果此字段要求不能为空,则字段变为唯一键
> DESC students;
> SHOW INDEX FROM students; 查看tudents表中索引信息
> ALTER TABLE students DROP INDEX name; 删除students表中索引名为name
> ALTER TABLE students ADD INDEX class (class); 添加字段class的索引为class,索引名和字段名可相同
> SHOW INDEX FROM students; 查看表中的索引
> ALTER TABLE students DROP INDEX class;
> SHOW INDEX FROM students;
> DESC students;
注意:表是库中的,如果使用'use 库名'设定了默认库,可使用表的基名,如果没有设定默认库,则:
> SHOW TABLES FROM testdb; 指明仓库testdb数据库中的表
> CTEATE TABLE mydb.class 指明在mydb数据库中创建表名为class
使用库名.表名为绝对路径,使用use 库名,设定默认库;
删除表:
DROP TABLE [IF EXISTS] tbl_name [, tbl_name] ...
例如:
> DROP TABLE students; 删除表studnets
表的引用方式:
tbl_name 在使用use 库名设定默认库后,才能使用此方式;
db_name.tbl_name 即库名.表名是决对路径方式;
第二种表的创建方式:
复制表结构;
第三种表的创建方式:
复制表数据;
索引管理:
索引是特殊的数据结构;
索引:要有索引名称;没有修改,只有删除;
创建索引:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [BTREE|HASH] ON tbl_name (col1, col2,,...)
UNIQUE 创建唯一键索引;前面介绍unique key也可创建唯一键索引;
FULLTEXT 创建全文索引;后面介绍;
BTREE|HASH 索引类型;mysql中只有memory表有哈希索引;而memory表很少手动创建,其它表又都不支持,所以只可手动创建为btree索引;
创建索引时,什么选项都不加默认创建为btree索引;
索引有两种类型,一种是树状是索引,一种是hash索引;mysql使用的是btree索引,b树索引也叫平衡树;b树索有3种:b-树,b树,b+树;mysql用的是b+树;
例如:
> SHOW TABLES FROM testdb;
> CREATE TABLE testdb.classesc 取消执行命令语句;
> use testdb
> SHOW INDEX FROM students; 没有任何索引;
> CREATE INDEX name_and_class ON students (name,class); 添加name和class为索引,
> SHOW INDEX FROM students; 查看表中的索引;为多值索引;
删除索引:
DROP INDEX index_name ON tbl_name
例如:
> help drop index; 查看删除索引命令帮助;
> DROP INDEX name_and_class ON students; 删除索引;
> SHOW INDEX FROM students;
服务器端命令:数据管理
DML:INSERT, DELETE, UPDATE, SELECT
INSERT INTO命令:添加数据
INSERT [INTO] tbl_name [(col1,...)] {VALUES|VALUE} (val1, ...),(...),...
col1对应val1,每个字段都应该对应一个值;可批量插入;
注意:
字符型:必须用引号;
数值型:不能用引号;
例如:
> 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 | |
| gender | enum('f','m') | YES | | NULL | |
+--------+---------------------+------+-----+---------+-------+
> INSERT INTO students VALUES (1,'kobe',96,38,'m'); 在表中(没指明字段)每个字段都插入数据;第一个字段为1,第二字段为kobe,以此类推;
允许为空的字段,可以不给值;
> INSERT INTO students (id,name) VALUES (2,'jodan'),(3,'oneal'); 只添加id和name字段的值;添加2个;
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: 降序序;
内连接有如下三种:
等值连接:让表之间的字段以“等值”建立连接关系;
自然连接:等值连接不将重复属性去掉,而自然连接去掉重复属性,也可以说,自然连接是去掉重复列的等值连接。
不等值连接:等值连接:主要用除了等号之外的操作符,比如:<>、>、<、>=、<=、LIKE、IN、BETWEEN…AND。
学生表与班级表取左外连接 select s.stuid,s.name,c.class from students as s left join classes as c on s.classid=c.classid; +-------+---------------+----------------+ | stuid | name | class | +-------+---------------+----------------+ | 1 | Shi Zhongyu | Emei Pai | | 2 | Shi Potian | Shaolin Pai | | 3 | Xie Yanke | Emei Pai | | 4 | Ding Dian | Wudang Pai | | 5 | Yu Yutong | QingCheng Pai | | 6 | Shi Qing | Riyue Shenjiao | | 7 | Xi Ren | QingCheng Pai | | 8 | Lin Daiyu | Ming Jiao | | 9 | Ren Yingying | Lianshan Pai | | 10 | Yue Lingshan | QingCheng Pai | | 11 | Yuan Chengzhi | Lianshan Pai | | 12 | Wen Qingqing | Shaolin Pai | | 13 | Tian Boguang | Emei Pai | | 14 | Lu Wushuang | QingCheng Pai | | 15 | Duan Yu | Wudang Pai | | 16 | Xu Zhu | Shaolin Pai | | 17 | Lin Chong | Wudang Pai | | 18 | Hua Rong | Ming Jiao | | 19 | Xue Baochai | Lianshan Pai | | 20 | Diao Chan | Ming Jiao | | 21 | Huang Yueying | Lianshan Pai | | 22 | Xiao Qiao | Shaolin Pai | | 23 | Ma Chao | Wudang Pai | # | 24 | Xu Xian | NULL | # | 25 | Sun Dasheng | NULL | +-------+---------------+----------------+ 学生表与班级表取左外连接,并且减去学生表与课程表交集部分 select s.stuid,s.name,c.class from students as s left join classes as c on s.classid=c.classid where c.classid is NULL; +-------+-------------+-------+ | stuid | name | class | +-------+-------------+-------+ | 24 | Xu Xian | NULL | | 25 | Sun Dasheng | NULL | +-------+-------------+-------+ 计算女生成绩的平均值 select avg(age) from students group by gender having gender='F';
例如:
> SELECT * FROM students; 从students表中查看所有字段;
+----+-------+-------+------+--------+
| id | name | class | age | gender |
+----+-------+-------+------+--------+
| 1 | kobe | 96 | 38 | m |
| 2 | jodan | | NULL | NULL |
| 3 | oneal | | NULL | NULL |
+----+-------+-------+------+--------+
> SELECT id,name FROM students; 查看指定字段;即投影;
+----+-------+
| id | name |
+----+-------+
| 1 | kobe |
| 2 | jodan |
| 3 | oneal |
+----+-------+
> SELECT id as stuID,name FROM students; 显示时把id字段显示为别名为stuID;
+-------+-------+
| stuID | name |
+-------+-------+
| 1 | kobe |
| 2 | jodan |
| 3 | oneal |
+-------+-------+
> SELECT * FROM students WHERE id=1; 查询表中,id=1的所有信息;
+----+------+-------+------+--------+
| id | name | class | age | gender |
+----+------+-------+------+--------+
| 1 | kobe | 96 | 38 | m |
+----+------+-------+------+--------+
> SELECT * FROM students WHERE name='jodan'; 字符比较使用一个=也可以;
+----+-------+-------+------+--------+
| id | name | class | age | gender |
+----+-------+-------+------+--------+
| 2 | jodan | | NULL | NULL |
+----+-------+-------+------+--------+
> 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 | |
| gender | enum('f','m') | YES | | NULL | |
+--------+---------------------+------+-----+---------+-------+
注意:char类型不区分大小写;
> SELECT name,age FROM students WHERE age > 30 and age < 80; 组合条件查询;
> SELECT name,age FROM students WHERE age >= 30 and age =< 80; 显示age和name字段,且符合age的条件;
> SELECT name,age FROM students WHERE age BETWEEN 30 and 80; 同上,between..and表示范围区间;
> SELECT name FROM students WHERE name LIKE '%n%'; 使用mysql中的模式匹配查看name中包含n的字段;
+-------+
| name |
+-------+
| jodan |
| oneal |
+-------+
> SELECT name FROM students WHERE name RLIKE '^.*n.*$'; 使用正则式模式匹配;查询结果同上;
> SELECT name,age FROM students WHERE age IS NULL; 查询age字段为空,只显示name和age字段;
+-------+------+
| name | age |
+-------+------+
| jodan | NULL |
| oneal | NULL |
+-------+------+
> SELECT name,age FROM students WHERE age IS NOT NULL; age字段不为空的字段;
+------+------+
| name | age |
+------+------+
| kobe | 38 |
+------+------+
> SELECT id,name FROM students ORDER BY name; 根据name字段排序,默认为升序,只显示id和name字段;
+----+-------+
| id | name |
+----+-------+
| 2 | jodan |
| 1 | kobe |
| 3 | oneal |
+----+-------+
> SELECT id,name FROM students ORDER BY name DESC; 根据name降序排序;
注意:广义的查询,在增、删、查、改中都包含查询;
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]
注意:慎用删除;删除指明条件;
例如:
> DELETE FROM students WHERE age IS NULL; 删除age字段为空的行;
> SELECT * FROM students;
+----+------+-------+------+--------+
| id | name | class | age | gender |
+----+------+-------+------+--------+
| 1 | kobe | 96 | 38 | m |
+----+------+-------+------+--------+
注意:删除不指明条件,则整表都删除;
退出数据库,在linux中批量添加;
]# for i in {1..100};do AGE=$[$RANDOM%100];mysql -e "INSERT INTO testdb.students (id,name,age) VALUES ($i,"stu$i",$AGE);";done
]# mysql
> use testdb
> SELECT * FROM student; 显示添加了100个用户;
> SELECT * FROM students ORDER BY age DESC; 手动排序,age字段为降序;
> DELETE FROM students ORDER BY age DESC LIMIT 20; 删除age最大的前20个用户的行;
> UPDATE students SET age=age-5 ORDER BY id DESC LIMIT 5; 只改id最大的前10个用户,把age字段-10;
> SELECT name,age FROM students WHERE name NOT LIKE 'stu%'; 查找name,age字段,name不是stu开头的字段的行;
+------+------+
| name | age |
+------+------+
| kobe | 38 |
+------+------+
> UPDATE students SET age=age-5 WHERE name NOT LIKE 'stu%';查找name字段,不是stu开头的字段,age-5;
> SELECT name,age FROM students WHERE name NOT LIKE 'stu%';
+------+------+
| name | age |
+------+------+
| kobe | 33 |
+------+------+
UPDATE:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1=value1 [, col_name2=value2] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
用户账号及权限管理:
用户账号:'username'@'host'
host:主机名,此用户访问当前mysql服务器时,允许其通过哪些主机远程创建连接;
表示方式:IP,网络地址、主机名、通配符(%和_);
禁止检查主机名:my.cnf
[mysqld]
skip_name_resolve = ON
创建用户账号:
CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
例如:
> CREATE USER 'testuser'@'172.18.%.%.' IDENTIFIED BY 'testpass'; 创建用户testuser,密码为testpass;
> FLUSH PRIVILEGES; 刷新授权表;
]# mysql -utestuser -h172.18.11.2 -p
输入设置的密码登录成功;
> show databases;
删除用户账号:
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],权限还可为CREATE, ALTER, DROP, INSERT, DELETE, UPDATE, SELECT;
db_name.tbl_name:库名.表名;
*.*:所有库的所有表;
db_name.*:指定库的所有表;
db_name.tbl_name:指定库的特定表;
db_name.routine_name:指定库上的存储过程或存储函数;
如果db_name与routine_name同名,只能靠object_type来区别;
[object_type]
TABLE 表
FUNCTION 函数
PROCEDURE 过程
查看用户所获得的授权:
SHOW GRANTS FOR 'user'@'host' 查看指定用户所获得的授权:
SHOW GRANTS FOR CURRENT_USER; 查看当前用户的权限;
例如:
> GRANT SELECT ON testdb.students TO 'testuser'@'172.18.%.%'; 授权testuser用户有SELECT权限,对testdb库中的students表;
> FLUSH PRIVILEGES; 刷新授权;
> exit
]# mysql -utestuser -h172.18.11.2 -ptestpass
> SHOW DATABASES;
> use testdb;
> SELECT * FROM students;
> INSERT INTO students (id,name) VALUES (101,'NIK YANG'); 不能插入,没有insert权限;
> GRANT INSERT ON testdb.students TO 'testuser'@'172.18.%.%';
> FLUSH PRIVILEGES;
> INSERT INTO students (id,name) VALUES (101,'NIK YANG'); 此时有权限插入;
注意:授权时不影响原来的权限;授权是累加的;
> SHOW GRANTS FOR 'testuser'@'172.18.%.%'; 查看指定用户的授权;
> 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命令方可;