常用的 SQL 语句

数据库操作

CREATE DATABASE db_name;
SHOW CREATE DATABASE db_name;
SHOW DATABASES;
USE db_name;
DROP DATABASE db_name;

表操作

CREATE TABLE tb_name (
  id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(60) NOT NULL,
  score TINYINT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY(id)
) ENGINE = InnoDB DEFAULT charset = utf8;
SHOW CREATE TABLE tb_name;
SHOW TABLES;
DESCRIBE tb_name;
-- show table status vertically
SHOW TABLE STATUS LIKE 'tb_name'\G;
-- rename
RENAME TABLE tb_name TO new_tb_name
ALTER TABLE tb_name RENAME new_tb_name;
DROP TABLE IF EXISTS tb_name;

列操作

-- show columns,equal describe tb_name
SHOW COLUMNS FROM tb_name;
-- edit column
ALTER TABLE tb_name ADD col_name VARCHAR(80) NOT NULL;
ALTER TABLE tb_name MODIFY col_name INT DEFAULT 0;
ALTER TABLE tb_name CHANGE col_name new_col_name SMALLINT(4) NOT NULL;
ALTER TABLE tb_name ALTER col_name SET DEFAULT 0;
ALTER TABLE tb_name DROP col_name;
-- primary key
ALTER TABLE tb_name ADD PRIMARY KEY(col_name);
ALTER TABLE tb_name DROP PRIMARY KEY;
-- foreign key
ALTER TABLE tb_name ADD CONSTRAINT fk_name FOREIGN KEY (col_name) REFERENCES tb2_name(col_name);
ALTER TABLE tb_name DROP FOREIGN KEY fk_name;
-- index
CREATE [UNIQUE] INDEX idx_name ON tb_name(col_name);
ALTER TABLE tb_name DROP INDEX idx_name;

权限操作

-- change password
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
-- create user and grant privileges
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, ALTER, DROP ON db_name.tb_name IDENTIFIED BY 'password' WITH GRANT OPTION;
-- revoke privileges
REVOKE ALL, GRANT ON db_name.tb_name FROM user_name;
-- show privileges
SHOW GRANTS FOR user_name;

增删查改

-- select
SELECT col1, col2 FROM tb_name;
SELECT DISTINCT col1, col2 FROM tb_name;
-- null
SELECT * FROM tb_name WHERE col1 IS NULL;
SELECT * FROM tb_name WHERE col1 IS NOT NULL;
-- order
SELECT * FROM tb_name WHERE col1 = val1 ORDER BY col2 ASC, col3 DESC;
-- pagination
SELECT * FROM tb_name LIMIT 10;
SELECT * FROM tb_name LIMIT 0, 10;
SELECT * FROM tb_name LIMIT 10 OFFSET 0;
-- like
SELECT * FROM tb_name WHERE col1 LIKE 'abc%';
SELECT * FROM tb_name WHERE col1 NOT LIKE '_abc';
-- in
SELECT * FROM tb_name WHERE col1 IN (val1, val2, val3);
SELECT * FROM tb_name WHERE col1 IN (SELECT col1 FROM tb2_name);
-- between
SELECT * FROM tb_name WHERE col1 BETWEEN val1 AND val2;
-- group
SELECT col1, count(*) as total FROM tb_name GROUP BY col1 HAVING total > 10 ORDER BY total;
-- exisit
SELECT * FROM tb1_name WHERE EXISTS (SELECT * FROM tb2_name WHERE col1 = tb1_name.col1 AND col2 < val2);
-- join
SELECT * FROM tb1_name t1 INNER JOIN tb2_name t2 ON t1.col1 = t2.col1;
SELECT * FROM tb1_name t1 LEFT JOIN tb2_name t2 ON t1.col1 = t2.col1;
SELECT * FROM tb1_name t1 RIGHT JOIN tb2_name t2 ON t1.col1 = t2.col1;
SELECT * FROM tb_name t1, tb_name t2 WHERE t1.col1 = t2.col1 AND t1.col2 != t2.col2;
-- aggregate functions
SELECT MIN(col1) FROM tb_name;
SELECT MAX(col1) FROM tb_name;
SELECT AVG(col1) FROM tb_name;
SELECT SUM(col1) FROM tb_name;
SELECT COUNT(*) FROM tb_name;
-- count like all aggregate functions, it does not count null as a distinct value
SELECT COUNT(col1) FROM tb_name;
SELECT COUNT(DISTINCT col1, col2) FROM tb_name;
-- insert
INSER INTO tb1_name (col1, col2, col3) VALUES(val1, val2, val3);
INSER INTO tb1_name (col1, col2, col3) VALUES(val1, val2, val3), (val4, val5, val6), (val7, val8, val9);
-- update
UPDATE tb_name SET col1 = val1, col2 = val2 WHERE col1 = val1;
-- delete
DELETE FROM tb_name WHERE col1 = val1;

管理操作

USE information_schema
-- db size
SELECT TABLE_SCHEMA, ROUND(SUM(DATA_LENGTH/1024/1024), 2) as SIZE_MB FROM TABLES GROUP BY TABLE_SCHEMA ORDER BY SIZE_MB DESC;
--- table size
SELECT TABLE_NAME, DATA_LENGTH + INDEX_LENGTH, TABLE_ROWS, ROUND((DATA_LENGTH + INDEX_LENGTH)/1024/1024, 2) as SIZE_MB FROM TABLES WHERE TABLE_SCHEMA='tb_name' ORDER BY SIZE_MB;

注意:如果密码中带有小数点,如 abc.123,在命令行中输入密码 -p[password] 时,需要给密码加个单引号,即 -p'abc.123', 而不是 -pabc.123,这样会出错。

# mysql status
SHOW STATUS;
# mysql variables
SHOW VARIABLES
# import
mysql -h host -u root -p[password] [-D] db_name < file.sql
# backup
mysqldumb -h host -u user_name -p[password] -v --databases db_name > dump.sql
# fix: A dump made using PowerShell on Windows with output redirection creates a file that has UTF-16 encoding:
mysqldumb -h host -u user_name -p[password] -v --databases db_name --result-file=dump.sql

如果这篇文章对您有帮助,记得给作者点个赞,谢谢!

你可能感兴趣的:(常用的 SQL 语句)