当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL创建和删除表操作命令实例讲解

MySQL创建和删除表操作命令实例讲解

2017年12月12日  | 移动技术网IT编程  | 我要评论

创建表

简单的方式

复制代码 代码如下:

create table person (
number int(11),
name varchar(255),
birthday date
);

或者是

复制代码 代码如下:

create table if not exists person (
number int(11),
name varchar(255),
birthday date
);

查看mysql创建表:

复制代码 代码如下:

show create table person;

create table `person` (
  `number` int(11) default null,
  `name` varchar(255) default null,
  `birthday` date default null
) engine=myisam default charset=utf8;

查看表所有的列:

复制代码 代码如下:

show full columns from person;
+----------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| field    | type         | collation       | null | key | default | extra | privileges                      | comment |
+----------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| number   | int(11)      | null            | yes  |     | null    |       | select,insert,update,references |         |
| name     | varchar(255) | utf8_general_ci | yes  |     | null    |       | select,insert,update,references |         |
| birthday | date         | null            | yes  |     | null    |       | select,insert,update,references |         |
+----------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+

创建临时表:

复制代码 代码如下:

create temporary table temp_person (
number int(11),
name varchar(255),
birthday date
);

在创建表格时,您可以使用temporary关键词。只有在当前连接情况下,temporary表才是可见的。当连接关闭时,temporary表被自动取消。这意味着两个不同的连接可以使用相同的临时表名称,同时两个临时表不会互相冲突,也不与原有的同名的非临时表冲突。(原有的表被隐藏,直到临时表被取消时为止。)您必须拥有create temporary tables权限,才能创建临时表。

如果表已存在,则使用关键词if not exists可以防止发生错误。

复制代码 代码如下:

create table if not exists person2 (
number int(11),
name varchar(255),
birthday date
);

注意,原有表的结构与create table语句中表示的表的结构是否相同,这一点没有验证。注释:如果您在create table...select语句中使用if not exists,则不论表是否已存在,由select部分选择的记录都会被插入。

在create table语句的末尾添加一个select语句,在一个表的基础上创建表

复制代码 代码如下:

create table new_tbl select * from orig_tbl;

注意,用select语句创建的列附在表的右侧,而不是覆盖在表上。

复制代码 代码如下:

mysql> select * from foo;
+---+
| n |
+---+
| 1 |
+---+
mysql> create table bar (m int) select n from foo;
mysql> select * from bar;
+------+---+
| m    | n |
+------+---+
| null | 1 |
+------+---+

也可以明确地为一个已生成的列指定类型

复制代码 代码如下:

create table foo (a tinyint not null) select b+1 as a from bar;

根据其它表的定义(包括在原表中定义的所有的列属性和索引),使用like创建一个空表:

复制代码 代码如下:

create table new_tbl like orig_tbl;

创建一个有主键,唯一索引,普通索引的表:

复制代码 代码如下:

create table `people` (
  `peopleid` smallint(6) not null auto_increment,
  `firstname` char(50) not null,
  `lastname` char(50) not null,
  `age` smallint(6) not null,
  `townid` smallint(6) not null,
  primary key (`peopleid`),
  unique key `unique_fname_lname`(`firstname`,`lastname`),
  key `fname_lname_age` (`firstname`,`lastname`,`age`)
) ;

其中peopleid是主键,以firstname和lastname两列建立了一个唯一索引,以firstname,lastname,age三列建立了一个普通索引

删除表

复制代码 代码如下:

drop table  tbl_name;

或者是

复制代码 代码如下:

drop table if exists tbl_name;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网