关于Mysql里面创建表的问题·

来源:百度知道 编辑:UC知道 时间:2024/06/07 22:02:42
以前创建表的时候都是create table name;为什么看了人家的
人家写成这样呢?有什么目的?
drop table if exists t_user;
create table if not exists t_user
(
user_id varchar(10) primary key not null,
user_name varchar(20) not null,
password varchar(20),
contact_tel varchar(30),
email varchar(30),
create_date datetime
);

drop table if exists t_user;---如果存在t_user表,则把它删除掉
create table if not exists t_user
(
user_id varchar(10) primary key not null,
user_name varchar(20) not null,
password varchar(20),
contact_tel varchar(30),
email varchar(30),
create_date datetime
); ---如果不存在t_user表,则建立它

这样的好处是;如果这张表存在的话,你直接create table table_name的时候,就会报错,因为已经存在了,所以起相同名字你自然建不起来

但是这样的不好的地方是,如果原来这张表有用,那么里边的数据就没了
不过一般象你写的那样,都把原来数据作为没用的数据删除掉,所以这样就避免了如果存在相同表名报错

exists 存在与不存在判断 新建的table 可以不用这个判断 直接写create table t_user

就是加个判断
drop table if exists t_user; 有t_user就删除
create table if not exists t_user 没t_user就创建