主要内容:
- 常用命令
- 数据类型
- DDL操作
- DML语句
- DCL语句
常用命令
-
登录、退出
- 登录:mysql -u usrName -p
- 退出:exit:回车 || quit || \p
-
查看
- select now();(查看当前时间)
- select curdate();
- select curtime();
- select version();
- select user();
-
数据库
- 查看数据库:
show databases;
。系统自建的数据库:- infomation_schema:数据库对象信息,如用户表、列信息、权限、字符集、分区信息等。
- mysql:存储系统的用户权限信息。
- sys和performance_schema
- 创建:
create database dbname;
- eg: create database demo;
- 删除:
drop database dbname;
- eg: drop database demo;
- 使用:
use dbname;
/进入数据库中
- 查看数据库:
数据类型
- 整型int:
- tinyint 【占1字节:有符号-128~127;无符号:0-255】 表示年龄可以,小数据。
- smallint 【有符号:-3276832767;无符号:065535】
- int 【占4字节:10位数】
- BIGINT 【8字节】
注:bool == tinyint 1
- 浮点型float:
- float 4字节 日常生活小数 【会丢失精度】
- double 4字节 用不到
- DECIMAL[m,d] m总位数,d小数点后的位数。【不会丢失精度】
- 字符串
- char(位数) 定长字符:225
- varchar(位数) 变长字符【常用】
- text 65535个字符
- MEDIUMBLOB 2的24方字符
- enum(val1,val2,val3...)枚举 【eg:enum(“男”,“女”)】
- 日期、时间
- date
- time
- datetime
DDL操作
DDL: data definition languages. 数据表的创建删除修改,命令不区分大小写,貌似。
-
查看表:
查看当前数据库中的所有数据表:
show tables
。查看某一个数据表的定义:
desc tablename
。-
查看创建表的sql语句:
show create table tablename \G
。其中\G
使记录按照字段竖向排列,其实也就是使输出内容更加简洁整齐。mysql> desc pages; +---------+----------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+----------------+------+-----+-------------------+----------------+ | id | bigint(7) | NO | PRI | NULL | auto_increment | | tiltle | varchar(200) | YES | | NULL | | | content | varchar(10000) | YES | | NULL | | | created | timestamp | NO | | CURRENT_TIMESTAMP | | +---------+----------------+------+-----+-------------------+----------------+ mysql> show create table pages \G; *************************** 1. row *************************** Table: pages Create Table: CREATE TABLE `pages` ( `id` bigint(7) NOT NULL AUTO_INCREMENT, `tiltle` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `content` varchar(10000) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 1 row in set (0.00 sec) ERROR: No query specified
-
创建表:
CREATE TABLE tablename( column_name_1 column_type_1 constraints, column_name_1 column_type_1 constraints, ...... column_name_n column_type_n constraints, );
删除表:
DROP TABLE tablename
修改表名:
ALTER TABLE tablename RENAME [TO] new_tablename;
-
修改表:
-
修改表类型:
ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
,例如对pages这个数据表操作,改变content的类型,把varchar(10000)改成varchar(6000)。mysql> alter table pages modify content varchar(6000); Query OK, 72 rows affected (0.94 sec) Records: 72 Duplicates: 0 Warnings: 0 mysql> desc pages; +---------+---------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+-------------------+----------------+ | id | bigint(7) | NO | PRI | NULL | auto_increment | | tiltle | varchar(200) | YES | | NULL | | | content | varchar(6000) | YES | | NULL | | | created | timestamp | NO | | CURRENT_TIMESTAMP | | +---------+---------------+------+-----+-------------------+----------------+ 4 rows in set (0.00 sec)
-
增加表字段,
ALTER TABLE tablename ADD [column] column_definition [FIRST | AFTER col_name];
mysql> alter table pages add column age int(3); Query OK, 0 rows affected (0.81 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc pages; +---------+---------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+-------------------+----------------+ | id | bigint(7) | NO | PRI | NULL | auto_increment | | tiltle | varchar(200) | YES | | NULL | | | content | varchar(6000) | YES | | NULL | | | created | timestamp | NO | | CURRENT_TIMESTAMP | | | age | int(3) | YES | | NULL | | +---------+---------------+------+-----+-------------------+----------------+ 5 rows in set (0.00 sec)
删除字段,
ALTER TABLE tablename DROP [COLUMN] col_name;
-
字段改名,
ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition [FIRST|AFTER col_name];
mysql> ALTER TABLE pages CHANGE tiltle title varchar(100); Query OK, 72 rows affected (0.73 sec) Records: 72 Duplicates: 0 Warnings: 0 mysql> desc pages; +---------+---------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+-------------------+----------------+ | id | bigint(7) | NO | PRI | NULL | auto_increment | | title | varchar(100) | YES | | NULL | | | content | varchar(6000) | YES | | NULL | | | created | timestamp | NO | | CURRENT_TIMESTAMP | | | age | int(3) | YES | | NULL | | +---------+---------------+------+-----+-------------------+----------------+ 5 rows in set (0.00 sec)
注:change和modify都可以改变表的定义(数据类型),不同之处在于change需要写两次列名,可以同时更改列名;而modify只需要写一次列名,但不能更改列名。
-
修改字段排列顺序。用可选项
[FIRST|AFTER col_name]
。比如把age放到最前。mysql> alter table pages modify age int(3) first; Query OK, 0 rows affected (0.64 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc pages; +---------+---------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+-------------------+----------------+ | age | int(3) | YES | | NULL | | | id | bigint(7) | NO | PRI | NULL | auto_increment | | title | varchar(100) | YES | | NULL | | | content | varchar(6000) | YES | | NULL | | | created | timestamp | NO | | CURRENT_TIMESTAMP | | +---------+---------------+------+-----+-------------------+----------------+ 5 rows in set (0.00 sec)
-
DML语句
DML: data manipulation language. 数据库中表记录的插入(insert)更新(update)删除(delete)和查询(select)
-
插入记录:
INSERT INTO tablename (field1, field2, ... fieldn) VALUES(value1, value2, ... valuen);
mysql> insert into pages (age,id,title) values(13, 12306, '第一条数据'); Query OK, 1 row affected (0.05 sec) mysql> select * from pages; +------+-------+------------+---------+---------------------+ | age | id | title | content | created | +------+-------+------------+---------+---------------------+ | 13 | 12306 | 第一条数据 | NULL | 2017-02-09 15:24:02 | +------+-------+------------+---------+---------------------+ 1 row in set (0.01 sec)
添加多条数据
INSERT INTO tablename (field1, field2, ... fieldn) VALUES (record1_value1, record1_value2, ... record1_valuen), (record2_value1, record2_value2, ... record2_valuen), ... (recordn_value1, recordn_value2, ... recordn_valuen), ;
-
更新记录:更改记录中的值,
UPDATE tablename SET field1=value1, field2=value2,...,fieldn=valuen [WHERE CONDITION];
mysql> update pages set content='第一条数据的内容blablabla' where id=12306; Query OK, 1 row affected (0.07 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from pages; +------+-------+------------+---------------------------+---------------------+ | age | id | title | content | created | +------+-------+------------+---------------------------+---------------------+ | 13 | 12306 | 第一条数据 | 第一条数据的内容blablabla | 2017-02-09 15:24:02 | +------+-------+------------+---------------------------+---------------------+ 1 row in set (0.00 sec)
可以对两个表同时操作,语法:
UPDATE t1,t2,...,tn set t1.field1=expr1, tn.fieldn=exprn [WHERE CONDITION];
。t1 t2是tablename。eg:
update table1 a, table2 b set a.column1=a.colomn1*b.column2, b.column1=a.column2 where a.column3=b.column2;
,这里的ab是别名。 -
删除记录:
DELETE FROM tablename [WHERE CONDITON];
- 删除多个表里的数据:
DELETE t1, t2,...,tn FROM t1, t2,...,tn [WHERE CONDITON];
- 删除所有记录:
DELETE FROM tablename;
||TRUNCATE TABLE tablename
,前者返回删除的记录数目,后者返回0。如果有自增项目,不希望删除后回到1的话,使用DELETE FROM tablename WHERE 1;
。
- 删除多个表里的数据:
-
查询记录:
SELECT * FROM tablename [WHERE CONDITION];
- 查询不重复记录【distinct】:
select distinct column from tablename;
- 条件查询【where】
- 排序【order by】:
select * from tablename [Where CONDITON] [ORDER BY field1 [DESC|ASC], field1 [DESC|ASC]];
- 限制【limit】:
select ... [LIMIT offset_start, raw_count]
,其中offset_start
是其实偏移量,默认为0,raw_coun
t是显示的行数。 - 聚合:也就是分类汇总。
- 表链接
- 子查询:关键字包括
in
、not in
、=
、!=
、exists
、not exists
。
- 查询不重复记录【distinct】:
查询语句应该是MySQL中最复杂的了,之后再从新整理一份关于查询语句的笔记。
DCL语句
DCL: data control language. 主要是DBA用来管理系统中的对象权限时使用。
例,创建一个数据库用户chz,具有对scaping数据库中所有数据表的insert和select权限:
mysql> grant select, insert on scraping.* to 'chz'@'localhost' identified by '2333';
Query OK, 0 rows affected, 1 warning (0.12 sec)
之后退出root用户,用mysql -uchz -p2333
登陆,就可以对scraping数据库进行操作了。
C:\Users\cc>mysql -uchz -p2333
mysql: [Warning] Using a password on the command line interface can be insecure.
......
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use scraping;
Database changed
mysql> insert into pages (age,id,title) values(14, 12307, '第二条数据');
Query OK, 1 row affected (0.05 sec)
mysql> select * from pages;
+------+-------+------------+---------------------------+---------------------+
| age | id | title | content | created |
+------+-------+------------+---------------------------+---------------------+
| 13 | 12306 | 第一条数据 | 第一条数据的内容blablabla | 2017-02-09 15:24:02 |
| 14 | 12307 | 第二条数据 | NULL | 2017-02-09 17:30:57 |
+------+-------+------------+---------------------------+---------------------+
2 rows in set (0.00 sec)
移除权限:登陆root用户,执行revoke insert on scraping.* from 'chz'@'localhost';
,取消chz用户的insert权限。这时用chz登陆就只能进行select操作,而不能insert或者其他。
mysql> use scraping;
Database changed
mysql> insert into pages (age,id,title) values(14, 12308, '第san条数据');
ERROR 1142 (42000): INSERT command denied to user 'chz'@'localhost' for table 'pages'
mysql> delete from pages where id=12307;
ERROR 1142 (42000): DELETE command denied to user 'chz'@'localhost' for table 'pages'
mysql> select * from pages;
+------+-------+------------+---------------------------+---------------------+
| age | id | title | content | created |
+------+-------+------------+---------------------------+---------------------+
| 13 | 12306 | 第一条数据 | 第一条数据的内容blablabla | 2017-02-09 15:24:02 |
| 14 | 12307 | 第二条数据 | NULL | 2017-02-09 17:30:57 |
+------+-------+------------+---------------------------+---------------------+
2 rows in set (0.00 sec)