11.1 summary

Review last lesson:

index:

The role of the index: improve query efficiency, like a dictionary catalog.

The underlying principle: B + tree is a special file on the index nature.

Index Category:

Primary key index: speed up queries can not be repeated + + can not be empty

```python

increase:

The first method:
the Create the Table table name (
the above mentioned id # int auto_increment primary key primary key increment the above mentioned id
)
Note: auto_increment dependent on primary key, and the primary key is not dependent auto_increment
second method:
the ALTER the Table table name change old field name the new field name increment primary key data type;
third method:
ALTER table add the primary key table name (field name)
scene: this is generally added to the column id

delete:

If you want to delete the primary key auto_increment with the need to remove auto_increment advance.
alter table drop primary key table name;

```

```

The only index: + speed up queries can not be repeated

##增加:
第一种方法:
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 索引名;

Co-unique index, use the method above.

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)

The general index action: accelerate Find

增加:
第一种方法:
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)

Today's content:

1. Transaction: In layman's terms, a transaction refers to a set of operations are executed either succeed or fail is executed

思考:
            我去银行给朋友汇款,
            我卡上有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回滚,影响所有:
无论改了几次值,回滚都会让他返回到回滚前的那个最后状态。

Characteristics of the transaction: (***)

Atomicity (Atomicity): means the smallest atomic particles, that can not be divided transaction, either all executed or the abolition of all (bank transfer example)

Consistency (Consistency): refers to transactions that occurred before and after the occurrence, the total amount of data still matches

Isolation (Isolation): In simple terms, the operation of a transaction are not visible to other transactions

Persistent (Durability): When the transaction is completed, its impact should be preserved, can not be undone, it can only be offset by a 'compensatory affairs' before the error

2. Storage Engine:

InnoDB: Porsche engine

MyIsam: Benni engine

When construction of the table

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 or higher, with InnoDB the default engine.

The difference between the two engines:

1.InnoDB support services, MyIsam not supported.

2.InnoDB support line lock, MyIsam support table lock.

3. view:

Project, there are 100 SQL, SQL 80 of which are:

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

Increase the view :( view is the shadow of the SQL statement can not be modified, consuming the underlying MySQL performance, it seldom used)

create view view name as SQL statements;

delete:

​ drop view v1;

example:

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. Trigger:

Two tables: Orders table inventory table

Scene: When I place an order, order the table need to add a record, while the need to reduce the inventory table 1. These two operations occur simultaneously, before and after the operation to trigger an action.

Triggers can be customized using user data for a particular table were [add, delete, change] behavior before and after the operation (no query), making additions and deletions to trigger an action when a trigger is called. In fact, when the additions and deletions to perform some additional SQL statements can not be called directly by the user is a passive trigger call.

Instructions:

increase:

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 ;

View Triggers:

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)

Delete: drop trigger trigger name;

The stored procedure

Like a SQL function

create:

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)

Delete: drop procedure p1;

6. Functions

        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. Database Backup

The reason the backup: Save important data down.

Syntax: mysqldump -h -u user name -p password server database name watches 1, table 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;

Guess you like

Origin www.cnblogs.com/lidandanaa/p/11783771.html