MySQL 6年前

MySQL之创建表

作者头像 刘宇帅
2983 0

创建表

创建学生表 SQL

create table if not exists`students`  (
    `id` int not null auto_increment comment '学生id',
    `no` char(5) not null comment '学生学号',
    `name` varchar(128) not null default '' comment '学生姓名',
    `sex` tinyint not null default 0 comment '0 无 1 女 2 男',
    `age` tinyint null default null comment '年龄',
    `created_at` datetime not null default current_timestamp comment '创建时间',
    `updated_at` datetime default null on update current_timestamp comment '更新时间',
    primary key(`id`),
    key `created_at`(`created_at`),
    unique key `student_no`(`no`)
) engine=InnoDB default charset=utf8mb4 collate=utf8mb4_unicode_ci comment "学生信息表";

执行创建语句

mysql> use school;
Database changed
mysql> create table if not exists`students`  (
    -> `id` int not null auto_increment comment '学生id',
    -> `no` char(5) not null comment '学生学号',
    -> `name` varchar(128) not null default '' comment '学生姓名',
    -> `sex` tinyint not null default 0 comment '0 无 1 女 2 男',
    -> `age` tinyint null default null comment '年龄',
    -> `created_at` datetime not null default current_timestamp comment '创建时间',
    -> `updated_at` datetime default null on update current_timestamp comment '更新时间',
    -> primary key(`id`),
    -> key `created_at`(`created_at`),
    -> unique key `student_no`(`no`)
    -> ) engine=InnoDB default charset=utf8mb4 collate=utf8mb4_unicode_ci comment "学生信息表";
Query OK, 0 rows affected (0.08 sec)

创建表中的字段 id created_at updated_at 和索引 primary key(id) 是创建任何表都必须的。表默认统一使用 InnoDB 引擎 和 utf8mb4 字符集、utf8mb4_unicode_ci 字符序。
查看所有表

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)

查看表字段基本结构

mysql> desc students;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field      | Type         | Null | Key | Default           | Extra                       |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id         | int(11)      | NO   | PRI | NULL              | auto_increment              |
| no         | char(5)      | NO   | UNI | NULL              |                             |
| name       | varchar(128) | NO   |     |                   |                             |
| sex        | tinyint(4)   | NO   |     | 0                 |                             |
| age        | tinyint(4)   | YES  |     | NULL              |                             |
| created_at | datetime     | NO   | MUL | CURRENT_TIMESTAMP |                             |
| updated_at | datetime     | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
+------------+--------------+------+-----+-------------------+-----------------------------+
7 rows in set (0.01 sec)

查看创建表语句

mysql> show create table students\G;
*************************** 1. row ***************************
       Table: students
Create Table: CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生id',
  `no` char(5) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '学生学号',
  `name` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '学生姓名',
  `sex` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0 无 1 女 2 男',
  `age` tinyint(4) DEFAULT NULL COMMENT '年龄',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `student_no` (`no`),
  KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='学生信息表'
1 row in set (0.00 sec)

ERROR:
No query specified

\G 用来格式化输出结果,更好看一点。

临时表

临时表只在当前会话中生效,退出重连就会消失,我们有时候需要一个临时表存放一些数据。比如保存下 id 小于 100 的学生到一个临时表

mysql> create temporary table students_temporary select * from students where id<100;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from students_temporary limit 1\G;
*************************** 1. row ***************************
        id: 1
        no: 00001
      name: 阿宝
       sex: 0
       age: NULL
created_at: 2019-11-20 01:34:04
updated_at: NULL
1 row in set (0.00 sec)

ERROR:
No query specified

查看库所有所有表时不展示临时表

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)

创建一个相同的表

只创建表不要数据

mysql> create table students_new like students;
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| students         |
| students_new     |
+------------------+
2 rows in set (0.00 sec)

