创建、删除表
- CREATE TABLE [IF NOT EXISTS] table_name …
- 表创建后,它们的定义保存在: information_schema.tables
- 丢弃⼀张表: DROP TABLE [IF EXISTS] table_name;
- 删除表内所有数据: TRUNCATE TABLE table_name;
删除普通表和临时表的⽤法如下:
- DROP TEMPORARY TABLE 只能删除本地临时表
- DROP GLOBAL TEMPORARY TABLE 只能删除全局临时表
- DROP TABLE 可以删除普通表或临时表
查看创建表的 DDL
SHOW CREATE TABLE
tidb> SHOW CREATE TABLE universe.stars\G
*************************** 1. row ***************************Table: stars
Create Table: CREATE TABLE `stars` (`id` bigint(20) NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,`name` char(20) NOT NULL DEFAULT '',`mass` float NOT NULL DEFAULT '0.0' COMMENT '10**24 kg',`density` int(11) NOT NULL DEFAULT '0' COMMENT 'kg/m**3',`gravity` decimal(20,4) NOT NULL DEFAULT '0.0' COMMENT 'm/s**2',`escape_velocity` decimal(8,1) DEFAULT NULL COMMENT 'km/s',`mass_conversion_rate` int(11) DEFAULT NULL COMMENT '10**6
kg/s',`spectral_type` char(8) NOT NULL DEFAULT '',`distance_from_earth` float DEFAULT NULL COMMENT 'light year',`discover_date` datetime DEFAULT NULL,PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T!
[auto_rand_base] AUTO_RANDOM_BASE=134447 */
1 row in set (0.00 sec)
复制现有表结构
CREATE TABLE 与 LIKE 关键字⼀起使⽤可创建与另⼀个表具有相同结构的表:
- ⼆级索引
- 列选项
- 没有数据
CREATE TABLE LIKE 语句⽤于复制已有表的定义,但不复制任何数据。
create table test.dropme2 like test.dropme;
Query OK, 0 rows affected (0.52 sec)
tidb> desc test.dropme;
+---------+---------------+------+------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+------+---------+-------+
| id | bigint(20) | NO | PRI | NULL | |
| uuid | bigint(20) | NO | | NULL | |
| isvalid | enum('T','F') | NO | | F | |
+---------+---------------+------+------+---------+-------+
3 rows in set (0.00 sec)
tidb> desc test.dropme2;
+---------+---------------+------+------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+------+---------+-------+
| id | bigint(20) | NO | PRI | NULL | |
| uuid | bigint(20) | NO | | NULL | |
| isvalid | enum('T','F') | NO | | F | |
+---------+---------------+------+------+---------+-------+
3 rows in set (0.00 sec)