本文共 4786 字,大约阅读时间需要 15 分钟。
net start mysql;
net stop mysql;
mysql -u用户名 -p密码
mysql -u用户名 -p密码
mysql -hIP地址 -u用户名 -p密码
mysql --host=IP地址 --user=root --password=root
exit quit
-- 这是单行注释,后面必须有空格# 这是单行注释
/* 这是多行注释,可以和Java一样使用 */
create database 数据库名称;
create database if not exists 数据库名称;
create database 数据库名称 character set 字符集名称;
create database if not exists 数据库名称 character set 字符集名称;
show databases;
show create database 数据库名称;
select database();
alter database 数据库名称 character set 字符集名称;
drop database 数据库名称;
drop database if exists 数据库名称;
use 数据库名称;
create table 表名( 列名1 数据类型1, ... 列名n 数据类型n);
create table student( id int, name varchar(10), age int, score double(4,1), birthday date, insert_time timestamp);
create table 表名 like 被复制的表名;
desc 表名;
alter table 表名 rename to 新表名;
alter table 表名 character set 字符集名称;
alter table 表名 add 列名 数据类型;
alter table 表名 add(列名1 数据类型1, ...);
alter table 表名 modify 要修改的列名 修改后的数据类型;
alter table 表名 change 要修改的列名 修改后的列名 修改后的数据类型;
alter table 表名 drop 列名;
drop table 表名;
drop table if exists 表名;
insert into 表名(列1, ...) values(值1, ...);
insert into 表名 values(值1, ..., 值n);
update 表名 set 列名1 = 值1, ... where 条件;
update 表名 set 列名1 = 值1, ... where 条件;
delete from 表名;
delete from 表名 where 条件;
select * from 表名;
select 要查询的列名1, ..., 要查询的列名n from 表名;
select distinct 要查询的列名1, ..., 要查询的列名n from 表名;
select ifnull(要查询的列名1, 替换后的值) from 表名;
select 列名1 as 别名1, 列名2 as 别名2 from 表名;
select * from 表名 where 列名 > 指定参数;
select * from 表名 where 列名 >= 指定参数;
select * from 表名 where 列名 = 指定参数;
select * from 表名 where 列名 != 指定参数;
select * from 表名 where 列名 >= 指定参数1 and 列名 <= 指定参数2;
select * from 表名 where 列名 in (指定参数1, 指定参数2);
select * from 表名 where 列名 is null;
select * from 表名 where 列名 is not null;
select * from 表名 where 列名 like '___';
select * from 表名 where 列名 like '%';
select * from 表名 where 列名 like '_指定字符%';
select * from 表名 order by 列名1, 列名2;
select * from 表名 order by 列名1 desc, 列名2 desc;
select count(*) from 表名;
select max(列) from 表名;
select min(列) from 表名;
select sum(列) from 表名;
select avg(列) from 表名;
select 列 from 表名 group by 分组条件;
select * from 表名 group by 分组条件 having 条件;
select * from 表名 where 条件 group by 分组条件 having 条件;
select * from 表名 limit 起始索引, 显示条数;
select @@autocommit;
set @@autocommit = 参数;
commit;
select @@tx_isolation;
set global transaction isolation level 隔离级别;
start transaction;
use mysql;
select * from user;
create user '用户名'@'主机名' identified by '密码';
drop user '用户名'@'主机名';
update user set password = password('新密码') where user = '用户名';
set password for '用户名'@'主机名' = password('新密码');
show grants for '用户名'@'主机名';
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';
mysqldump -u用户名 -p密码 数据库 [表名称] > 保存路径/文件名.sql;
mysql -u用户名 -p密码
use 数据库名称;
source 保存路径/文件名.sql;
select * from 表1, 表2 where 表1.关联字段 = 表2.关联字段;
select * from 表1 inner join 表2 on 表1.关联字段 = 表2.关联字段;
select * from 表1 left outer join 表2 on 表1.关联字段 = 表2.关联字段;
select * from 表1 right outer join 表2 on 表1.关联字段 = 表2.关联字段;
select * from 学生表 where 学生表.成绩 = (select max(成绩) from 学生表);
select * from 学生表 where 学生表.成绩 < (select avg(成绩) from 学生表);
select * from 学生表 where 学生表.课程id in (select 课程id from 课程表 where name = '语文' or name = '数学');
select * from 班级表, (select * from 学生表 where 入职时间 = '2020-9-1') as 学生表 where 班级表.id = 学生表.班级id;
select 班级表.班级id, 班级表.班级名称, (select count(id) from 学生表 group by 班级id) as 班级人数 from 班级表;
select @@autocommit;
set @@autocommit = 参数;
commit;
select @@tx_isolation;
set global transaction isolation level 隔离级别;
start transaction;
通过以上内容,可以全面了解MySQL的操作与管理,包括数据库和表的创建、查询、修改等操作,权限管理,以及事务操作等。
转载地址:http://ovdfk.baihongyu.com/