03 表数据基本操作

本文详细介绍了SQL中的插入(insert)、查询(select)、更新(update)、删除(delete)以及对表字段的操作(alter),包括时间类型数据处理和SQL语句实例,展示了如何在数据库中管理书籍和学生信息表
摘要由CSDN通过智能技术生成

插入(insert)

insert into 表名 values(值1,值2...),(值1,值2...),...;
insert into 表名 (字段1,...) values (值1,值2...),...;

insert into class values
(1,"Lily",18,'f',89),
(2,"Lucy",18,'f',76),
(3,"Tom",17,'m',83);

insert into class
(name,age,sex,score)
values
("Levi",18,'m',86),
("Sunny",17,'m',91),
("Eva",17,'f',71);

insert into hobby
(name,hobby,level,price,remark)
values
("Joy","sing,dance","A",56800,"骨骼惊奇"),
("Abby","sing","B",14800.888,"天籁之音"),
("Barom","draw","B",26800.00,"当代达芬奇");

insert into hobby
(name,hobby,level,price)
values
("Jame","dance","C",8800),
("Emma","draw,sing","B",44800),
("Chen","sing","C",16800);

创建一个数据库  exercise

创建一个数据表  books  类型和约束自己设计 ,字段 : id  书名   作者   出版社  价格   备注

向其中插入数据若干  >= 6条
参考 : 老舍  沈从文  鲁迅  冰心  ....
出版社 : 中国文学  人民教育   机械工业
价格 :  30-120


创建一个数据库  books
create database books charset=utf8;
use books;

创建一个数据表  books  类型和约束自己设计 ,字段 : id  书名   作者   出版社  价格   备注
create table books(
id int primary key auto_increment,
bname varchar(50) not null,
author  varchar(30) default "佚名",
press varchar(128),
price float unsigned,
comment text
);


向其中插入数据若干  >= 6条
参考 : 老舍  沈从文  鲁迅  冰心  ....
出版社 : 中国文学  人民教育   机械工业
价格 :  30-120

insert into books
(bname,author,press,price,comment)
values
("边城","沈从文","机械工业出版社",36,"小城故事多"),
("骆驼祥子","老舍","机械工业出版社",43,"你是祥子么?"),
("茶馆","老舍","中国文学出版社",55,"老北京"),
("呐喊","鲁迅","人民教育出版社",71,"最后的声音"),
("朝花夕拾","鲁迅","中国文学出版社",53,"好时光"),
("围城","钱钟书","中国文学出版社",44,"你心中的围城是什么");

insert into books
(bname,author,press,price)
values
("林家铺子","茅盾","机械工业出版社",51),
("子夜","茅盾","人民教育出版社",47);

查询(select)

select * from 表名 [where 条件];
select 字段1,字段2 from 表名 [where 条件];

e.g. 
select * from class;
select name,age from class;

where子句

where子句在sql语句中扮演了重要角色,主要通过一定的运算条件进行数据的筛选,在查询,删除,修改中都有使用。

  • 算数运算符

suanshu

e.g.
select * from class where age % 2 = 0;
select * from class where age div 2=9;

-- 区分大小写
select * from class where binary name="lily";

  • 比较运算符

bijiao

e.g.
select * from class where age > 8;
select * from class where age between 8 and 10;
select * from class where age in (8,9);
select * from class where sex is null;
  • 逻辑运算符

luoji

e.g.
select * from class where sex='m' and age>9;

yunsuanfu

查询

1. 查找30多元的图书
2.查找人民教育出版社出版的图书 
3.查找老舍写的,中国文学出版社出版的图书 
4.查找备注不为空的图书
5.查找价格超过60元的图书,只看书名和价格
6.查找鲁迅写的或者茅盾写的图书


1. 查找30多元的图书
select * from books
where price between 30 and 39.99;

2.查找人民教育出版社出版的图书 
select * from books where press="人民教育出版社";

3.查找老舍写的,中国文学出版社出版的图书 
select * from books
where author="老舍" and press="中国文学出版社";

