我的存储过程有超过 5 个创建表语句。有没有办法选择由这些创建表语句在全局变量中设置的默认排序规则,而无需通过单独的创建表语句。
-- SAMPLE BLOCK A --
DROP TABLE IF EXISTS abc;
SET @createTable = CONCAT("CREATE TABLE "abc"(
record_id VARCHAR(255),
member VARCHAR(255),
name VARCHAR(255)
)CHARACTER SET 'utf8'
COLLATE 'utf8_unicode_ci'");
PREPARE create_table_statement FROM @createTable;
EXECUTE create_table_statement;
DEALLOCATE PREPARE create_table_statement;
-- SAMPLE BLOCK B--
DROP TABLE IF EXISTS def;
SET @createTable1 = CONCAT("CREATE TABLE "def"(
address VARCHAR(255),
member_gender VARCHAR(255),
member_age VARCHAR(255)
)CHARACTER SET 'utf8'
COLLATE 'utf8_unicode_ci'");
PREPARE create_table_statement1 FROM @createTable1;
EXECUTE create_table_statement1;
DEALLOCATE PREPARE create_table_statement1;
.
.
有两个选项:
1:使用可以更改的局部变量来影响所有创建表
-- SAMPLE BLOCK A --
set @character_set = 'utf8';
set @default_collation = 'utf8_unicode_ci';
DROP TABLE IF EXISTS abc;
-- use that variables to build the SQL:
SET @createTable = CONCAT("CREATE TABLE "abc"(
record_id VARCHAR(255),
member VARCHAR(255),
name VARCHAR(255)
)CHARACTER SET '",@character_set,"'
COLLATE '",@default_collation,"'");
-- same for next
2:设置数据库默认字符集和排序规则,当您设置默认值时:所有没有特定字符集或排序规则的新表都将使用该字符集或排序规则作为默认值。
CREATE DATABASE `your_database`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
CREATE DATABASE mydb
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
您可以在创建表时使用上述语句。