博客
关于我
mysql练习语句
阅读量:790 次
发布时间:2023-02-13

本文共 3233 字,大约阅读时间需要 10 分钟。

MySQL复习

一、复习前的准备

确认已安装MySQL数据库环境,确保ECShop数据库已创建并连接成功。

二、基础知识

1. 数据库连接

使用以下命令连接MySQL数据库:

mysql -u [username] -p -h [host]

参数说明:

  • -u:用户名
  • -p:密码
  • -h:主机地址

2. 库级操作

2.1 显示数据库列表

show databases;

2.2 选择数据库

use [database_name];

2.3 创建/删除数据库

create database [db_name] charset utf8;drop database [db_name];

三、表级操作

3.1 查看表结构

desc [table_name];

3.2 查看表创建语句

show create table [table_name];

3.3 创建新表

create table [table_name] (    [字段名] [字段类型] [参数] ...) engine=innodb charset=utf8;

3.4 修改表结构

  • 添加列:
alter table [table_name] add [column_name] [type] [参数];
  • 修改列:
alter table [table_name] change [旧名] [新名] [type] [参数];
  • 删除列:
alter table [table_name] drop column [column_name];
  • 添加主键:
alter table [table_name] add primary key ([主键列名]);
  • 删除主键:
alter table [table_name] drop primary key;

四、增删改查

4.1 插入数据

insert into [table_name] ([列名1], [列名2] ...) values (值1, 值2 ...);

4.2 修改数据

update [table_name] set [列名] = 值 where 条件;

4.3 删除数据

delete from [table_name] where 条件;

4.4 查询数据

查询基础语法:

select [列名] from [table_name] where 条件;

复杂查询:

  • 分组查询:
select max([列名]) from [table_name];
  • 过滤和排序:
select * from [table_name] order by [列名] desc limit n;

五、连接查询

5.1 左、右、内连接

left join tableA on [条件];right join tableB on [条件];inner join tableC on [条件];

六、子查询

6.1 where型子查询

select * from [tableA] where [条件] = (select [列名] from [tableB] where [条件]);

6.2 from型子查询

select * from (select * from [tableA]) as [alias];

七、触发器

创建触发器:

create trigger [trigger_name] after|before [事件] on [table_name] for each row [触发语句];

删除触发器:

drop trigger [trigger_name];

八、索引

8.1 索引类型

  • 主键索引
  • 普通索引
  • 唯一索引
  • 全文索引

九、字符集与存储引擎

9.1 存储引擎选择

  • innodb:支持事务,适合高并发
  • myisam:速度快,不支持事务

9.2 字符集设置

set names utf8;

十、事务管理

10.1 开始事务

start transaction;

10.2 提交/回滚

commit;rollback;

十一、综合练习

创建数据库并建立表:

create database shop charset utf8;use shop;create table goods (    goods_id int auto_increment,    goods_name varchar(20) not null,    cat_id int,    brand_id int,    goods_sn varchar(50),    goods_number int,    shop_price decimal(10,2),    click_count int default 0) engine=innodb charset=utf8;

插入数据:

insert into goods (goods_name, cat_id, brand_id, goods_sn, goods_number, shop_price, click_count) values ('KD876', 4, 8, 'ECS000000', 10, 1388.00, 7),('诺基亚N85原装充电器', 8, 1, 'ECS000004', 17, 58.00, 0),('诺基亚原装5800耳机', 8, 1, 'ECS000002', 24, 68.00, 3),...

执行完毕后,进行索引优化:

alter table goods add index idx_goods_name (goods_name);alter table goods add index idx_shop_price (shop_price);alter table goods add index idx_click_count (click_count);alter table goods drop index idx_goods_name;

十二、查询知识

12.1 基础查询

select goods_id, goods_name, shop_price from goods where goods_id = 32;

12.2 分组查询

select cat_id, max(shop_price) from goods group by cat_id;

12.3 having与group结合

select name, sum(score < 60) as gk, avg(score) as pj from stu group by name having gk >= 2;

十三、排序与限制

13.1 按价格排序

select * from goods order by shop_price desc limit 3;

十四、连接查询

14.1 外连接

select goods_name, cat_name, shop_price from goods left join category on goods.cat_id = category.cat_id;

十五、Union查询

15.1 合并数据

select id, sum(num) from (select * from ta union all select * from tb) group by id;

十六、触发器示例

创建触发器:

create trigger tg_goods_update after update on goods for each rowupdate other_table set field = new.field where id = new.id;

总结

以上内容涵盖了MySQL的基础操作和高级功能,适合用于技术复习和实际项目中的应用。

转载地址:http://eadfk.baihongyu.com/

你可能感兴趣的文章
mysql快速查询表的结构和注释,字段等信息
查看>>
mysql怎么删除临时表里的数据_MySQL中关于临时表的一些基本使用方法
查看>>
mysql性能优化
查看>>
MySQL性能优化必备25条
查看>>
Mysql性能优化(1):SQL的执行过程
查看>>
Mysql性能优化(2):数据库索引
查看>>
Mysql性能优化(3):分析执行计划
查看>>
Mysql性能优化(4):优化的注意事项
查看>>
Mysql性能优化(6):读写分离
查看>>
MySQL性能测试及调优中的死锁处理方法
查看>>
mysql性能测试工具选择 mysql软件测试
查看>>
Mysql悲观锁
查看>>
MySQL慢查询-开启慢查询
查看>>
MySQL慢查询分析和性能优化的方法和技巧
查看>>
MySQL慢查询日志总结
查看>>
Mysql慢查询日志,查询截取分析
查看>>
MySQL慢查询问题排查
查看>>
mysql截取sql语句
查看>>
mysql截取身份证号前几位_EXCEL中怎样截取身份证号前六位数字
查看>>
mysql手工注入
查看>>