mysql-linux-3

上午:
MySQL存储引擎
作为可插拔的组件提供
-mysql服务软件自带的功能程序,处理表的处理器
-不同的存储引擎有不同的功能和数据存储方式
默认的存储引擎
MYSQL 5.0/5.1 ----MyISAM
MySQL 5.5/5.6 ----InnoDB
mysql-5.7.17社区开源板
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)


查看表使用的存储引擎
mysql> show create table t32;
+-------+--------------------------------------------------------------------------------------------+
| Table | Create Table                                                                               |
+-------+--------------------------------------------------------------------------------------------+
| t32   | CREATE TABLE `t32` (
  `name` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


建表时指定表使用的存储引擎
mysql> create table t32(name char(10))engine=myisam;
Query OK, 0 rows affected (0.32 sec)


mysql> show create table t32;
+-------+--------------------------------------------------------------------------------------------+
| Table | Create Table                                                                               |
+-------+--------------------------------------------------------------------------------------------+
| t32   | CREATE TABLE `t32` (
  `name` char(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


修改表使用的存储引擎


mysql> alter table t32 engine=InnoDB
    -> ;
Query OK, 0 rows affected (0.28 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> show create table t32;
+-------+--------------------------------------------------------------------------------------------+
| Table | Create Table                                                                               |
+-------+--------------------------------------------------------------------------------------------+
| t32   | CREATE TABLE `t32` (
  `name` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


查看数据库服务使用的存储引擎
mysql> show engines;
建表时指定使用的存储引擎
mysql> create table t32(name char(10))engine=myisam;
Query OK, 0 rows affected (0.32 sec)
修改数据库服务软件默认使用的存储引擎
[root@localhost ~]# cat /etc/my.cnf
default-storage-engine=myisam
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# systemctl start mysqld
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | YES     | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | DEFAULT | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)


mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A


Database changed
mysql> create table t34(age int);
Query OK, 0 rows affected (0.19 sec)


mysql> use db1;
Database changed
mysql> show create table t34;
+-------+------------------------------------------------------------------------------------------+
| Table | Create Table                                                                             |
+-------+------------------------------------------------------------------------------------------+
| t34   | CREATE TABLE `t34` (
  `age` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
工作中怎么选用存储引擎




常用存储引擎特点
myisam
不支持事务、事务回滚、外键
支持表级锁
存储方式:一个表对应3个存储文件
            表名.frm  表结构
            表名.MYD 数据
             表名.NYI 索引
事务:对数据库服务的访问过程(连接数据库服务器、操作数据、断开连接)
支持事务的表 有对应的事务日志文件记录执行过的SQL操作
事务特性:原子性(操作要么成功要么失败),一致性(操作没有完成,表中的记录是没有变化的),隔离性(操作隔离),持久性(数据提交则数据永久改变)
ib_buffer_pool  
ibdata1         
ib_logfile0 




事务回滚:在事务执行过程中,任何一步操作失败,都会恢复之前的所有操作
mysql> create table t36(id int)engine=innodb;
Query OK, 0 rows affected (0.23 sec)


mysql> insert into t36 values(100);
Query OK, 1 row affected (0.07 sec)


mysql> select *from t36;
+------+
| id   |
+------+
|  100 |
+------+
1 row in set (0.00 sec)


mysql> show variables like "autocommit";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.01 sec)


mysql> set autocommit=off;
Query OK, 0 rows affected (0.00 sec)


mysql> show variables like "autocommit";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)


mysql> delete form t36;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't36' at line 1
mysql> delete from t36;
Query OK, 1 row affected (0.00 sec)


mysql> select * from t36;
Empty set (0.00 sec)


mysql> roolback
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'roolback' at line 1
mysql> rollback;
Query OK, 0 rows affected (0.05 sec)


mysql> select * from t36;
+------+
| id   |
+------+
|  100 |
+------+
1 row in set (0.00 sec)


mysql> commit
    -> ;
Query OK, 0 rows affected (0.00 sec)


mysql> select * from t36;
+------+
| id   |
+------+
|  100 |
+------+
1 row in set (0.00 sec)


mysql> set autocommit=ON;
Query OK, 0 rows affected (0.00 sec)


innodb
支持事务、事务回滚、行级锁、外键
存储方式:一个表对应2个存储文件
           表名.frm 表结构
           表名.ibd 数据和索引
锁粒度:
行级锁 innodb 只给当前被访问的行加锁,没有被访问的不加锁i
表级锁 myisam  只要对表做访问无论访问1行还是10行都回把整张表锁上


锁:解决的是并发访问冲突访问。
根据客户端的访问类型,锁又分为
读锁  共享锁 select
写锁  排他锁 互斥锁 insert update delete
建表是根据存储引擎的特点决定表使用那种存储引擎
写操作多的表适合使用innodb存储引擎,此引擎支持行级锁,这样对表的并发访问量大。
查询操作多的表适合使用myisam存储引擎,此引擎支持表级锁,只锁一次这样可以节省系统资源。


二。数据导入导出(批量操作数据)
2.1 数据导入的命令格式及数据导入时的注意事项
导入数据的命令格式:
数据导入:把系统文件的内容存储到数据昆服务器的表里。
把系统已有用户的信息保存到db3库下的usertab表里。


导入数据
1.把需要导入的数据放入到相应的文件夹中
mysql> show variables like "secure_file_priv";
+------------------+-----------------------+
| Variable_name    | Value                 |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.00 sec)
[root@localhost lib]# cd mysql-files/
[root@localhost mysql-files]# ls
[root@localhost mysql-files]# cp /etc/passwd /var/lib/mysql-files/
2.导入数据
mysql> load data
    -> infile "/var/lib/mysql-files/passwd"
    -> into table user1
    -> fields terminated by ":"
    -> lines terminated by "\n";
Query OK, 41 rows affected (0.00 sec)
Records: 41  Deleted: 0  Skipped: 0  Warnings: 0
mysql> alter table user1
    -> add
    -> id int(2) primary key auto_increment first;
Query OK, 41 rows affected (0.06 sec)
Records: 41  Duplicates: 0  Warnings: 0


下午课程:
3.更改指定的导入的路茎
[root@localhost ~]# mkdir /mydata
[root@localhost ~]# chown mysql /mydata/
[root@localhost ~]# ls -ld /mydata/
drwxr-xr-x. 2 mysql root 6 6月  27 10:37 /mydata/
[root@localhost ~]# gentenforce
bash: gentenforce: 未找到命令...
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
secure_file_priv="/mydata"
[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# systemctl start mysqld
mysql> show variables like "secure_file_priv";
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| secure_file_priv | /mydata/ |
+------------------+----------+
1 row in set (0.00 sec)


数据导出
mysql> select * from user1 into outfile
    -> "/mydata/user.txt";
Query OK, 123 rows affected (0.00 sec)
mysql> select name from user1 into outfile "/mydata/user3.txt";
Query OK, 123 rows affected (0.00 sec)
[root@localhost mydata]# ls
passwd  user2.txt  user3.txt  user.txt
在导出的数据的行和列加相应的间隔符号
mysql> select * from user1 into outfile "/mydata/user9\.txt" fields terminated by ":"  lines terminated by "\n";
Query OK, 123 rows affected (0.00 sec)


管理表记录
mysql> desc user1;
+----------+-----------+------+-----+---------+-------+
| Field    | Type      | Null | Key | Default | Extra |
+----------+-----------+------+-----+---------+-------+
| name     | char(30)  | YES  | MUL | NULL    |       |
| password | char(1)   | YES  |     | NULL    |       |
| uid      | int(11)   | YES  |     | NULL    |       |
| gid      | int(11)   | YES  |     | NULL    |       |
| comment  | char(100) | YES  |     | NULL    |       |
| homefir  | char(150) | YES  |     | NULL    |       |
| shell    | char(50)  | YES  |     | NULL    |       |
+----------+-----------+------+-----+---------+-------+
7 rows in set (0.01 sec)


mysql> load data infile "/mydata/passwd" into table user1 fields terminated by ":" lines terminated by "\n";
Query OK, 41 rows affected (0.00 sec)
Records: 41  Deleted: 0  Skipped: 0  Warnings: 0


mysql> alter table user1 add id int(2) primary key auto_increment first;
Query OK, 41 rows affected (0.33 sec)
Records: 41  Duplicates: 0  Warnings: 0




insert 插入的四种方式
注意事项
-字段值要与字段类型相匹配
-对应字符类型的字段,要用双或单引号括起来
-依次给所有字段赋值,字段名可以省
只给一部分字段赋值时,必须明确写出对应的字段名称
mysql> insert into user1 values(42,"bob","x",3000,3000,"student user","/home/bob","/bin/bash");
Query OK, 1 row affected (0.00 sec)


mysql> insert into user1 values(43,"bob","x",3000,3000,"student user","/home/bob","/bin/bash"),(44,"bbb","x",3000,3000,"student user","/home/bob","/bin/bash");
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0


mysql> insert into user1(name,uid ,gid) values ("lili",29,30);
Query OK, 1 row affected (0.00 sec)


mysql> insert into user1(name,uid ,gid) values ("lili",29,30),("yy",29,32);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0


select查询的两种方式
mysql> select * from user1;
mysql> select * from user1 where id =32;


update
修改全部
mysql> update user1 set password ="A";
Query OK, 47 rows affected (0.00 sec)
Rows matched: 47  Changed: 47  Warnings: 0
按条件修改
mysql> update user1 set password ="A" where name="root";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0


delect


mysql> delete from user1 where id=43;
Query OK, 1 row affected (0.00 sec)


mysql> delete from user1 


匹配条件
基本匹配条件:适用于select update delete
数值比较 > < != >= <= =
select * from user1 where id =10;
update user1 set password="B" where id<=10;
delete from user1 where uid >=3000;
字符比较 = !=
select * from user1 where name="root";
select name,shell from user1 where name!="root";


匹配空 is null
匹配非空 is not null
select name,uid from user1 where id is null;
update user1 set uid=250 where uid is null;
delete from user1 where uid  is null;


逻辑比较 
and 
逻辑与 and 
     或 or
     非 ! not 取反


范围内匹配
in(值列表) 在...里...
delete from user1 where name in ("zhangsan","mysql","rsync","apache");
not in(值列表) 不在..里..


Between 数字1 and 数字2 在..之间
select * from user1 where id between 10 and 25;
DISTINCT 字段名 去重显示
select  distinct shell from user1 where uid>=100;
高级匹配条件:适用于select update delete
模糊匹配like
-一个字符
% 0个或者多个
insert into user1(id,name)values(67,null),(69,"");  ---null是代表什么都没有,不是0


select name from user1 where name like '_';
select name from user1 where name like '_%_';
select name from user1 where name like 'a_';
select name from user1 where name like '%';


正则匹配 regrexp '正则表达式'
^开头
$结尾
[ ]
* 0个或者多个
.位数
select name from user1 where name regexp '^a'; 以a开头的
select name from user1 where name regexp 'a';  包含a
select name from user1 where name regexp '[0-9]'; 名字里面有数据的
select name from user1 where name regexp '[0-9]$';
select name,uid from user1 where uid regexp '...';
select name,uid from user1 where uid regexp '^...$';
select name,uid from user1 where uid regexp 'r.*t';
select name,uid from user1 where name regexp '^r.*t$';


四则运算 适用于 select update
+
-
*
/
%
select name,uid from user1 where uid >=10 and uid <=20;
update user1 set uid=uid+1 where uid >=10 and uid <=20;
alter table user1 add age int(2) default 19 after name;
select name,age from user1 where name="root";
select name,2018-age s_year from user1 where name="root";
select name,uid,gid,(uid+gid)/2 pjz from user1;





















































































































































猜你喜欢

转载自blog.csdn.net/zhydream77/article/details/80855564
今日推荐