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) 总和
其他
查询有同一字段的表
根据出生日期可算出年龄
在同一数据库中复制表结构
批量插入
不同数据库之间的复制