写给自己的MySQL速查文档

本文提供MySQL的基本操作指南,包括数据库及表的管理、增删改查等核心SQL语句,并介绍如何利用索引、视图等功能提升数据处理效率。

mysql 语句速查文档

基本命令

输入密码 登录

mysql -u root -p

显示数据库

show databases;

使用某一个数据库

use xxx;

显示表

show tables;

显示表的结构

desc xxx;

sql查询语句

基本查询

select * from table_xxx
where 字段=xxx;

创建/删除数据库

create database yourdatabase;
drop database yourdatabase;

创建/删除表

create table xxx(col1 type1 [not null][primary key], col2 type2 [not null], ...)
drop table  xxx;

增加/删除列

alter table xxx add column_name column_type;
alter table xxx drop column xxx;

添加主键

alter table xxx add primary key(col);
alter table xxx drop primary key(col);

创建索引

create index idxname on tabxxx;
drop index idxname on tabxxx;

创建视图

create view xxx as select statement;
drop view xxx;

查询语句:

为方便表示,假设
数据库:en 表:article 结构(id,title,body)

        select * from article
        select distinct * from article
        select top 10 * from article

        where id=32
        where id like '%32%'
        where id in (132,32,42)
        where id between 12 and 32
        order by id desc    默认是升序

更新语句

        update article set title='hello' where id=32;
        update article set title='hello',body='nice' where id=32;

删除语句

        delete from article where id=32;
        delete from article;

添加语句

        insert into article(id,title) values(32,'hello');
        insert into 目标表 select * from 源表;

修改语句

        alter table article alter column title varchar(40) not null;

数据统计

        avg(id)    平均值
        count(id)  数量
        max(id)    最大值
        min(id)    最小值
        sum(id)    总和

其他

        查询有同一字段的表
        根据出生日期可算出年龄
        在同一数据库中复制表结构
        批量插入
        不同数据库之间的复制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值