/* 创建数据库 */
/* utf8 */
CREATE DATABASE `数据库名` DEFAULT CHARACTER SET utf8 COLLATE utf8mb4_unicode_ci;
/* utf8mb4 */
CREATE DATABASE `数据库名` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

/* 创建数据表 */
/* utf8 */
CREATE TABLE `数据库名`.`数据表名`(id int(10) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8mb4_unicode_ci;
/* utf8mb4 */
CREATE TABLE `数据库名`.`数据表名`(id int(10) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

/* 新增不同类型字段 */
ALTER TABLE `数据表名`
ADD COLUMN `字段1` int(10) DEFAULT '0',
ADD COLUMN `字段2` varchar(255) DEFAULT '',
ADD COLUMN `字段3` longtext DEFAULT NULL;

/* 在某字段后增加字段 */
ALTER TABLE `数据表名`
ADD COLUMN `字段4` int(10) DEFAULT '0' AFTER `字段3`,
ADD COLUMN `字段5` varchar(255) DEFAULT '' AFTER `字段4`,
ADD COLUMN `字段6` longtext DEFAULT NULL AFTER `字段5`;

/* 清空数据表,初始自增id */
TRUNCATE TABLE `表名`;

/* 按条件删除记录 */
DELETE FROM `qqapi`.`qq_s` WHERE `qq_s`.`id` = {{id}};

/* 解决删除记录后id不连续 */
SET @i=0;
UPDATE `表名` SET `id`=(@i:=@i+1);
ALTER TABLE `表名` AUTO_INCREMENT=0;

/* 随机取1条记录 */
SELECT * FROM `data_man` AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `data_man`)-(SELECT MIN(id) FROM `data_man`))+(SELECT MIN(id) FROM `data_man`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;
Last modification:November 15, 2022
如果觉得我的文章对你有用,请随意赞赏