一、数据库相关操作
1. 创建数据库
create database 库名;
2. 查看所有数据库
show databases;
3. 查看数据库详情
show create database 库名;
4. 使用数据库
use 库名;
5.删除数据库
drop database 库名;
二、表相关操作
1. 创建表
create table 表名(id int(10)not null unique primary key,name varchar(20) not null,sex varchar(4));
2. 删除表
drop table 表名;
drop table 表名1,表名2;
3. 查看有哪些表
show tables;
4. 查看表结构
desc 表名;
5. 在表里添加字段
alter table 表名 add column 字段名 int(10) not null;
6. 在表里修改字段名;
alter table 表名 change 旧字段名 新字段名 新字段类型;
7. 删除字段
alter table 表名 drop 字段名;
8. 复制表结构,并创建新的表
create table 新表名 like 旧表名;
9. 复制表结构及数据,并创建新表
create table 新表名 as select * from 旧表名;
10. 复制表结构中指定字段,并创建新的表
create table 新表名 as select id,name,sex from 旧表名 where 1<>1;
三、数据相关操作
查询
1. 查询表中所有数据
select * from 表名;
2.去重(distinct)
select distinct 字段名 form 表名;
3.列重命名
select id as '编号', name '名称', sex '性别' from 表名;
4. 排序(desc降序,asc升序)
select * from 表名 order by 字段名 desc;
select * from 表名 order by 字段名 asc;
5.分组(可以和聚合函数一起使用:sum、count、max、min)
select * from 表名 group by 字段名 ;
select count(*) from 表名 group by 字段名;