DDL(数据定义)
库
创建数据库 CREATE DATABASE database_name;
删除数据库 DROP DATABASE database_name;
选择数据库 USE database_name;
表
创建表格 CREATE TABLE table_name( column1 datatype, column2 datatype, ... );
删除表格 DROP TABLE table_name;
修改表格
ALTER TABLE table_name ADD column_name datatype; -- 添加列
ALTER TABLE table_name MODIFY column_name datatype; -- 修改列
ALTER TABLE table_name DROP column_name; -- 删除列
约束
设置主键
ALTER TABLE table_name ADD CONSTRAINT constraint_name
PRIMARY KEY (column1, column2, ...);
设置外键
ALTER TABLE table_name ADD CONSTRAINT constraint_name
FOREIGN KEY (column) REFERENCES other_table(column);
索引
创建索引 CREATE INDEX index_name ON table_name (column1, column2, ...);
删除索引 DROP INDEX index_name ON table_name;
DML(数据操纵)
查询数据
SELECT column1, column2, ... FROM table_name WHERE condition;
ORDER BY xx --升序
ORDER BY xx DESC --降序
插入数据
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
更新数据
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
删除数据
DELETE FROM table_name WHERE condition;
DCL(数据控制)
创建用户
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
删除用户
DROP USER 'username'@'host';
修改密码
SET PASSWORD FOR 'username'@'host' = 'new_password';
刷新权限
FLUSH PRIVILEGES;
授予权限
GRANT privileges ON database_name.table_name TO 'user'@'host';
收回权限
REVOKE privileges ON database_name.table_name FROM 'user'@'host';