Mysql基础代码复习笔记

B站BV1Vt411z7wy学习笔记~
mysql -uroot -p密码

  • show databases;

  • exit;

  • create database xxx;

  • drop database xxx;

  • use table;

  • show tables;

  • describle xxx;

  • …………………………………………………………………………

  • 查询:select * from 数据表名 where 列名=‘xx’;

  • 删除:delete from 表名 where 列名=‘xx’;

  • 修改:update 表名 set 列名1=‘xxx’ where 列名2=‘xxx’;

  • 增加:insert into 表名 values (‘xxx’,‘xxx’);

  • 去重distinct:select distinct 列名 from 表名

  • 求区间 between…and:select * from 表名 where 列名 between 20 and 80;

  • 求区间,而区间在另一个表中的2个列名之间时:select 列名1(表1),列名2(表1),列名3(表2) from 表1,表2 where 列名4(表1) between 列名5(表2) and 列名6(表2);

  • 求区间 >或 <:select * from 表名 where 列名1>20 and 列名1<80;

  • 范围in:select * from 表名 where 列名1 in(xxx,xxx,xxx);

  • 或者条件的or:select * from 表名 where 列名1=‘xxx’ or 列名2=‘xxx’;

  • 排序order by降序desc升序为默认或者asc:select * from 表名 order by 列名1 desc;

  • 多重排序:select * from 表名 order by 列名1(这里是默认列名1升序),列名2 desc;

  • count计数:select count(*) from 表名 where 列名1=‘xxx’;

  • max和min最大最小值:select * from 表名 where 列名1=(select max(列名1)from 表名);

  • limit查询排序后的值:select * from 表名 order by 列名1 desc limit 0,1; (limit后接的第一个数字表示从哪里开始查询,第二个数字表示查询多少条数据)

…………………………………………………………………………

  • 主键约束primary key:create table 表名(name varchar(20) primary key);
  • 创建组合主键约束:create table 表名(name varchar(20),primary key(name));
  • 增加主键约束:alter table 表名 add primary key(列名);
  • 增加主键约束:alter table 表名 modify 列名 数据类型 primary key;
  • 删除主键约束:alter table 表名 drop primary key;
  • 自增约束auto_increment
  • 唯一约束unique:唯一约束不能重复,但可以为空
  • 删除唯一约束:alter table 表名 drop index 唯一约束的列名;
  • 非空约束not null
  • 默认约束default:插入字段数据时如果没有输入值,使用默认值
  • 外键约束foreign key:foreign key (列名) references 外表表名(外表列名)
  • 设置了外键后,主表中没有的数据值,在副表中是不可以使用的;主表中的记录被副表引用后,主表是不可以再删除被引用的数值的

………………………………………………………………………………

  • 平均值avg:select avg(列名) from 表名
  • 分组group by: select * from 表名 group by 列名;
  • having与group by组合使用,增加筛选条件:select * from 表名 group by 列名 having count(*)>=2;
  • 模糊查询 like’xx%’:select * from 表名 where 列名 like ‘xx%’;
  • 多表查询:select 列名1,列名2,列名3 from 表名1,表名2,表名3 where 表名1.列名a=表名2.列名b,表名2.列名c=表名3.列名d;
  • as重命名:select 列名1 as 重命名 from 表名;
  • 多条件查询时,不同条件使用and:条件1 xxxxxx and 条件2 xxxxx;
  • 从日期中提取年份:year(日期的列名)
  • 提取当前年份:year(now())
  • 合并筛选结果union
  • 至少满足任意一个条件any:select * from 表名 where 列名 >any(数据1,数据2,数据3);
  • 需要分组对比数据,而同表同字段不能对比时,假设复制出一张相同的表,表a和表b进行对比:select * from 表名a where 列名1<(select avg(列名1) from 表b where a.分组列名2=b.分组列名2);

…………………………………………………………………………

  • 内连接 inner join 或者join:通过某字段查询2个表的相关记录
    select * from 表1 inner join 表2 on 表1.列1=表2.列2
  • 外连接的左连接 left join 或者left outer join:左边表的数据全部显示,右边有则显示,没有则显示null
    select * from 表1 left join 表2 on 表1.列1=表2.列2
  • 外连接的右连接:right join 或者right outer join
  • 完全外连接:full join 或者full outer join

…………………………………………………………………………

  • 查询事务:select @@autocommit; (1为自动提交,0为关闭自动提交)
  • mysql默认开始事务自动提交,自动提交事务为autocommit,一旦执行,不能回滚
  • 事务回滚rollback
  • 关闭自动提交:set autocommit=0
  • 自动提交关闭的情况下,需要插入一条数据且不允许回滚,可以在插入数据后使用commit;语句
  • 自动提交的情况下,需要插入数据且允许回滚,可以在插入数据前使用begin;或者start transaction;来手动开启事务,然后插入数据
  • 事务的隔离性 【未理解】
  • read uncommitted; 读未提交的
  • read commited; 读已经提交的
  • repeatable read; 可以重复读 (默认级别)
  • serializable; 串行化
  • 查询数据的系统隔离级别:select @@global.transaction_isolation;
  • 查询数据的会话隔离级别:select @@transaction_isolation;
  • 修改隔离级别:set global transaction isolation level read uncommitted;
  • read-uncommitted > read-committed > repeatable-read > serializable
    隔离级别越高,性能越差
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值