-----MySql数据库操作基础-----
#显示数据库
show databases;
#判断是否存在数据库db,有的话先删除
drop database if exists db;
#创建数据库
create database db;
#删除数据库
drop database db;
#使用该数据库
use db;
#显示数据库中的表
show tables;
#先判断表是否存在,存在先删除
drop table if exists student;
#创建表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
#删除表
drop table student;
#查看表的结构
describe student; #可以简写为desc student;
#插入数据
insert into student values(null,'aa','男','1988-10-2','......');
insert into student values(null,'bb','女','1889-03-6','......');
insert into student values(null,'cc','男','1889-08-8','......');
insert into student values(null,'dd','女','1889-12-8','......');
insert into student values(null,'ee','女','1889-09-6','......');
insert into student values(null,'ff','null','1889-09-6','......');
#查询表中的数据
select * from student;
select id,name from student;
#修改某一条数据
update student set sex='男' where id=4;
#删除数据
delete from student where id=5;
# and 且
select * from student where date>'1988-1-2' and date<'1988-12-1';
# or 或
select * from student where date<'1988-11-2' or date>'1988-12-1';
#between...and... 在......和......之间
select * from student where date between '1988-1-2' and '1988-12-1';
#in 查询制定集合内的数据
select * from student where id in (1,3,5);
#order by 排序 asc 升序 desc 降序 默认升序排列
select * from student order by id asc;
#group by 分组查询 #聚合函数
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as '求平均' from student;
select count(*) from student; #统计表中总数
select count(sex) from student; #统计表中性别总数 若有一条数据中sex为空的话,就不予以统计~
select sum(id) from student;
#limit n,m 查询第n条以后到第m条的数据(不包括第n条)
select * from student limit 2,5; #显示3-5条数据