标签:constrain 元素 行记录 rpo 建立 机制 nal 区分 数据库操作
MySQL索引与事务显示参数 | 描述 |
---|---|
Table | 表的名称 |
Non_unique | 索引值的唯一性,0表示唯一性,1表示非唯一 |
Key_name | 索引的名称 |
Seq_in_index | 索引中的序列号从1开始 |
Column_name | 列名称 |
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 学员信息 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use 学院信息;
mysql> create table 班级学员信息(学号 int,名字 varchar(10),分数 int,爱好 varchar(10));
mysql> select * from 班级学员信息;
+--------+--------+--------+--------+
| 学号 | 名字 | 分数 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 88 | 1 |
| 2 | 李四 | 88 | 2 |
| 3 | 王五 | 88 | 1 |
+--------+--------+--------+--------+
3 rows in set (0.00 sec)
mysql> create index name_index on 班级学员信息(学号); #创建普通索引实例
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from 班级学员信息; #查看索引信息
+--------------------+------------+------------+--------------+-------------+---------
--+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collatio
n | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+------------+--------------+-------------+---------
--+-------------+----------+--------+------+------------+---------+---------------+
| 班级学员信息 | 1 | name_index | 1 | 学号 | A
| 3 | NULL | NULL | YES | BTREE | | |
+--------------------+------------+------------+--------------+-------------+---------
--+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> create unique index name_unique_index on 班级学员信息(名字);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from 班级学员信息;
+--------------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--
------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | P
acked | Null | Index_type | Comment | Index_comment |
+--------------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--
------+------+------------+---------+---------------+
| 班级学员信息 | 0 | name_unique_index | 1 | 名字 | A | 3 | NULL | N
ULL | YES | BTREE | | |
+--------------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--
------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> show tables; #在学员信息里面有三个表现在我重新创建一个新表在创建新编的同时定义主键
+------------------------+
| Tables_in_学员信息 |
+------------------------+
| 学生爱好 |
| 爱好 |
| 班级学员信息 |
+------------------------+
3 rows in set (0.00 sec)
mysql> create table 学生兴趣 (编号 int(4)primary key, 爱好 varchar(10));
Query OK, 0 rows affected (0.01 sec)
mysql> desc 学生兴趣; #创建了主键的表看一下两者区别创建主键的表Key里面多一个PRI
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 编号 | int(4) | NO | PRI | NULL | |
| 爱好 | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc 爱好; #原有的表没有创建主键的表没有创建主键的表Key里面是空的
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 编号 | int(4) | YES | | NULL | |
| 爱好 | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create table 学生爱好 (编号 int(4), 爱好 varchar(10));
Query OK, 0 rows affected (0.01 sec)
mysql> desc 学生爱好;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 编号 | int(4) | YES | | NULL | |
| 爱好 | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table 学生爱好 add constraint pk_id primary key 学生爱好 (编号);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc 学生爱好;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 编号 | int(4) | NO | PRI | NULL | |
| 爱好 | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> create index duolie_index on 班级学员信息(学号,名字);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from 班级学员信息;
+--------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| 班级学员信息 | 1 | duolie_index | 1 | 学号 | A | 3 | NULL | NULL | YES | BTREE | | |
| 班级学员信息 | 1 | duolie_index | 2 | 名字 | A | 3 | NULL | NULL | YES | BTREE | | |
+--------------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> alter table 班级学员信息 drop column 学号; #删除多列索引中其中一个索引
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from 六年级二班学生数据表;
+--------+--------+--------+--------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 80 | 1 |
| 2 | 李四 | 81 | 2 |
| 3 | 王五 | 81 | 3 |
| 4 | 赵六 | 99 | 2 |
+--------+--------+--------+--------+
4 rows in set (0.00 sec)
mysql> select * from 六年级二班兴趣爱好表;
+--------+-----------+
| 编号 | 爱好 |
+--------+-----------+
| 1 | 看书 |
| 2 | 玩游戏 |
| 3 | 游玩 |
| 4 | 打球 |
+--------+-----------+
4 rows in set (0.00 sec)
mysql> select i.编号,i.姓名,i.学分,h.爱好 from 六年级二班学生数据表 i join 六年级二班兴趣爱好表 h on i.爱好=
+--------+--------+--------+-----------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+-----------+
| 1 | 张三 | 80 | 看书 |
| 2 | 李四 | 81 | 玩游戏 |
| 4 | 赵六 | 99 | 玩游戏 |
| 3 | 王五 | 81 | 游玩 |
+--------+--------+--------+-----------+
4 rows in set (0.00 sec)
mysql> create database 数据;
Query OK, 1 row affected (0.00 sec)
mysql> use 数据;
Database changed
mysql> use 学院班级信息;
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> begin;
Query OK, 0 rows affected (0.00 sec)
mysql>insert
-> into 六年级二班学生数据表 values (5,‘田七‘,2,1); #在表中插入这段信息
Query OK, 1 row affected (0.00 sec)
mysql> select
-> * from 六年级二班学生数据表;
+--------+--------+--------+--------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 80 | 1 |
| 2 | 李四 | 81 | 2 |
| 3 | 王五 | 81 | 3 |
| 4 | 赵六 | 99 | 2 |
| 5 | 田七 | 2 | 1 |
+--------+--------+--------+--------+
5 rows in set (0.00 sec) #查看表中数据有田七这个人员信息
#我们另开一个终端登入数据库再查看表的信息
mysql> select * from 六年级二班学生数据表;
+--------+--------+--------+--------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 80 | 1 |
| 2 | 李四 | 81 | 2 |
| 3 | 王五 | 81 | 3 |
| 4 | 赵六 | 99 | 2 |
+--------+--------+--------+--------+
4 rows in set (0.00 sec)
#从另一个终端的数据库看不到田七数据因为begin只是开始一个事务并没有提交
#想要提交事务用commit提交 如果怕后面出错可以在这边进行一个留档
mysql> savepoint s1;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into 六年级二班学生数据表 values (6,‘黑八‘,77,4);
Query OK, 1 row affected (0.00 sec)
mysql> savepoint s2;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from 六年级二班学生数据表;
+--------+--------+--------+--------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 80 | 1 |
| 2 | 李四 | 81 | 2 |
| 3 | 王五 | 81 | 3 |
| 4 | 赵六 | 99 | 2 |
| 5 | 田七 | 2 | 1 |
| 6 | 黑八 | 77 | 4 |
+--------+--------+--------+--------+
6 rows in set (0.00 sec)
#如果再第二个留档中出错了用rollback to savepoint s1;
mysql> rollback to savepoint s1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from 六年级二班学生数据表;
+--------+--------+--------+--------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 80 | 1 |
| 2 | 李四 | 81 | 2 |
| 3 | 王五 | 81 | 3 |
| 4 | 赵六 | 99 | 2 |
| 5 | 田七 | 2 | 1 |
+--------+--------+--------+--------+
5 rows in set (0.00 sec)
#此时留档二中的黑八就叫已经不存在了,注:回滚到上一个s1的留档的位置那么从s1下面所有操作的事务全都不存在了
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into 六年级二班学生数据表 values (6,‘黑八‘,77,4);
Query OK, 1 row affected (0.00 sec)
mysql> select * from 六年级二班学生数据表;
+--------+--------+--------+--------+
| 编号 | 姓名 | 学分 | 爱好 |
+--------+--------+--------+--------+
| 1 | 张三 | 80 | 1 |
| 2 | 李四 | 81 | 2 |
| 3 | 王五 | 81 | 3 |
| 4 | 赵六 | 99 | 2 |
| 6 | 黑八 | 77 | 4 |
+--------+--------+--------+--------+
5 rows in set (0.00 sec)
#上表关闭了自动提交功能然后操作事务不会自动提交相当于执行了begin同样在另一个终端登录的时候不会显示出来
#如果set autocommit=1开启自动提交那么以上正在进行的事务自动提交相当于执行了begin语句中的commit
标签:constrain 元素 行记录 rpo 建立 机制 nal 区分 数据库操作
原文地址:http://blog.51cto.com/13645280/2133337