MySQL的一些终端基本指令
1、数据库的操作
连接数据库:
mysql -u root -p
退出数据库:
exit
创建数据库:
create DATABASE <数据库名>;
删除数据库:
drop database <数据库名>;
查看数据库:
show databases;
2、数据表的操作
创建数据表:
create table table_name (column_name column_type);
查看数据表:
show tables;
删除数据表:
drop table table_name ;
查看表结构:
describe table_name; # 可缩写为 desc
插入数据:
insert into table_name ( field1, field2,...fieldN )
values( value1, value2,...valueN );
3、数据查询
查询数据:
select * from table_name
- 关键字 distinct ——排除重复
- where *** between A and B——查询区间
- where *** in ()——表示或者关系的查询
- select * from table_name order by *** desc/asc——升序、降序
- select count(*) from table_name where ***=***——统计
- union——合并
4、连接查询
内连接:
inner join 或者 join
外连接:
left join 或者 left outer join
right join 或者 right outer join
完全连接:
full join 或者 full outer join
5、控制事务
5.1 事务得四大特征:
1、原子性:事务是最小得单位,不可以再分割;
2、一致性:事务要求,同一事务中得sql语句,必须保证同时成功或者同时失败;
3、隔离性:事务1和事务2之间是具有隔离性得;
4、持久性:事务一旦结束(commit,rollback),就不可以返回。
自动提交事务
select @@autocommit;
事务回滚
rollback;
设置为手动提交事务
set autocommit=0;
手动提交事务
commit;
手动开启一个事务
begin;或者 start transaction;
5.2 事务的隔离性:
1、read uncommitted;——读未提交的
2、read committed;——读已经提交的
3、repeatable read;——可以重复读(幻读)
4、serializable;——串行化
查看隔离级别:
select @@global.transaction_isolation; # 系统级别
select @@transaction_isolation; # 会话级别
修改隔离级别:
set global transaction isolation level read uncommitted;