目錄sql
DESCRIBE `table_name`;
SHOW CREATE TABLE `table_name`;
SHOW TABLES;
CREATE TABLE `table_name` ( `c_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '惟一ID', `c_is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '邏輯刪除', `c_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新時間', `c_create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', PRIMARY KEY (`c_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='table_name'
CREATE TABLE IF NOT EXISTS `table_name` ( `c_id` bigint(20) unsigned NOT NULL COMMENT '惟一ID', `c_uid` char(11) NOT NULL DEFAULT '' COMMENT 'uid列', `c_char` char(11) NOT NULL DEFAULT '' COMMENT 'char列', `c_varchar1` varchar(30) NOT NULL DEFAULT '' COMMENT 'varchar列', `c_varchar2` varchar(300) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT 'varchar列,單獨設置字符集', `c_text` text CHARACTER SET utf8mb4 NOT NULL COMMENT 'text列,單獨設置字符集', `c_tinyint` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'tinyint,當枚舉用(0A 1B 2C)', `c_enum` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'enum,枚舉用', `c_json` json NOT NULL COMMENT 'json類型列', `c_is_deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '邏輯刪除', `c_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新時間', `c_create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間', PRIMARY KEY (`c_id`), KEY `inx_c_uid` (`c_uid`) USING BTREE, UNIQUE KEY `udx_c1_c2` (`c_char`,`c_tinyint`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='table_name';
ALTER TABLE `table_name` ADD `c_column` tinyint(1) not null DEFAULT 0 COMMENT '新增列';
ALTER TABLE `table_name` MODIFY COLUMN `c_varchar1` VARCHAR ( 300 ) NOT NULL DEFAULT '' COMMENT '標識符' AFTER `c_varchar2`;
ALTER TABLE `table_name` DROP COLUMN `c_column`;
DROP TABLE if EXISTS `table_name`;
RENAME TABLE `table_name` TO `table_name_2`;
SHOW INDEX FROM table_name [FROM db_name]
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [USING index_type] ON table_name (index_col_name, ...)
CREATE UNIQUE INDEX idx_uid ON `table_name` (`c_uid`); -- TEXT和BLOB要指定長度 CREATE UNIQUE INDEX idx_text ON `table_name` (`c_text`(6));
ALTER TABLE
方式ALTER TABLE table_name ADD [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name (index_col_name,...) [USING index_type]
ALTER TABLE `table_name` ADD PRIMARY KEY ( `c_uid` ) ALTER TABLE `table_name` ADD INDEX `udx_c1_c2` ( `c_char`(n), `c_tinyint` ) USING BTREE;
ALTER TABLE `table_name` DROP INDEX `udx_c1_c2`, ADD INDEX `udx_c1_c2` ( `c_char`, `c_tinyint`, `c_is_deleted` ) USING HASH;
ALTER TABLE table_name DROP PRIMARY KEY; DROP INDEX `idx_name` ON `table_name`; ALTER TABLE `table_name` DROP INDEX `udx_c1_c2`;
子句順序:SELETC
、 FROM
、 WHERE
、 GROUP BY
、 HAVING
、 ORDER BY
、 LIMIT
json
SELECT * FROM table_name WHERE ... ORDER BY c_column1,c_column2 DESC;
%
_
SELECT * FROM table_name WHERE c_column LIKE CONCAT('first','second','third'); SELECT * FROM table_name WHERE c_column LIKE CONCAT('%','_','%');
分組查詢函數
WHERE
過濾行, HAVING
過濾分組ui
SELECT GROUP_CONCAT(c_column) FROM table_name WHERE c_is_deleted=0 GROUP BY c_column1, c_column2 HAVING COUNT(c_column3) >= 2; -- 可能報錯:incompatible with sql_mode=only_full_group_by SELECT GROUP_CONCAT(c_column) FROM table_name WHERE c_is_deleted=0 GROUP BY c_column1, c_column2 HAVING COUNT(c_column3) >= 2 ORDER BY c_order DESC; SELECT GROUP_CONCAT(c_id),COUNT(*) FROM table_name GROUP BY c_type WITH ROLLUP;
分頁查詢code
檢索出來的第一行爲行0而不是行1。索引
-- 查詢兩條記錄,開始行1 SELECT * FROM table_name LIMIT 1, 2;
(1)計算字段string
SELECT 1+1 AS result; SELECT CONCAT('first', c_column, 'third') AS c_alias_name FROM table_name; SELECT CONCAT('first', TRIM(c_column), 'third') AS c_alias_name FROM table_name;
(2)處理函數it
SELECT UUID() AS `uid`; SELECT UPPER('a'); SELECT DATE(c_create_time) AS `date`, TIME(c_create_time) AS `time` FROM table_name;
(3)彙集函數table
SELECT COUNT( * ), MIN( c_column ), MAX( c_column ), AVG( c_column ) FROM table_name; SELECT SUM( c_column1 * c_column2 ) FROM table_name;
SELECT * FROM table_name1 WHERE c_column IN ( SELECT c_column FROM table_name2 WHERE c_is_deleted = 1 ); -- 計算字段做爲子查詢 SELECT table_name1.c_name, ( SELECT COUNT( * ) FROM table_name2 WHERE table_name2.c_key = table_name1.c_key ) AS nums FROM table_name1 WHERE c_is_deleted = 0;
(1) 自聯結class
SELECT t1.c_id, t1.c_name FROM table_name AS t1, table_name AS t2 WHERE t1.c_id = t2.c_id AND t2.c_key = '';
(2) 天然聯結
SELECT t1.c_id, t1.c_name FROM table_name1 AS t1, table_name2 AS t2 WHERE t1.c_id = t2.c_id AND t2.c_key = '';
(3) 外部聯結
SELECT table_name1.c_id, table_name1.c_name, table_name2.c_name FROM table_name1 LEFT JOIN table_name2 ON table_name2.c_key = table_name1.c_key WHERE ...;
case when then else end
用法(1) 語法
--簡單Case函數 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其餘' END
--Case搜索函數, CASE WHEN sex = '1' THEN '男' WHEN sex = '2' THEN '女' ELSE '其餘' END
(2) 示例
SELECT `name`, CASE `name` WHEN 'sam' THEN 'yong' WHEN 'lee' THEN 'handsome' ELSE 'good' END AS `alias` FROM `table_name`; SELECT `name`, CASE WHEN birthday < '1981' THEN 'old' WHEN birthday > '1988' THEN 'yong' ELSE 'ok' END AS `yorn` FROM `table_name`;
INSERT INTO table_name ( column1, column2 ) VALUES ( value1, value2 );
INSERT INTO table_name ( column1, column2 ) VALUES ( value1, value2 ), ( value1, value2 );
INSERT SELECT
插入INSERT INTO table_name ( column1, column2 ) SELECT column1, column2 FROM table_name WHERE c_id=1;
UPDATE `table_name` SET `c_varchar1` = 'string', `c_update_time` = CURRENT_TIMESTAMP WHERE `c_id` = 1;
DELETE FROM `table_name` WHERE `c_id`=1;
-- 刪除原表再新建表 TRUNCATE TABLE `table_name`