--显示当前时间
select now();
--创建class表(id name两个字段)
use testdb;
create table class (
id tinyint unsigned primary key not null auto_increment,
name varchar(20)
);
desc class;
--创建一个students表(id,name,age,high,gender,cls_id)
create table students (
id smallint unsigned auto_increment primary key,
name varchar(22),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum(‘male‘,‘female‘,‘secret‘) default ‘secret‘,
cls_id tinyint unsigned
);
desc students;
--修改表,添加字段
alter table students add birth datetime;
--修改表修改字段,不重命名
alter table students modify birth date;
--修改表字段,且重命名
alter table students change birth birthday date;
alter table students add birthday datetime;
desc students;
alter table students modify birthday date;
desc students;
alter table students change birthday birth datatime;
drop table classes; #删除classes表
--删除字段
alter table students drop birth;
desc students;
select * from students;
select id,age,name from students;
--给表添加值
insert into students values (1,‘ken‘,23,170,‘male‘,1);
insert into students values (0,‘ken1‘,23,170,‘male‘,1),(0,‘ken2‘,23,170,‘male‘,1);
select * from students;
insert into students values (0,‘pheb‘,24,176.250,’male‘,1);
insert into students values (default,‘pheb‘,24,176.250,’male‘,1);
insert into students values (null,‘pheb‘,24,176.250,’male‘,1);
--三种情况都是走primary-key的默认值
insert into students values (10,‘pheb‘,24,176.250,’male‘,1);
insert into students values (0,‘pheb‘,24,176.250,’male‘,1);
--这个时候再插入数据的时候,就会默认从11开始
select * from students;
show create table students; #可以看到auto_increment的值
insert into students (name,age,high) values ("joy",23,176.24); #如果有些不能为空的字段没有默认值会报错
delete from students where id=1;
select * from students;
delete from students; #会清空这张表
delete from students where name=‘joy‘;
select * from students;
alter table students add is_delete bit(1);
bit(2): 00 01 10 11
--删除表
truncate table students; #不可恢复的删除
delete from students; #可以恢复
delete 删除自增还在,但是如果使用truncate来删除,自增归零。
添加的bit位可能很小,看不到值,但是不影响使用。
select * from students where is_delete=1;
update students set is_delete=1 where name=‘joy‘;
Update students set age=22,class=6 where id=10;
\G 使用竖排去查看,如果横排太乱。
--主键字段可以使用 0 null default 来占位。
--枚举类型也可以使用数字来指代。1——表示定义的第一个枚举类型。超出枚举范围会出现报错的清况。
--delete from 表名 where 条件
--MyIsam和Innodb区别
两种类型最主要的区别就是Innodb支持事物处理与外键和行级锁。
myisam支持表级锁
--update 表名 set 列1=值1,列2=值2..... where 条件;
update students set age=100,cls_id=3 where id=10;
update students set age=100,cls_id=3 where id<=10;
update students set cls_id=null where id<=6;
数据库查询操作:
select name as "姓名",age as “年龄” from students; #只是为了显示阅读的方便,没有更改数据库。
select students.name from students;
select s.name from students as s; #起别名常用于关联查询
select distinct age from students; #消除重复项
--条件查询
select * from students where id < 22;
select * from students where age > 38;
select * from students where age > 18 and age < 30;
select * from students where age <= 23;
select * from students where age >= 18 && age <= 38;
select * from students where age < 18 || age > 33;
select * from students where age between 18 and 28; #头和尾都包含了。
select * from students where age > 18 || high > 170;
--模糊查询
select * from students where name like ‘%锋‘; #%就相当于shell中的*
select * from students where name like ‘%霆%‘;
select * from students where name like ‘__‘; #一个下划线表示一个字
select * from students where name like ‘__%‘;
select * from students where age=16 or age=22;
select * from students where age in (18,34,55,66,22);
select * from students where age not between 18 and 33;
--查询空不能使用=null来查询,update中set使用=null可行
select * from students where high is null;
select * from students where high=175 and name="joy";
select * from students where high=175 or name="joy";
select * from students where high is not null;
--排序
select * from students order by age; #默认从小到大排列
select * from students order by age desc; #descent 降序排列
select * from students order by age desc,high desc;
select * from students order by age asc;
select * from students where age between 18 and 33 and gender=‘male‘ order by age asc;
select * from students where age between 18 and 33 and gender=1 order by age desc;
--聚合函数
select * from students; #查询后会有一个总的行数统计,这个统计是准确的。
--但是这种查询来看总的行数的方式很可能会出现问题,一旦数据量较大,那么会出现卡死的现象,也就是说select * from students实际上会将
所有的内容读到你的内存里,大量的io操作会导致系统崩溃。
所以一般会先看看一张表里有多少行,再进行查询的操作。
--查询总行数
select count(name) from students;
select count(*) from students; #这种统计方式很准确,只要任意一个字段占一行都算。
select count(high) from students; #可能会少几行,如果有null的情况。
select count(*) as ’total‘ from students;
--查询最大
select max(age) from students;
select name,max(age) from students; #这条查询语句发生了错位,姓名和max不匹配
select max(age)as ‘max’ from students; #实际上没有办法用max去查询对应的数据
select * from students where age=100;
select min(age) from students;
select min(age) from students where gender=2;
select max(age) from students where gender=1;
select max(age) from students where gender=2;
select sum(age) from students;
select sum(age)/count(age) from students;
select sum(age)/count(age) as ‘average‘ from students;
select round(sum(age)/count(age),2) as ‘average‘ from students;
select round(avg(high),2) as ‘avg‘ from students; #使用avg自动剔除了空项
select sum(high) from students;
--分组
select distinct gender from students; #查询不重复的
select gender from students gourp by gender;
select gender,count(*) from students group by gender; #统计各组性别的人数。
select gender,group_concat(name) from students group by gender;
select gender,group_concat(name,age) from students group by gender;
select gender,group_concat(name,‘|‘,age,‘|‘,high) from students group by gender;
select gender,group_concat(name,‘|‘,age,‘|‘,high) from students where gender=1 group by gender;
select gender,group_concat(name,‘|‘,age,‘|‘,high) from students where gender=2 group by gender;
select gender,group_concat(name) from students group by gender having avg(age)>=40;
select gender,avg(age) from students group by gender having avg(age)>=50;
select gender,avg(age) from students group by gender having avg(age)>=10; #这里的having使用where不符合语法规则。
select gender ,group_concat(name) from students group by gender having count(*)>3;
--分页显示
select * from students limit 2;
select * from students limit 0,2;
select * from students limit 2,2; #limit 2 与 limit 2,2 区别在于分页
select * from students limit 4,2;
select * from students limit 6,2; #从6行之后的两行
select * from students where gender=2 order by high desc limit 0,1;
select * from students where gender=2 order by high desc limit 2,1;
select * from students where gender=2 order by high desc limit 3,1;
select * from students where high is null;
update students set high=null where id=1;