# 基本操作

# 数据表管理

# 创建表(create table

create table class (
    id int primary key AUTO_INCREMENT,
    cname varchar(30) NOT NULL,
    description varchar(100) default NULL
)
1
2
3
4
5

以上语句创建表 class 的字段说明如下:

  • 字段 id 为主键自增
  • 字段 cname 为字符串类型 varchar 并不允许为 null
  • 字段 description 为可为 null 字符串

# 查看创建的表

desc class
1

# 添加数据

INSERT INTO class (cname,description) VALUES('PHP','木子李的松');
INSERT INTO class (cname) VALUES('Mysql');
1
2

# 复制表数据

根据已经存在的表结构创建新表

create table copy_class like class;
1

复制其他表的全部数据

insert into copy_class select * from class;
1

只复制指定字段

insert into copy_class (cname) select cname from class;
1

复制表时同时复制数据

create table copy_class select * from class;
1

删除数据表

DROP TABLE IF EXISTS copy_class;
1

# 查询操作(select...from...

# 创建表

-- 创建
CREATE TABLE stu (
    id int PRIMARY KEY AUTO_INCREMENT,
    sname char(30),
    class_id int default null,
    age smallint default null
)

-- 插入数据
INSERT INTO stu(sname,class_id,age) VALUES('小明',1,20),('张三',2,32),('李四',3,null),('小刘',null,46);
1
2
3
4
5
6
7
8
9
10

# 查询所有(*

查询所有字段数据

select * from stu;
1

查询指定字段数据

select sname,age from stu;
1

# 条件查询(where

根据条件查询

select * from stu where sname = '张三'
1

根据关键字查询

select * from stu where sname like '%小%'
1

合并列返回查询结果

select CONCAT(sname,age) as 'class_info' FROM stu;
1

多条件查询(and)

select * from stu where id > 1 and sname = '李四'
1

多条件查询(or)

select * from stu where class_id = 1 or sname like '%张%'
1

查询不包含的(not)

select * from stu WHERE sname NOT LIKE '%小%';
1

查询在……之间(between ... and),前面加not则相反。

select * from stu where age NOT BETWEEN 30 and 35;
1

查询包含内的所有值(in)

select * FROM stu where class_id IN(2,3);
相当于
select * FROM stu where class_id = 2 or class_id = 3;
1
2
3

查询空字段 NULL(is)

-- 查询没有分配班级的学生姓名
select sname from stu where class_id is null;

-- 查询已经分配班级的学生信息
select * from stu where class_id is not null;

-- 查询结果中对没分配班级的学生显示未分配,通过as重命名
select sname,if(class_id is null,'未分配',class_id) as cmm from stu;
-- 也可以使用IFNULl 函数简化操作
select sname,ifnull (class_id ,'未分配') as cmm from stu;
1
2
3
4
5
6
7
8
9
10

# 查询并排序(order by

按学生年龄从大到小(desc)排序

select * FROM stu order by age desc;
1

班级从大到小(desc)排序,相同班级的同学年龄从小到大(asc)排序

select * from stu order by class_id DESC,age ASC;
1

随机rand()获取一名同学

select * from stu order by rand() limit 1;
1

最后报名的同学

select * from stu order by id desc limit 1
1

每二和第三报名的同学

-- Limit 是从零开始的
select * from stu order by id ASC limit 1,2;
1
2

查找 2 班年龄最小的同学,使用子查询是为了防止有年龄相同的情况

select * from stu
where age = (SELECT age from stu where class_id =2 and age is not null order by age asc limit 1)
1
2

# 更新操作(update...set...)

将班级为 2 的学生改为班级 3

update stu set class_id = 3 where class_id = 2
1

3 班年龄小于 40 岁的同学年龄设置为 NULL

update stu set age = null where class_id = 3 and age < 40
1

# 删除操作(delete from ...)

删除所有年龄小于 25 的同学

delete from stu where age < 25
1

删除班级为null的字段

delete from stu where class_id is null
1

# 添加操作(insert into...)

添加一条记录

insert into stu set sname = '小明', age = 22, class_id = 1;insert into stu (sname,class_id,age) values ('小王',4,32)
1
2
3

添加多条记录

insert into stu (sname,class_id,age) values ('小明',2,32),('小张',3,45)
1