mysql判断是否存在数据库和表,进行删除和创建

1.存在莫数据库,则删除创建一个新库

drop database if exists `tpm_business`;

CREATE DATABASE tpm_business DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2.不存在某数据库,则创建

CREATE DATABASE if not exists tpm_business DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

3.存在某表,先删除再创建

DROP TABLE IF EXISTS `sys_auth`;
CREATE TABLE if not exists `sys_auth` (
  `authorizer_id` bigint(20) NOT NULL COMMENT '授权对象id',
  `role_id` bigint(20) NOT NULL COMMENT '角色id'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='授权表';

4.不存在某表,则创建

CREATE TABLE if not exists `sys_auth` (
  `authorizer_id` bigint(20) NOT NULL COMMENT '授权对象id',
  `role_id` bigint(20) NOT NULL COMMENT '角色id'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='授权表';

猜你喜欢

转载自www.cnblogs.com/zsg88/p/11737897.html