SQL 类型 | Go 类型 |
---|---|
INT, INTEGER, SMALLINT, TINYINT | int, int32, int64(具体取决于数据库中整数的范围和你的应用需求) |
BIGINT | int64 |
FLOAT, REAL | float32 |
DOUBLE, DOUBLE PRECISION | float64 |
DECIMAL(p,s), NUMERIC(p,s) | database/sql包中的sql.NullFloat64(可空浮点型)、sql.NullInt64(可处理整数型的小数部分)等,或者使用自定义结构体结合sql.Scanner和driver.Valuer接口处理 |
CHAR(n), VARCHAR(n), TEXT | string、sql.NullString(可空字符串) |
DATE | time.Time |
DATETIME, TIMESTAMP | time.Time |
BOOLEAN | bool、sql.NullBool(可空布尔型) |
CREATE TABLE `foo` ( `id` BIGINT unsigned NOT NULL AUTO_INCREMENT COMMENT 'pk', `code` VARCHAR(191) NOT NULL COMMENT 'code', `is_deleted` TINYINT unsigned NOT NULL DEFAULT 0 COMMENT '0 valid/1 deleted', `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create_time', `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update_time', INDEX `idx_create_time` (`create_time`), UNIQUE `uniq_code` (`code`), PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='comments';