MySQL数据库以及表的管理
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
今天我们探讨的话题就是如何使用MySQL做开发,我们运维的主要工作不是去开发SQL的,但尽管如此,我们有可能需要对别人写出来的复杂SQL语句做性能评估,去分析他们写的SQL语句是不是足够高效,如果不是的话,我们还要能够达到对对方的SQL语句做改写的能力。所以,我们无需自行去开发SQL,但是我们一定要有一个看懂别人写的SQL的能力哟!
一.DBA的职责
1.开发DBA:
负责数据库设计(E-R关系图)、sql开发、内置函数、存储历程(存储过程和存储函数)、触发器、时间调度器(event scheduler)
2.管理DBA:
负责安装、升级、备份、恢复、用户管理、权限管理、监控、分析、基准测试,语句优化(SQL语句)、数据字典,按需要配置服务器(服务器变量:MyISAM,InnoDB,缓存,日志)
二.SQL语言组成部分
1.DDL:
全称Data Defination,我们叫它数据定义语言,典型命令指令有CREAT/ALTER/DROP
2.DML:
全称Data Manipulation,我们叫它数据操作语言,典型命令有INSERT/DELETE/SELECT/UPDATE
3.完整性定义语言,DDL的一部分功能
主键约束、外键约束、唯一键约束、条件约束、非空约束、事务约束
4.视图定义:即虚表,它是存储下来的select语句
5.事务控制:
例如Transactions(在mysql交互界面执行“HELP content”可以查看相关信息。)
6.嵌入式SQL和动态SQL:
7.DCL:
我们叫它数据控制语言,如实现授权和权限收回的典型命令有GRANT/REVOKE.
三.数据类型的功用
MySQL的数据类型请参考:http://www.cnblogs.com/yinzhengjie/p/7818092.html
1.存储的值类型;
2.占据的存储空间大小;
3.定长,变长;
4.如何被索引及排序;
5.是否能够被索引;
四.数据字典:依赖系统编目(花名册)(system catalog)
对于关系型数据库来讲,它的数据字典也是另外找个地方存起来的。对于MySQL数据库来讲,这个位置就是名称为mysql的数据库。我们在第一次启动MySQL时,它第一步工作就是初始化系统表,所谓初始化系统表就是用来创建mysql这个数据库的。我们也可以称这个mysql数据库叫做MySQL的数据字典。数据字典是用来保存数据库服务器上的元数据。那么什么是元数据呢?我总结有以下几点:
1>.保存关系(表)的名字
2>.保存每个关系(表)的各字段的名字
3>.保存各字段的数据类型和长度
4>.保存约束条件
5>.保存每个关系(表)上的视图的名字及视图的定义
6>.保存授权用户(user表)的名字
7>.保存用户的授权和账户信息等
8>.统计类的数据,如每个关系字段的个数,每个关系中行数,每个关系的存储方法
9>.保存元数据的数据库( 即:information_schema,mysql, performance_schema)
五.MySQL内部组件
如下图所示,连接器(Connectors)和连接池他们之间是建立连接关系的。连接池(Connection Pool)的所有SQL语句都得发送给SQL接口(SQL Interface)进行接收,然后再由分析器(Parser)进行分析,由优化器(Optimizer)进行优化处理,最终我们有可能在缓存(Caches&Buffers)中获取数据,实在再不行在交由存储引擎(Pluggable Storage Engines)去执行SQL语句。
六.MySQL中字符大小写情况说明
1.SQL关键字及函数不区分大小写;
2.数据库、表及视图名称的大小写区分与否取决于底层OS(操作系统)及FS(文件系统);
3.存储过程、存储函数及事件调度器的名字不区分大小写,但触发器区分大小写;
4.表别名区分大小写;
5.对字段中的数据,如果字段类型为binary类型,则区分大小写,非binary不区分大小写;
七.SQL指令详解-数据库操作
1.数据库的创建
a>.查看创建库时的帮助信息
mysql> help create database
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ... create_specification:
[DEFAULT] CHARACTER SET [=] charset_name #设置字符集
| [DEFAULT] COLLATE [=] collation_name #设置排序方式 CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE. URL: http://dev.mysql.com/doc/refman/5.1/en/create-database.html mysql>
b>.创建不存在的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
rows in set (0.00 sec) mysql>
mysql> create database yinzhengjie;
Query OK, row affected (0.01 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| yinzhengjie |
+--------------------+
rows in set (0.00 sec) mysql>
c>.已经存在数据库的如何使用创建命令
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| yinzhengjie |
+--------------------+
rows in set (0.00 sec) mysql> create database if not exists yinzhengjie;
Query OK, row affected, warning (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| yinzhengjie |
+--------------------+
rows in set (0.00 sec) mysql>
2.数据库的删除
a>.查看删除数据库时的帮助信息
mysql> help drop database;
Name: 'DROP DATABASE'
Description:
Syntax:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name DROP DATABASE drops all tables in the database and deletes the
database. Be very careful with this statement! To use DROP DATABASE,
you need the DROP privilege on the database. DROP SCHEMA is a synonym
for DROP DATABASE. *Important*: When a database is dropped, user privileges on the
database are not automatically dropped. See [HELP GRANT]. IF EXISTS is used to prevent an error from occurring if the database
does not exist. URL: http://dev.mysql.com/doc/refman/5.1/en/drop-database.html mysql>
b>.删除数据库操作
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| yinzhengjie |
+--------------------+
rows in set (0.00 sec) mysql> drop database if exists yinzhengjie;
Query OK, rows affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
rows in set (0.00 sec) mysql>
3.修改数据库的字符集和排序字符以及数据字典
mysql> help alter database
Name: 'ALTER DATABASE'
Description:
Syntax:
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name #设置字符集
| [DEFAULT] COLLATE [=] collation_name #修改排序方式 ALTER DATABASE enables you to change the overall characteristics of a
database. These characteristics are stored in the db.opt file in the
database directory. To use ALTER DATABASE, you need the ALTER privilege
on the database. ALTER SCHEMA is a synonym for ALTER DATABASE.
....
mysql>
4.数据库改名操作
如果我们在创建数据库的时候把数据库的名字起错了,这个时候你想要讲数据库名字改正过来。其实目前修改数据库名称基本上没有什么很好的办法,一个比较明智的做法就是讲该数据库的数据全部备份出来,然后将该库删除掉,创建你想要的数据库名称然后再把数据导入进去。还有一种非常暴力的做法就是去MySQL数据库目录下将创建错的目录进行改名操作,这种做法虽然是把数据库名称改正过来了,但是对于该库的数据字典并没有修改哟,因此这种做法我是不推荐去这样做的。
八.SQL指令详解-表的基本操作
1.MyISAM和InnoDB存储的区别概要
MyISAM表,每个表有三个文件,都位于数据库目录中
tb_name.frm:表结构定义
tb_name.MYD:数据文件
tb_name.MYI:索引文件
InnoDB表,有两种存储方式
第一种(默认方式):每表有一个独立文件和一个多表共享的文件
tb_name.frm:表结构的定义,位于数据库目录中
ibdata#:共享的表空间文件,默认位于数据目录(datadir指向的目录)中
第二种(独立的表空间文件,推荐使用这一种方式):
tb_name.frm:每表有一个表结构文件
tb_name.ibd:一个独立的表空间文件
应该修改innodb_file_per_table为ON,我们可以在mysql的配置文件中的[msyqld]下的字段修改它的值为NO即可完成永久生效哟。
2.表的第一种方式,即自定义新表格式
CREATE [TEMPORARY(临时表,保存在内存中)] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
(create_definition,...)
字段的定义:字段名、类型和类型修饰符
键、索引和约束
primary key,unique key,foreign key,check
{index|key}
[table_options]
engine [=] engine_name
AUTO_INCREMENT [=] value 指定AUTO_INCREMENT的起始值
[DEFAULT] CHARACTER SET [=] charset_name 指定默认字符集
CHECKSUM [=] { | } 是否使用校验值
[DEFAULT] COLLATE [=] collation_name 排序规则
COMMENT [=] 'string' 注释
DELAY_KEY_WRITE [=] { | } 是否启用键延迟写入
ROW_FORMAT [=] {DEFAULT(默认)|DYNAMIC(动态)|FIXED(静态)|COMPRESSED(压缩)|REDUNDANT(冗余)|COMPACT(紧致)} 表格式
TABLESPACE tablespace_name [STORAGE {DISK|MEMORY|DEFAULT}] 表空间
mysql> help create table
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
....
mysql>
查看创建表的帮助信息
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
row in set (0.00 sec) mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
rows in set (0.00 sec) mysql> create database if not exists yinzhengjie;
Query OK, row affected (0.00 sec) mysql> use yinzhengjie
Database changed
mysql>
mysql> select database();
+-------------+
| database() |
+-------------+
| yinzhengjie |
+-------------+
row in set (0.00 sec) mysql> create table t1 (Name varchar() not null,Age tinyint unsigned not null,primary key(Name,Age)); #创建一个表,定义Name字段类型自动变化长度的字符类型(varchar),不能为空,定义一个Age字段类型为微整形(tinyint),也不能为空,定义主键(primary key)是Name和Age两个字段。
Query OK, rows affected (0.08 sec) mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
创建一个表包含主键的表
mysql> show table status like 't1'\G; #查看之前创建表的存储引擎。
*************************** . row ***************************
Name: t1
Engine: MyISAM
Version:
Row_format: Dynamic
Rows:
Avg_row_length:
Data_length:
Max_data_length:
Index_length:
Data_free:
Auto_increment: NULL
Create_time: -- ::
Update_time: -- ::
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
row in set (0.00 sec) ERROR:
No query specified mysql>
mysql> drop table t1;
Query OK, rows affected (0.00 sec) mysql> create table t1 (Name varchar() not null,Age tinyint unsigned not null,primary key(Name,Age)) engine='InnoDB';
Query OK, rows affected (0.05 sec) mysql> show table status like 't1'\G;
*************************** . row ***************************
Name: t1
Engine: InnoDB
Version:
Row_format: Compact
Rows:
Avg_row_length:
Data_length:
Max_data_length:
Index_length:
Data_free:
Auto_increment: NULL
Create_time: -- ::
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
row in set (0.00 sec) ERROR:
No query specified mysql>
创建表的存储引擎
3.表的第二种创建方式,即复制表数据
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
select_statement #将select的结果来作为字段创建新表的字段,但是可能失去属性定义的。
用法格式
mysql> select * from t1;
Empty set (0.01 sec) mysql>
mysql> insert into t1(Name,Age) values ("yinzhengjie",);
Query OK, row affected (0.00 sec) mysql>
mysql> select * from t1;
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
+-------------+-----+
row in set (0.00 sec) mysql>
mysql> create table t2 select * from t1;
Query OK, row affected (0.01 sec)
Records: Duplicates: Warnings: mysql> select * from t2;
+-------------+-----+
| Name | Age |
+-------------+-----+
| yinzhengjie | |
+-------------+-----+
row in set (0.00 sec) mysql>
mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> desc t2; #我们可以发现t2的表中的字段属性和t1的并不一致哟!只是数值一致而已。
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | | NULL | |
| Age | tinyint() unsigned | NO | | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
案例展示
4.表的第三种创建方式,即复制表结构
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
用法格式
mysql> select database();
+-------------+
| database() |
+-------------+
| yinzhengjie |
+-------------+
row in set (0.00 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| t1 |
| t2 |
+-----------------------+
rows in set (0.00 sec) mysql> create table t3 like t1;
Query OK, rows affected (0.04 sec) mysql> select * from t3;
Empty set (0.01 sec) mysql> desc t3;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.01 sec) mysql>
用法展示
5.创建一张表的思想
我们可以自定义表,重新创建表的结构,当然也可以通过第三种方式找一个符合我们要求的表结构复制出来,然后通过insert语句将数据插入到这个心创建的表中即可。
6.表的删除
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
用法格式
mysql> select database();
+-------------+
| database() |
+-------------+
| yinzhengjie |
+-------------+
row in set (0.00 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| t1 |
| t2 |
| t3 |
+-----------------------+
rows in set (0.00 sec) mysql> drop table t2,t3;
Query OK, rows affected (0.07 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| t1 |
+-----------------------+
row in set (0.00 sec) mysql>
用法展示
7.表的修改操作
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
修改字段定义:
插入新字段:
ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name ]
删除字段
DROP [COLUMN] col_name
修改字段
修改字段名称
CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
修改字段类型及属性等
MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
表改名:
rename to|as new tb_name
修改存储引擎
engine =
指定排序标准的字段
ORDER BY col_name [, col_name] ...
用法格式集合
mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> alter table t1 add ID tinyint unsigned not null;
Query OK, row affected (0.02 sec)
Records: Duplicates: Warnings: mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
| ID | tinyint() unsigned | NO | | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
插入新字段用法展示
mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
| ID | tinyint() unsigned | NO | | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
mysql> alter table t1 add Gender enum('boy','girl') not null default 'boy' after Age; #插入的字段我们用关键字“after”指定在“Age”之后。(注意,如果你使用first则表示插入在第一个字段哟)
Query OK, row affected (0.05 sec)
Records: Duplicates: Warnings: mysql> desc t1;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
| Gender | enum('boy','girl') | NO | | boy | |
| ID | tinyint() unsigned | NO | | NULL | |
+--------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
插入指定的位置用法展示
mysql> desc t1;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
| Gender | enum('boy','girl') | NO | | boy | |
| ID | tinyint() unsigned | NO | | NULL | |
+--------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
mysql> alter table t1 drop Gender;
Query OK, row affected (0.02 sec)
Records: Duplicates: Warnings: mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
| ID | tinyint() unsigned | NO | | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
删除字段案例展示
mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
| ID | tinyint() unsigned | NO | | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> alter table t1 modify ID tinyint unsigned not null first;
Query OK, row affected (0.02 sec)
Records: Duplicates: Warnings: mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| ID | tinyint() unsigned | NO | | NULL | |
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
修改字段类型及属性等(改变字段的位置)
mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| ID | tinyint() unsigned | NO | | NULL | |
| Name | varchar() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> alter table t1 change Name StudentName char() not null;
Query OK, row affected (0.03 sec)
Records: Duplicates: Warnings: mysql> desc t1;
+-------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+-------+
| ID | tinyint() unsigned | NO | | NULL | |
| StudentName | char() | NO | PRI | NULL | |
| Age | tinyint() unsigned | NO | PRI | NULL | |
+-------------+---------------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql>
修改字段名称
mysql> show indexes from t1; #查看当前索引信息
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | | PRIMARY | | StudentName | A | | NULL | NULL | | BTREE | |
| t1 | | PRIMARY | | Age | A | | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
rows in set (0.00 sec) mysql> alter table t1 add index(StudentName); #创建一个索引
Query OK, row affected (0.02 sec)
Records: Duplicates: Warnings: mysql> show indexes from t1;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | | PRIMARY | | StudentName | A | | NULL | NULL | | BTREE | |
| t1 | | PRIMARY | | Age | A | | NULL | NULL | | BTREE | |
| t1 | | StudentName | | StudentName | A | | NULL | NULL | | BTREE | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
rows in set (0.00 sec) mysql> alter table t1 drop INDEX StudentName; #删除索引信息
Query OK, row affected (0.04 sec)
Records: Duplicates: Warnings: mysql> show indexes from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | | PRIMARY | | StudentName | A | | NULL | NULL | | BTREE | |
| t1 | | PRIMARY | | Age | A | | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
rows in set (0.00 sec) mysql>
创建索引和删除索引用法展示
mysql> select database();
+-------------+
| database() |
+-------------+
| yinzhengjie |
+-------------+
row in set (0.01 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| t1 |
+-----------------------+
row in set (0.00 sec) mysql> alter table t1 rename to mysql_test_table; #可以用alter命令进行修改标明
Query OK, rows affected (0.00 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| mysql_test_table |
+-----------------------+
row in set (0.00 sec) mysql>
mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| mysql_test_table |
+-----------------------+
row in set (0.00 sec) mysql> rename table mysql_test_table to t1; #当然我们也可以直接用rename命令进行修改哟~
Query OK, rows affected (0.01 sec) mysql> show tables;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| t1 |
+-----------------------+
row in set (0.00 sec) mysql>
修改表名的两种常见的姿势
mysql> show table status like 't1'\G
*************************** . row ***************************
Name: t1
Engine: InnoDB
Version:
Row_format: Compact
Rows:
Avg_row_length:
Data_length:
Max_data_length:
Index_length:
Data_free:
Auto_increment: NULL
Create_time: -- ::
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
row in set (0.00 sec) mysql> alter table t1 engine=MyISAM;
Query OK, row affected (0.02 sec)
Records: Duplicates: Warnings: mysql> show table status like 't1'\G
*************************** . row ***************************
Name: t1
Engine: MyISAM
Version:
Row_format: Fixed
Rows:
Avg_row_length:
Data_length:
Max_data_length:
Index_length:
Data_free:
Auto_increment: NULL
Create_time: -- ::
Update_time: -- ::
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
row in set (0.00 sec) mysql>
修改表选项案例展示