4.查找备注不为空的图书
select * from books where comment is not null;

5.查找价格超过60元的图书,只看书名和价格
select bname,price from books where price>60;

6.查找鲁迅写的或者茅盾写的图书
select * from books where author in ("鲁迅","茅盾");

更新表记录(update)

update 表名 set 字段1=值1,字段2=值2,... where 条件;

注意:update语句后如果不加where条件,所有记录全部更新

e.g.
update class set age=18,score=91 where name="Abby";
update class set sex='m' where sex is null;
update class set age=age+1;

删除表记录(delete)

delete from 表名 where 条件;

注意:delete语句后如果不加where条件,所有记录全部清空

e.g.
delete from class where score=0 and sex='m';

表字段的操作(alter)

语法 :alter table 表名 执行动作;

* 添加字段(add)
    alter table 表名 add 字段名 数据类型;
    alter table 表名 add 字段名 数据类型 first;
    alter table 表名 add 字段名 数据类型 after 字段名;
* 删除字段(drop)
    alter table 表名 drop 字段名;
* 修改数据类型(modify)
    alter table 表名 modify 字段名 新数据类型;
* 修改字段名(change)
    alter table 表名 change 旧字段名 新字段名 新数据类型;

e.g. 
--增加字段
alter table hobby add phone char(10) after price;

--删除字段
alter table hobby drop level;

--修改字段数据类型
alter table hobby modify phone char(16);

--修改字段名
alter table hobby change phone tel char(16);

时间类型数据

  • 日期 : DATE
  • 日期时间: DATETIME,TIMESTAMP
  • 时间: TIME
  • 年份 :YEAR

shijian

  • 时间格式

    date"YYYY-MM-DD"
    time"HH:MM:SS"
    datetime"YYYY-MM-DD HH:MM:SS"
    timestamp"YYYY-MM-DD HH:MM:SS"
    
    
e.g.
create table marathon (
id int primary key auto_increment,
athlete varchar(32),
birthday date,
r_time datetime comment "报名时间",
performance time
);

insert into marathon values
(1,"曹操","1998-2-16","2021/5/6 10:10:27","2:38:49"),
(2,"关羽","2000-7-19","2021/4/30 16:22:09","2:27:18"),
(3,"孙策","1995-10-23","2021/5/2 20:1:2","2:44:00");

  • 日期时间函数

    • now() 返回服务器当前日期时间,格式对应datetime类型
  • 时间操作

    时间类型数据可以进行比较和排序等操作,在写时间字符串时尽量按照标准格式书写。

  select * from marathon where birthday>='2000-01-01';
  select * from marathon where birthday>="2000-07-01" and performance<="2:30:00";

使用book表
1. 将呐喊的价格修改为452. 增加一个字段出版时间 类型为 date 放在价格后面
3. 修改所有老舍的作品出版时间为 2018-10-1
4. 修改所有中国文学出版社出版的但是不是老舍的作品出版时间为 2020-1-1
5. 修改所有出版时间为Null的图书 出版时间为 2019-10-1
6. 所有鲁迅的图书价格增加57. 删除所有价格超过70元或者不到40元的图书


1.将呐喊的价格修改为45update books set price=45 where bname="呐喊";

2.增加一个字段出版时间 类型为 date 放在价格后面
alter table books
add p_time date comment "出版时间" after price;

3.修改所有老舍的作品出版时间为 2018-10-1
update books set p_time="2018-10-1"
where author="老舍";

4.修改所有中国文学出版社出版的但是不是老舍的作品
出版时间为 2020-1-1
update books set p_time="2020-1-1"
where press="中国文学出版社" and author!="老舍";

5.修改所有出版时间为Null的图书出版时间为2019-10-1
update books set p_time="2019-10-1"
where p_time is null;

6.所有鲁迅的图书价格增加5update books set price=price+5
where author="鲁迅";

7.删除所有价格超过70元或者不到40元的图书
delete from books where price not between 40 and 70;
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简 洁 冬冬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值