mysql create table 语法详解
2024-08-27 01:56:48
create table 可以分成三类
一、一般create table 语句:
1 语法
create [temporary] table [if not exists] tbl_name
(create_definition)
[table_options]
[parttion_options]
2 例子:创建一个person表它包涵id,name,birthday这几个列
create table person(id int not null auto_increment,
name varchar(8),
birthday datetime,
constraint pk__person primary key(id));
二、create table like 参照已有表的定义,来定义新的表:
1 语法
create [temporary] table [if not exists] tbl_name
{like old_tbl_name | (like old_tbl_name)};
2 例子:定义一个person_like 表,它的表结构参照上面例子中的person表
mysql> create table person_like like person;
Query OK, 0 rows affected (0.01 sec) mysql> show create table person_like \G
*************************** 1. row ***************************
Table: person_like
Create Table: CREATE TABLE `person_like` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(8) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
可以看出使用create table like 方式创建的表,它的表结构和原表是一样的。
三、根据select 的结果集来创建表:
1 语法
create [temporary] table [if not exists] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
[ignore | replace]
[as] query_expression
2 例子:
mysql> create table person_as
-> as
-> select id,name from person;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table person_as \G
*************************** 1. row ***************************
Table: person_as
Create Table: CREATE TABLE `person_as` (
`id` int(11) NOT NULL DEFAULT '',
`name` varchar(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
----
最新文章
- POJ2774 Long Long Message [后缀数组]
- Extjs.Button 按钮
- Excel 自动更正
- 树状sql--采用递归方式获取节点
- anr产生的原理&;如何避免(android)
- 何时要打开stm32的AFIO时钟
- (转载)js----对象直接量
- 在Xcode中使用Clang Format
- AC自动机模板2(【CJOJ1435】)
- NodeJs实现自定义分享功能,获取微信授权+用户信息
- Linux 虚存 linux2.6内核特性
- Android简易实战教程--第一话《最简单的计算器》
- 巡风源码阅读与分析--querylogic函数
- 如何设置C-Lodop打印控件的端口
- Node.js简述
- 操作系统笔记(六)页面置换算法 FIFO法 LRU最近最久未使用法 CLOCK法 二次机会法
- SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
- 微信小程序 发现之旅(三)—— 组件之间的参数传递
- c++字节数组转换为整型
- centos 7 网站前端中文乱码分析、解决办法