mysql数据库之管理表和索引

show  engines;   --->可以显示当前数据库 所支持的所有存储引擎

   名称             是否支持   简要描述                                                     是否支持事务   是否支持分布式事务   是否支持保存点

+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

| Engine             | Support | Comment                                                        | Transactions | XA                 | Savepoints |

+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys                  | YES      | YES                 | YES         |

                       (默认

show table status like '表名' \G --->查看一个表的属性信息

创建表:

1、直接定义一张空表

   create   table   [if not extsts]  表名 (表属性) ,(表选项:字段名称,字段属性,约束/索引)

  设置主键方法:

     create table sun1 (id int not  null auto_increment primary key ,name char(20)not null ,age tinyint  not null );

     create table sun2 (id int not  null auto_increment ,name char(20)not null ,age tinyint  not null,primary key(id,name));  :id和name联合起来成为主键 

  设置唯一键(名称字段中不允许出现重复)

     create table sun2 (id int not  null auto_increment ,name char(20)not null ,age tinyint  not null,primary key(id,name)  ,unique key (name));  :name这个字段中的数据不允许出现重复

     create table sun2 (id int not  null auto_increment ,name char(20)not null ,age tinyint  not null,primary key(id,name)  unique key (name)index(age)); :设置age 为索引

 设置表的存储引擎

     create  table  表名  (一系列属性 )engine = 存储引擎名

 设置最多存储多少行

     create  table  表名  (一系列属性 ) max-rows=多少行

当使用AUTOINCREMENT列创建序列时,INSERT语句中不使用字段名将导致MySQL自动产生序列的下一个序号。这个序号作为表的主键。

     insert into   kk     (ke)  values   ('huama'),('pixie'),('kuihua');

        关键字   表    字段  关键字    添加内容

 

键也称作约束,可用于索引,属于特殊索引(有特殊限定);B+tree

show  indexes   from  表名 ;    --->查看指定表的索引

那个表上  是不是               第几个    索引在哪       排序                                      索引

的索引    非唯一键   键名称     索引     个字段上       规则                                      类型

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| kk    |        0 | PRIMARY  |   1        |   name     |    A   |       9  |   NULL | NULL  |    | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2、从其他表中查询出数据并以之创建新表

仿照一张表创建一张新表(有数据)

create  table  xinwen  select  *  from  kk     where  name >= 2;

    创建表     表名      查看 旧表   表名    关键字  条件

3、以其他表为模板,创建一个新表

仿照一张表创建一张新表(空表)

create  table  新建表名  like  仿照表明;

4、直接修改表引擎

alter   table   表名   engine=存储引擎名(MyIASM|lnnoDB

修改表:

  alter  table 

     添加、修改、删除字段    修改表属性

     添加、删除、修改索引    改表名

给表中一个字段添加一个唯一键(前提是表中没有重复的数据)

alter  table  表名  add  unique  key   (字段名);

给库中的表改名字

alter  table  旧名字  rename  to  新名字;

或者:rename  table  旧名字 to  新名字;

 

多表查询(将两张表联合起来查询相同的结果):

 

select  name   ,    keming   from  xuehao,kehao  where  xuehao.cid=kehao.cid;

查询    表A字段 表B字段         表名   表名  条件    两张表的cid相同的数据

 

注意,外键约束只能够用在支持事务的存储引擎上

创建与删除索引

创建索引: create   index  索引名称    on  表名  (字段) ;

删除索引:drop  index  索引名   on  表名;

 

insert  into 

insert  into  表名  (字段,字段value  ('字符串',数字)('字符串',数字)

insert  into  表名  set  字段=数值(字符串加引号),字段=数值,字段=数值;

select  *  from  kebiao  order  by    id     desc    limit  1;

                 表名   排序       字段   倒叙   只显示1个

当我们在表中删除一个数据(id 10),当我们再创建这个数据时,id不会再是10,用一条命令查看自动生成的id,(select  last_insert_id();)mysql会接着这里面的数字继续排下去,除非把这里面的数值清零。

 

在表中筛选出的数据插入到另一张表中(两种表的字段必须相同)

insert  into  kebiao  (name) select  name  from  xuesheng   where   id>2;

   插入到     表名   字段      在  字段        表名        筛选   条件

在表中筛选出的数据插入到另一张表中,若这张表中数据重复则替换

replace  into  kebiao  (name) select  name  from  xuesheng   where   id>2;

 

delete

mysql内置选项:当你使用delete语句或者update语句时,没有使用where条件(字句),它不会让你执行!

注意:当你清空整张表时,在插入数据,数据的id号也会接着最后一个被清空数据的id号继续往下排

      所以,想要从零开始,必须清空(select  last_insert_id();)中的数值

truncate  表名; --->清空表,并且重置计数器(select  last_insert_id();)类似于id。。。

posted @ 2019-08-02 17:35  え稚始گ  阅读(208)  评论(0编辑  收藏  举报