ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

11.1 总结

2019-11-02 20:03:07  阅读:252  来源: 互联网

标签:总结 name uu 11.1 sec str mysql id


上节课回顾:

索引:

索引的作用:提高查询效率,好比字典中的目录。

底层原理:B+树,索引本质上就是一个特殊文件。

索引的分类:

主键索引:加快查询速度+不能重复+不能为空

```python

增加:

第一种方法:
create table 表名(
id int auto_increment primary key #主键自增id
)
注意:auto_increment 依赖primary key,而primary key不依赖auto_increment
第二种方法:
alter table 表名 change 旧字段名 新字段名 数据类型 自增 主键;
第三种方法:
alter table 表名 add 主键(字段名)
场景:一般是加在id这列

删除:

如果要删除带有auto_increment的primary key 需要提前删除auto_increment。
alter table 表名 drop 主键;

```

```

​ 唯一索引:加快查询速度+不能重复

##增加:
第一种方法:
create table 表名(
   id int auto_increment primary key, #主键自增id
   phone int not null default 0,
   name varchar(32)
   unique 索引名(phone字段名)
)
第二种方法:
alter table 表名 add unique index 索引名(phone字段名);
第三种方法:
create unique index 索引名 on 表名 (phone字段名);
场景:应用在需要唯一值的时候
##删除:
alter table 表名 drop index 索引名;

联合唯一索引,使用方法同上。

create table user (
        id int auto_increment primary key,
        a int not null default 0,
        b int not null default 0,
        unique ix_ab (a,b)
        )charset utf8;

        insert into user (a,b) values (1,2);
        insert into user (a,b) values (1,3);
        insert into user (a,b) values (3,2);

        mysql> insert into user (a,b) values (1,2);
        ERROR 1062 (23000): Duplicate entry '1-2' for key 'ix_ab'

        mysql> insert into user (a,b) values (1,3);
        Query OK, 1 row affected (0.05 sec)

普通索引作用:加速查找

增加:
第一种方法:
create table 表名(
      id int auto_increment primary key,
      name varchar(32) not null default '',
      index 索引名(name)
)
第二种方法:
alter table 表名 add index 索引名(name);
第三种方法:
create index 索引名 on 表名(字段名);
删除:
alter table 表名 drop index 索引名;
联合(组合)索引
index(name,age)

今日内容:

1.事务:通俗的说,事务指一组操作,要么都执行成功,要么都执行失败

思考:
            我去银行给朋友汇款,
            我卡上有1000元,
            朋友卡上1000元,
            我给朋友转账100元(无手续费),
            如果,我的钱刚扣,而朋友的钱又没加时,
            网线断了,怎么办?
            
演示:开两个cmd,用同一个数据里的同一个表
#第一个cmd:
mysql> create table uu(
    -> id int auto_increment primary key,
    -> name varchar(32) not null default '',
    -> salary int not null default 0
    -> )charset utf8;
Query OK, 0 rows affected (0.03 sec)

mysql> insert into uu (name,salary) values('jshsdh',1000);
Query OK, 1 row affected (0.01 sec)

mysql> insert into uu (name,salary) values('uwgdg',1000);
Query OK, 1 row affected (0.00 sec)
解决方法:
使用事务:
start transaction;
sql语句
例子:
commit/rollback;
commit成功:
mysql> update uu set salary=900 where name='jshsdh';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from uu;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
|  2 | uwgdg  |   1000 |
+----+--------+--------+
2 rows in set (0.00 sec)

mysql> update uu set salary=1100 where name='uwgdg';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from uu;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
|  2 | uwgdg  |   1100 |
+----+--------+--------+
2 rows in set (0.00 sec)
#第二个cmd:
mysql> select * from uu;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |   1000 |
|  2 | uwgdg  |   1000 |
+----+--------+--------+
2 rows in set (0.00 sec)
一查询结果没变
#第一个cmd:
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
#第二个cmd:
mysql> select * from uu;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
|  2 | uwgdg  |   1100 |
+----+--------+--------+
2 rows in set (0.00 sec)
只有第一个cmd提交,第二个cmd才能得到同步。
rollback 回滚:
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> update uu set salary=800 where name='jshsdh';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from uu;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    800 |
|  2 | uwgdg  |   1100 |
+----+--------+--------+
2 rows in set (0.00 sec)

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

mysql> select * from uu;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
|  2 | uwgdg  |   1100 |
+----+--------+--------+
2 rows in set (0.00 sec)

rollback回滚,影响所有:
无论改了几次值,回滚都会让他返回到回滚前的那个最后状态。

事务的特性:(***)

原子性(Atomicity) :原子意为最小的粒子,即不能再分的事务,要么全部执行,要么全部取消(银行转账例子)

一致性(Consistency):指事务发生前和发生后,数据的总额依然匹配

隔离性(Isolation):简单来说,某个事务的操作对其他事务不可见的

持久性(Durability):当事务完成后,其影响应保留下来,不能撤销,只能通过‘补偿性事务’来抵消之前的错误

2.存储引擎:

InnoDB:保时捷引擎

MyIsam:奔奔引擎

建表的时候

mysql> create table uu(
    -> id int auto_increment primary key,
    -> name varchar(32) not null default '',
    -> salary int not null default 0
    -> )engine=Innodb charset utf8;

mysql 5.5 以上,默认用InnoDB引擎。

两个引擎的区别:

​ 1.InnoDB支持事务,MyIsam不支持。

​ 2.InnoDB支持行锁,MyIsam支持表锁。

3.视图:

项目,有100个SQL,其中80个SQL都是:

select * from 表名 where 字段='xxx';

增加视图:(视图是SQL语句的影子,不能修改,底层非常消耗MySQL的性能,一般很少用)

​ create view 视图名 as SQL语句;

删除:

​ drop view v1;

例子:

mysql> select * from uu where name='jshsdh';
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
+----+--------+--------+
1 row in set (0.00 sec)

mysql> create view v1 as select * from uu where name='jshsdh';
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+--------------+
| Tables_in_tt |
+--------------+
| boy          |
| boy2girl     |
| class        |
| course       |
| department   |
| employee     |
| girl         |
| school       |
| score        |
| student      |
| t1           |
| t2           |
| teacher      |
| user         |
| userinfo     |
| uu           |
| v1           |
| xxx          |
+--------------+
18 rows in set (0.00 sec)

mysql> select * from v1;
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
+----+--------+--------+
1 row in set (0.01 sec)

4.触发器:

两张表:订单表 库存表

场景:当我下一个订单的时候,订单表中需要增加一个记录,同时库存表中需要减1。这两个操作是同时发生的,并且前一个操作触发后一个操作。

使用触发器可以定制用户对某一张表的数据进行[增,删,改]操作时前后的行为(没有查询),在进行增删改的时候触发某个动作叫做触发器。其实就是在增删改的时候另外执行了一段SQL语句,触发器是被动调用的,不能由用户直接调用。

使用方法:

增加:

mysql> delimiter //
mysql> select * from t2;
    -> //

mysql>  CREATE TRIGGER tri_before_insert_t2 BEFORE INSERT ON t2 FOR EACH ROW
    ->  BEGIN
    ->  INSERT INTO t1 (NAME) VALUES ('aa');
    ->  END //
Query OK, 0 rows affected (0.02 sec)

mysql>  delimiter ;

查看触发器:

mysql> show triggers\G
*************************** 1. row ***************************
             Trigger: tri_before_insert_t2
               Event: INSERT
               Table: t2
           Statement: BEGIN
 INSERT INTO t1 (NAME) VALUES ('aa');
 END
              Timing: BEFORE
             Created: NULL
            sql_mode: NO_ENGINE_SUBSTITUTION
             Definer: root@localhost
character_set_client: gbk
collation_connection: gbk_chinese_ci
  Database Collation: utf8_general_ci
1 row in set (0.02 sec)

删除:drop trigger 触发器名;

5.存储过程

像一个SQL函数

创建:

mysql> delimiter //
mysql> select * from uu;
    -> //
+----+--------+--------+
| id | name   | salary |
+----+--------+--------+
|  1 | jshsdh |    900 |
|  2 | uwgdg  |   1100 |
+----+--------+--------+
2 rows in set (0.00 sec)

mysql> create procedure p1()
    -> BEGIN
    ->     select * from uu where id=2;
    -> END//
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter;
mysql> call p1();
+----+------+--------+
| id | name | salary |
+----+------+--------+
|  2 | uwgdg|   1100 |
+----+------+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

删除:drop procedure p1;

6.函数

        CHAR_LENGTH(str)
            返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。
            对于一个包含五个二字节字符集, LENGTH()返回值为 10, 而CHAR_LENGTH()的返回值为5。

        CONCAT(str1,str2,...)
            字符串拼接
            如有任何一个参数为NULL ,则返回值为 NULL。
        FORMAT(X,D)
            将数字X 的格式写为'#,###,###.##',以四舍五入的方式保留小数点后 D 位, 并将结果以字符串的形式返回。若  D 为 0, 则返回结果不带有小数点,或不含小数部分。
            例如:
                SELECT FORMAT(12332.1,4); 结果为: '12,332.1000'
        INSTR(str,substr)
            返回字符串 str 中子字符串的第一个出现位置。
        LEFT(str,len)
            返回字符串str 从开始的len位置的子序列字符。
        LOWER(str)
            变小写
        UPPER(str)
            变大写
        LTRIM(str)
            返回字符串 str ,其引导空格字符被删除。
        RTRIM(str)
            返回字符串 str ,结尾空格字符被删去。
        SUBSTRING(str,pos,len)
            获取字符串子序列
        LOCATE(substr,str,pos)
            获取子序列索引位置
        REPEAT(str,count)
            返回一个由重复的字符串str 组成的字符串,字符串str的数目等于count 。
            若 count <= 0,则返回一个空字符串。
            若str 或 count 为 NULL,则返回 NULL 。
        REPLACE(str,from_str,to_str)
            返回字符串str 以及所有被字符串to_str替代的字符串from_str 。
        REVERSE(str)
            返回字符串 str ,顺序和字符顺序相反。
        RIGHT(str,len)
            从字符串str 开始,返回从后边开始len个字符组成的子序列

7.数据库的备份

备份的原因:将重要的数据保存下来。

语法:mysqldump -h 服务器 -u用户名 -p密码 数据库名 表名1,表名2,...>

                #示例:
                #单库备份
                mysqldump -uroot -p123 db1 > db1.sql
                mysqldump -uroot -p123 db1 table1 table2 > db1-table1-table2.sql

                #多库备份
                mysqldump -uroot -p123 --databases db1 db2 mysql db3 > db1_db2_mysql_db3.sql

                #备份所有库
                mysqldump -uroot -p123 --all-databases > all.sql

            重新导入:
                mysql> source D:/test3.sql;

标签:总结,name,uu,11.1,sec,str,mysql,id
来源: https://www.cnblogs.com/lidandanaa/p/11783771.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有