表操作
储引擎:即表的类型,不同的引擎处理机制不同
查看mysql支持的存储引擎
show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
创建表指定表类型(存储引擎)
mysql> create table t1(id int) engine=innodb;#对应两张表t1.frm t1.ibd mysql> create table t2(id int) engine=memory;#数据存放与内存,只有结构表,无数据表t2.frm mysql> create table t3(id int) engine=blackhome;#数据黑洞,只有结构表,无数据表t3.frm
mysql> create table t4(id int) engine=myisam;#对于3张表t4.frm t4.MYD t4.MYI
创建表
create table t5( -> id int, -> name varchar(50), -> sex enum('male','female'), -> age int(13) -> );
mysql> desc t5;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(13) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
查看详细信息
show create table t5\G
修改表结构
修改表名:
alter table 表明 rename 新表明;
增加字段:
alter table 表名 add 字段名 数据类型 [完整性约束]
alter table 表名 add 字段名 数据类型 [完整性约束] first
alter table 表名 add 字段名 数据类型 [完整性约束] after 字段名
删除字段:
alter table 表名drop 字段名
修改字段:
alter table 表名 modify 字段名
alter table 表名 change 原字段名 新字段名 数据类型
修改字段备注:
alter table 表名 modify column 字段名 text comment '备注信息';
修改表备注信息:
alter table 表名 comment '备注信息';
复制表
复制表结构+数据
#创建新的数据库 mysql> create database db3; #切换数据库 mysql> use db3; #复制mysql.user表中的host,user字段到新表t1中 mysql> create table t1 select host,user from mysql.user; Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| host | char(60) | NO | | | |
| user | char(32) | NO | | | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
复制表结构
#添加一个不存在的条件即可拷贝表结构 create table t2 select host,user from mysql.user where host="123";
拷贝整张表结构
create table t3 like mysql.user;
删除表
drop table t1;
清空表
delete from 表名; 用于和where连用删除指定数据行
主:delete无法清空auto_increment记录,下次增加不会从1开始自增。
truncate 表明;清空表所有信息。