001-常用命令汇总

1. 命令行

mysql -uroot -p [-hlocalhost]
exit/quit

2. SQL

2.1. SQL简介

2.2. DCL

2.2.1. 数据库账号创建命令create user

create user hanma@'192.168.0.%' identified with 'mysql_native_password' by '123456';
alter user username@'localhost' identified with mysql_native_password by 'password';
create user username@'localhost' identified with mysql_native_password by 'password' with max_user_connection 1;

2.2.2. 数据库用户授权语句 grant

grant select(user,host) on mysql.user to hanma@'localhost';
grant select on mysql.user to hanma@'localhost';
grant select,insert,delete,update on mysql.user to hanma@'localhost';
grant select(user,host) on mysql.* to hanma@'localhost';
grant all privileges on *.* to hanma@'localhost';
flush privileges;

2.2.3. 回收授权语句 revoke

revoke all on *.* from hanma@'localhost';
revoke select on mysql.user from hanma@'localhost';
revoke select,insert,delete,update on mysql.* from hanma@'localhost';
flush privileges;

2.3. DDL

2.3.1. 概览

2.3.2. 数据库

create database 数据库名;
drop database 数据库名;

2.3.3. 表

create table 表名(
	列名1 数据类型 [约束],
	列名2 数据类型 [约束],
	...
);
-- 示例1:
create table user1(
	id int not null,
	name char,
	age int
);
-- 示例2:
create table user2(
	id INT not null primary key,
	username varchar(255) not null,
	password varchar(20) not null,
	sex tinyint COMMENT '性别:1 男, 2 女'
);
drop table 表名;
describe 表名;
desc 表名;
show create table;
truncate table 表名;
delete from 表名;
alter table 旧表名 rename 新表名;

2.3.4. 表字段

-- 添加一个字段
alter table 表名 add column 列名 数据类型[约束];
-- 添加多个字段
alter table 表名 add column 列名 数据类型[约束],
				add column 列名 数据类型[约束],
				add column 列名 数据类型[约束];
alter table 表名 change 旧字段名 新字段名 新数据类型[约束];
alter table 表名称 drop 字段名;

2.3.5. 表索引

create index 索引名称 on 表名称(字段);
alter table 表名称 add index 索引名称(字段);
create unique index 索引名称 on 表名称(字段);
alter table 表名称 add unique 索引名称(字段);
create fulltext 索引名称 on 表名称(字段);
alter table 表名称 add fulltext 索引名称(字段);
show index from 表名称;
drop index 索引名称 on 表名称;

2.4. DML

2.4.1. insert

insert into 表名(列1,列2,...) values(值1,值2,...);

2.4.2. delete

delete from 表名 where 条件;

2.4.3. update

update 表名 set 列1 = 新值1,列2 = 新值2 where 条件;

2.4.4. select

见:003-select