mysql 内部语句和语法

来源:互联网 发布:魔乐科技java课堂笔记 编辑:程序博客网 时间:2024/06/05 10:41

alter 语句

 

// 修改数据库编码 utf8

alter database DBNAME character set utf8 collate utf8_general_ci;

 

// 增加索引

alter table DBNAME add index `new_index`(id)

可以代替index的有

    primary key

    unique

    fulltext    全文搜索只有在 MyISAM 引擎中可用

 

// 另一种增加索引方法

create index  索引文件名 on 表名( 字段名)

 

// 删除索引

alter table DBNAME drop index 索引文件名

 

// 另一种删除索引方法

drop index 索引文件名 on 表名 

 

// 删除字段

alter table DBNAME drop column 字段名

 

 

// 停止更新 MyISAM 表中的非唯一索引。

alter table DBNAME disable keys;

插入大批量的数据之后

// 重新创建丢失索引

alter table DBNAME enable keys;

 

上面的操作能让大批量的数据在写入到数据表时加快执行速度。

 

 

 

// 复制表

create table  A select * from B;

将B表的所有数据和字段全部复制到 A表。但是不会复制任何的属性 和索引之类的东西。

 

// 如果想创建表时建立索引 则可以如下使用

create table A (primary key(id)) select * from B;

 

将B表的数据复制到 A 表时 设置 id列为主键

 

完全复制 B 表的属性和索引 ( 数据不会被复制)

create table A like B;

 

 

 删除多个表中的行

 

a 表

id        name

1         test

 

b表

id           aid

1            1

 

c表

id          bid

1           1

 

delete a,b,c from a,b,c where a.id = b.aid and b.id = c.bid

 

如果上述删除看作是查询时只有在链表成功 才会删除 a b c 三个表中的三行记录

 

具体条件成立时删除那个表的数据 是看 delete 后面紧跟着的表名

 

 

导入txt 文本中的数据

load data infile 文本文件路径 into table 表名 fields

terminated "," escaped by "\\" lines terminated by '\n'

 

terminated ","   指的是 用来 , 分割列

escaped by "\\"  不知道啥意思 不写也行 (好像是对转移的处理)

terminated by '\n' 指的是用什么分割一行 (默认好像就是 \n)

 

 

 

原创粉丝点击