mysql中,创建表的时候指定if not exists参数的作用?

需求说明:

  在创建表的时候,如果指定if not exists语句,有什么作用,在此做个实验,并且官方手册,

  理解下这个参数的作用.

操作过程:

1.创建测试表test01

mysql> create table test01 (id int);
Query OK, 0 rows affected (0.08 sec)

2.不指定if not exists语句,创建test01表

mysql> create table test01 (id int);
ERROR 1050 (42S01): Table 'test01' already exists

备注:如果不指定if not exists语句,创建同名表的时候就会报错.

3.指定if not exists语句创建表

mysql> create table if not exists test01 (id01 int);  #虽然字段不同,但是仍然不能创建.
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+-------------------------------+
| Level | Code | Message                       |
+-------+------+-------------------------------+
| Note  | 1050 | Table 'test01' already exists |
+-------+------+-------------------------------+
1 row in set (0.00 sec)

备注:指定了if not exists语句来创建表,虽然表名是存在的,但是创建没有报错,但是存在警告信息,警告中信息是表已经存在了.

另:两次创建的表,虽然字段不同不同,表名相同,还是不允许创建.

小结:

  1.如果指定了if not exists语句来创建表,如果表存在,也不会报错

  2.创建表的语句不会验证要创建的表与已经存在的表的结构是否一致,只要名字相同就不允许创建.

文档创建时间:2018年6月11日21:26:54

猜你喜欢

转载自www.cnblogs.com/chuanzhang053/p/9169323.html