一、执行引擎 engine(表的类型)
1、显示执行引擎
show engines\G \G表示按照行显示,以\G结尾,就不需要结尾的分号了。(命令以\G或者分号结尾);
如果敲命令行的时候,误敲回车,可以使用\c取消。
显示结果:
mysql> show engines\G
*************************** 1. row ***************************
Engine: MEMORY ---存储在内存中,服务重启后就会丢失。
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
*************************** 5. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 8. row ***************************
Engine: BLACKHOLE ----黑洞引擎,不存数据,直接丢弃
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
9 rows in set (0.00 sec)ERROR:
No query specified
示例,创建一个表,指定执行引擎为innodb(innodb为默认执行引擎,不指定时实际使用的就是innodb):
mysql> create table t1(id int, name char(10))engine=innodb;
Query OK, 0 rows affected (1.34 sec)mysql> insert into t1 values(1, 'keheyang');
Query OK, 1 row affected (0.40 sec)mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.00 sec)mysql> select * from t1;
+------+----------+
| id | name |
+------+----------+
| 1 | keheyang |
+------+----------+
1 row in set (0.00 sec)mysql>
二、 表定义
1、创建表
create 表名(字段1 字段类型[宽度] [约束条件],字段2 字段类型[宽度] [约束条件],字段2 字段类型[宽度] [约束条件]);
/*宽度不写,默认为1, 最后一个字段后面不需要逗号*/
2、查看表结构
desc [dbname.]表名; /*查看表结构,在当前数据库下查看,可以省略dbname*/
show create table 表名 \G; /*查看表定义详细信息,\G表示按行显示,可以不用\G*/
show tables; 查看当前数据库有哪些表。
show tables from 库名; 查看指定数据库下有哪些表
mysql> desc db1.t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> show create table t1;
+-------+--------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int DEFAULT NULL,
`name` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------+
1 row in set (0.28 sec)mysql>
3、修改表结构
修改表名:
alter table 表名 rename 新表名;
增加字段:
alter table 表名 add 字段名 数据类型 约束条件, 字段名 数据类型 约束条件;
alter table 表名 add 字段名 数据类型 约束条件 first;
alter table 表名 add 字段名 数据类型 约束条件 after 字段名;
删除字段:
alter table 表名 drop 字段名;
修改字段:
alter table 表名 modify 字段名 数据类型 约束条件;
alter table 表名 change 旧字段名 新字段名 数据类型 约束条件;
4、删除表
drop table 表名;
5、复制表
create table 表名 select 字段1,字段2(或者*) from 表名2; /*复制表结构和记录*/
create table 表名 like 表名2; /*仅复制表结构*/
create table 表名 select 字段1,字段2 from 表名2 where 1=2;/*通过设置条件为假(1等于2为假),事实只拷贝了表结构*/