mysql笔记(9)-表的创建和删除(drop/truncate/delete)

本文将简单介绍在数据库中创建和删除表的相关操作,包括关键字create、drop、truncate、delete等

一、表的创建

在数据库中创建一张新表的写法如下:

create table table_name {
column1_name  data_type  restrictions,
column2_name  data_type  restrictions,
column3_name  data_type  restrictions,
other_restrictions);

关于各种数据类型的详细说明 可以参考以下链接:
MySQL数据类型|菜鸟教程
有关约束(restrictions)的知识将会在后面的笔记中进行介绍

二、表的删除

删除一张(或多张)表有三种方式:

1、drop
drop table table_name;

这种方式将会把表结构连同记录一起删除,且无法找回

2、truncate
truncate table table_name;

这种写法将会保留表结构,但会删除表中所有记录且无法找回

3、delete
delete from table_name
where xxx;

这种方式是最保守的删除方式
在省略where从句时,将会删除表中所有记录,并保留表结构
在添加where从句时,可以删除表中指定的行

注意:以上三种方式中,只有delete操作可以被回滚(rollback)

猜你喜欢

转载自www.cnblogs.com/baebae996/p/12912669.html