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)
----