mysql 数据库 简单命令

本文详细介绍了MySQL数据库的基本操作,包括库和表的管理、数据的增删改查等实用技巧,并对比了不同表类型的优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 库操作
    • mysql -u root -p; -----------------------------启用 进入musql
    • show databases; -----------------------------查看所有库
    • create database 库名 default charset=utf8; --创建库
  • 表操作
    • --------------------表结构操作------------------------------
      • show tables; ----------------------------------查看库中的表
      • desc 表名; ------------------------------------查看表结构
      • create table 表名( 字段 属性(约束条件) ,) default charset = utf8; --------创建表
      • drop table 表名--------------------------------删除表
      • alter table 表名 add 新字段(约束条件)--------------------------添加新字段,默认尾部添加
      • alter table 表名 add 新字段(约束条件) after 字段-------------- after 向某个字段后追加一个新字段
      • alter table 表名 drop 指定字段 --------------------------------- 指定删除某个字段
      • alter table 表名 change 旧字段名 新字段名 (约束条件)--------- change 可修改字段名
      • alter table 表名 modify 字段(新约束条件)-----------------------不可修改字段名,只能修改字段属性
      • alter table 表名 add unique 字段(唯一索引)-------------------- unique 添加唯一索引
      • alter table 表名 add index 字段( 普通索引) -------------------- index 添加普通索引
      • alter table 表名 drop (unique or index) 索引值 ---------------- drop 删除索引
      • delete from 表名 ------------------------------------------------ 删除数据,(不重置自增)
      • truncate table 表名 --------------------------------------------- 清空表数据,重置自增
      • alter table 旧表名 rename as 新表名 --------------------------- rename as 修改表名
      • alter table 表名 auto_increment = 1 ---------------------------- 重置自增初始值
      • alter table 表名 engine = 'myisam'-------------------------------更改表类型
        • innodb在删除数据并重启后,会重新计算最大的id
        • myisam在删除数据并重启后,不会重新计算,
        • -- 常用的表类型(引擎)  Innodb  Myisam
        • Innodb
        • 当前表是由两个文件组成 .frm  .ibd
        • 一个存储 表结构,一个存储数据和索引
        • 对数据的操作效率高,
        • 支持事务
        • Myisam
        • 一个表由 三个 文件组成 .frm  .MYD  .MYI
        • 一个存储表结构,一个存储数据,一个存储索引
        • 数据的读,效率高
        • MyISAM相对简单所以在效率上要优于InnoDB
        • 不支持事务
    • ----------------------数据增删改查----------------------------
      • select * from 表名 ;--------------------------------------------------查询表内数据;
      • select 字段1,字段2 from 表名---------------------------------------查询表内指定字段数据
      • select 字段1 as 别名 from 表名-------------------------------------- as 别名查询(用于长字段重复操作)
      • select * from 表名 where 条件 -------------------------------------- where 条件查询
        • 可使用比较运算符 > < >= <= !=
        • 逻辑运算 and or in not in
      • select * from 表名 where 字段 like '_%' ----------------------------- like 模糊查询
        • % 代表任意位数的任意字符
        • _ 代表任意的单个字符

      • select max(age) ,min(age),avg(age) from 表名; -------------------- 统计函数(聚合函数)
        • max() 最大值 min() 最小值, avg() 平均值 cont() 总个数 sum() 和 数值
      • select 字段, 字段1 from 表名 group by 分组字段;------------------- group by 分组统计
        • 分组 根据一个或者多个列队进行分组 用于统计 给某个字段 进行分组

      • select * from 表名 order by 字段[id] -(默认esc)-------------------- order by 排序 asc升序 desc降序
        • 可以在where条件后面 指定字段进行排序
        • 默认为升序 asc
        • 也可以降序 desc
      • select * from 表名 limit 2 ( 某表前两条数据) ------------------------- limit 分页 关键字 查询部分数据
        • 一共10条数据,每页显示2条,
        • 第一页  limit 0,2
        • 第二页  limit 2,2
        • 第三页  limit 4,2
        • 第n页  limit (n-1)*2,2
        • 第n页  limit (n-1)*2,2
      • select * from [where ...] [group by ...]  [ order by ...]   [limit ...] -----sql 语句 顺序
      • select 表1.字段,表2 字段 from 表1,表2 where 表1.id = 表2.id;-------- where 多表连查( 两表要有关联字段)
        • -- 查询所有学生信息,并跨表显示对应的班级名称信息
        • select stu.*,class.* from stu,class where stu.classid = class.id;
        • -- select s.*,c.* from stu s,class c where s.classid = c.id;
        • select stu.*,class.cname from stu,class where stu.classid = class.id;
        • -- select s.*,c.cname classname from stu s,class c where s.classid = c.id;
        • -- 获取张三和所在班级信息
        • select stu.*,class.cname from stu,class where stu.classid = class.id and stu.username ='张三';
        • -- select stu.id,stu.username,class.cname from stu,class where stu.classid = class.id and stu.username ='张三';
        • -- 统计每个班级的人数
        • -- select classid,count(id) from stu group by classid; X
        • select c.cname,count(s.id) from stu as s,class as c where s.classid = c.id group by s.classid;
      • select 表1.*, 表2.* from 表1 inner join 表2 on 表1.id = 表2.id;------ inner join on 链接式查询
        • inner join 内连接 获取两个表中字段匹配关系的记录。
        • left join  左链接 获取左表所有记录,即使右表没有对应匹配的记录
        • right join 右链接  与 LEFT JOIN 相反
      • insert into 表名 ( 字段) ,values(值) ------------添加值
        • -- 指定部分字段和值 注意,有些字段不能为null,必须写
        • insert into stu(username) values('aabbcc');
        • -- 批量添加
        • insert into stu values
        • (null,'ccc',20,'男'),
        • (null,'eee',20,'男'),
        • (null,'adf',20,'男');
      • delete from 表名 where id = ? ---------------删除指定数据
      • updata 表名 set 字段 = 新值,字段=新值 where 条件-----------修改字段值
  • 事务
    • 事务就是把 多条sql操作 看成一个操作单元,要么都成功,要么都失败
    • 注意,只右innodb支持事务
    • BEGIN       事务开启
    • ROLLBACK    事务回滚 意味回到事务开启前的状态
    • COMMIT      事务提交 意味着执行事务中所有操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值