1.1 判断某个数据库是否存在
SELECT * FROM information_schema.SCHEMATA
WHERE SCHEMA_NAME = 'dbname';
1.2 判断某个数据库是否存在 + 不存在则新建数据库
CREATE DATABASE IF NOT EXISTS `dbname`;
注意, dbname两侧是反引号(可以没有),但一定不能加引号;
2.1 判断某个数据库中某张数据表是否存在
SELECT * FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tbname';
2.2 判断某个数据库中某张数据表是否存在 + 不存在则新建数据表
CREATE TABLE IF NOT EXISTS `account` (
`customer_id` int(11) NOT NULL,
`account_number` int(11) NOT NULL,
`type` varchar(255) DEFAULT NULL,
`balance` double DEFAULT NULL,
`active` smallint(6) DEFAULT NULL,
PRIMARY KEY (`customer_id`,`account_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;