【mysql】基础操作

重看

  • 复制数据表(带数据)
mysqldump base --add-drop-table | mysql test_base;
  • 创建数据库
 create database <name>;
  • 使用数据库
use <name>;
  • 查看所有数据库
show databases;

计数

  • 计算某列中某值有多少行
select count(*) from <> where gender = "男";
  • 计算某个列有多少不同值
select count(distinct <column> from <table_name>);

更新

  • update
update <table_name> set column=<value> where column=<value>;

删除

  • delete
delete from <table_name> where column = <value>;

其他

  • 别名
- select column as <name> concat(volumn,"+",volumn_2) as volumn_3 from table_name limt 5;
  • join

  • inner join

  • 左表和右表的交集

  • left join

  • 返回所有左表数据,左右表未匹配的补null

  • right join

  • union(合并查询结果)

  • select <column_1,column_2> from <table_name> where column_3= union select <column_1,column_2> from <table_name> where column_3=

  • select <> into

  • 简单插入

  • select column_1,column_2 into <new_table> from <old_table>

  • 多表插入

  • select table_1.column_1,table_2.column_2 into <new_table> from table_1 left join table_2 on table_1.column_3 = table_2.column_4

  • 备注: 先into 后 left join

  • 创建空表

  • select * into <new_table> from table_1 where 1=0;

  • insert into <> select

  • insert into table_2(name,country) select (name,country) from table_1 where id =1;

  • alter (在已有表中添加、删除或修改列)

  • 增加列

  • alter table <table_name> add <column_name> ;

  • 删除列

  • alter table <table_name> drop column <column_name>;

  • 修改数据类型

  • alter table <table_name> alter column <column_name>

  • 创建索引

  • create index <index_name> on <table_name> <column_name>

  • 备注 索引名 对应 所有索引 ,即索引名表的属性

  • 函数

  • having

  • where无法与aggregate函数一起使用

  • 索引过长

    • 原因

      • mysql 索引长度为 max_length * 3 ,而最大索引长度为767bytes
    • 解决方案

      • 使用innodb引擎

      • 修改索引字段的max_length

      • my.conf
        default-storage-engine=INNODB

                   Innodb_large_prefix=on
        
  • text作为联合索引的问题

    • 由于text是可变长度,所以在做索引时需要指定取作为索引的长度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴姬压酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值