mysql> desc students_new;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field      | Type         | Null | Key | Default           | Extra                       |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id         | int(11)      | NO   | PRI | NULL              | auto_increment              |
| no         | char(5)      | NO   | UNI | NULL              |                             |
| name       | varchar(128) | NO   |     |                   |                             |
| sex        | tinyint(4)   | NO   |     | 0                 |                             |
| age        | tinyint(4)   | YES  |     | NULL              |                             |
| created_at | datetime     | NO   | MUL | CURRENT_TIMESTAMP |                             |
| updated_at | datetime     | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
+------------+--------------+------+-----+-------------------+-----------------------------+
7 rows in set (0.01 sec)

创建表并复制全部数据

mysql> create table students_new2 select * from students;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from students_new2 limit 1\G;
*************************** 1. row ***************************
        id: 1
        no: 00001
      name: 阿宝
       sex: 0
       age: NULL
created_at: 2019-11-20 01:34:04
updated_at: NULL
1 row in set (0.00 sec)

ERROR:
No query specified

使用 create talbe ... select 不会创建任何索引

mysql> show create table students_new2\G;
*************************** 1. row ***************************
       Table: students_new2
Create Table: CREATE TABLE `students_new2` (
  `id` int(11) NOT NULL DEFAULT '0' COMMENT '学生id',
  `no` char(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '学生学号',
  `name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '学生姓名',
  `sex` tinyint(4) NOT NULL DEFAULT '0' COMMENT '0 无 1 女 2 男',
  `age` tinyint(4) DEFAULT NULL COMMENT '年龄',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

ERROR:
No query specified
作者头像

刘宇帅

非著名程序员,全栈开发工程师,长期专注系统开发与架构设计。

提示

功能待开通!


暂无评论~

相关文章

MySQL之创建库

MySQL 创建库语法 CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name 创建一个默认的数据库 create database school; 执行结果 mysql&gt; create database school; Query OK, 1 row affecte

MySQL 技术内幕阅读笔记(0)--- InnoDB 体系结构和存储引擎

MySQL 体系结构 从图我们可以看出,MySQL 有如下几部分组成: 连接池组件 管理服务和工具组件 SQL 接口组件 查询优化组件 优化器组件 缓冲组件 插件式存储引擎 物理文件 需要注意存储引擎是基于表的而不是库,比如我们可以在建表的时候为表指明存储引擎。 MySQL 存储引擎 常见存储引擎 InnoDB MySQL 5.5.8 版本之后的默认存储引擎,事务性,行锁,支持外键。 MyISAM 存储引擎不支持事务、表锁,支持全文索引,主要面对一些 OLAP 数据库应用。MySQL 5.5.6 版本之前的默认存储引擎。 NDB 存储引擎是一个集群存储引擎。 Memory 存储引擎将表

MySQL之删除数据

单表删除语法 DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias] [PARTITION (partition_name [, partition_name] ...)] [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] 多表删除 DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name[.*] [, tbl_name[.*]] ... FROM table_r

查看 MySQL 表的大小

information_schema MySQL 自带一个数据库 information_schema ,这个数据库用于记录 MySQL 数据库的基本信息,比如数据库名,数据库的表,表栏的数据类型与访问权限等。 mysql&gt; use information_schema; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql&gt;

高性能MySQL阅读笔记(0)--- 选择优化的数据类型

MySQL支持的数据类型非常多,选择正确的数据类型对于获得高性能至关重要。不管存储哪种数据类型,下面几个简单的原则都有助于做出更好的选择。 更小的通常更好 一般情况下,尽可能使用可以正确存储数据的最小数据类型。更小的数据类型通常更快,因为他们占用的更少的磁盘、内存和CPU缓存,处理时需要的CPU周期更少。 简单最好 简单的数据类型的操作通常需要更少的CPU周期。例如整数比字符操作代价更低,因为字符集和校对规则使字符串比较比整型更复杂。 两个实际使用中的例子,一个是使用MySQL内置的类型而不是字符串存储时间,另一个是使用整型而不是字符串存储IP。 尽量避免NULL 很多表都包含可为 NULL