mysql基础汇总

命令行操作

  • 启动数据库服务:net start mysql;
  • 进入mysql:mysql -u root -p;
  • 退出mysql:shutdown;

基础操作

库操作:

  • 查看所有库: show databases;
  • 创建库:create database 库名;
  • 使用库:use 数据库名;
  • 删除库:drop database 数据库名;

表操作:

  • 查看所有表:show tables;
  • 查看表结构:desc 表名称;
  • 创建表:create table 表名(

字段名 字段类型 comment 注释,

字段名 字段类型 comment 注释

);

  • 删除表:drop table 表名;
  • 修改表名: alter table 表名 rename to 新的表名;

字段操作:

  • 添加字段:alter table 表名 add 字段名 字段类型 ;
  • 修改字段类型: alter table 表名 modify 字段名 新的字段类型;
  • 修改字段: alter table 表名 change 旧字段名 新字段名 新的类型 ;

数据操作:

  • 添加数据: insert into 表名 values (值1,值2…),(值1,值2,…);
  • 修改数据:update 表名 set 字段名1=值1,字段名2=值2 where 条件;
  • 删除数据: delete from 表名 where 条件;

查询操作

基础查询:

  • 基础查询:select 字段名 from 表名 where 条件 ;
  • 分组查询:select 字段名 from 表名 where 条件 group by 字段名 having 条件;
  • 排序查询:select 字段名 from 表名 where 条件 order dy 字段名 desc/asc(降序/升序);
  • 分页查询:select 字段名 from 表名 limit 起始索引,查询记录数;

查询操作的顺序:

FROM -> WHERE -> GROUP BY -> SELECT -> ORDER BY -> LIMIT

合并查询:

  • 内连接:select 字段名 from 表1 [as 别名1],表二 where 条件;
  • 左外连接:select 字段名 from 表1 left join 表二 on 条件;
  • 右外连接:select 字段名 from 表1 right join 表二 on 条件;
  • 自连接查询:select 字段名 from 表 as 别名1,表名 as 别名2 on 条件;
  • 联合查询select 字段名 from 表1 where 条件 union [all] select 字段名 from 表2 where 条件;
  • 子查询:select 字段名 from 表1 where 字段=(select 字段名 from 表2 where 条件);

约束

常用约束;

  • 非空约束:not null;
  • 主键:primary key;
  • 自动增长:auto_increment;
  • 默认值:default;
  • 唯一:unique;
  • 逻辑条件:check;

外键约束

  • 添加外键:alter table 表名 add constaint 外键名 foreign key(外键字段名) references 主表(主表字段名);
  • 删除外键:alter table 表名 drop foreign key 外键名;

事务

事务常用语法:

  • 查看提交方式:select @@autocommit;
  • 修改提交方式:set @@autocommit=0/1(手动/自动);
  • 开启事务:start transaction;
  • 提交事务:commit;
  • 回滚:rollback;

视图

  • 创建视图:create view 视图名称 as (select 字段名 from 表名 where 条件);
  • 使用视图查询:select * from 视图名称;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值