JAVA开发常用数据库技术——DML(数据操作语言)

-- insert 插入、update 更新、delete 删除

-- 给 tb_user 表插入数据
insert into tb_user(name, age, sex)
values ('Jss', 18, '男');
-- 如果没有指定列名,则默认是全部的
insert into tb_user
values ('翠花', 17, '女');
-- 如果只是指定部分值的话,必须要指定对应的列名
insert into tb_user(name, sex)
values ('春花', '女');

commit

-- 如果我们在当前用户下提交数据的时候,只能当前用户访问的到
-- 其他用户如果也想访问的话,需要我们进行“提交”
-- 如果需要提交的话,有两种做法
-- 1)点击工具栏上面的绿色提交按钮
-- 2)在插入语句后面,写上 commit 即可进行提交。
-- 如果没有提交,数据相当于是在缓存中,并没有保存到物理文件中。

-- Oracle 中是不会自动提交的,需要开发者手动操作。
-- MySQL 是自动提交的。


-- 更新数据
-- 更改年龄
update tb_user 
set age = 20 
where name = 'Jss';

-- 更改两列信息
update tb_user
set name = '小翠花', sex = '男'
where name = '翠花';

-- 修改所有男生的年龄为 18
update tb_user
set age = 18
where sex = '男';

-- 修改所有男生和春花的年龄改为 20
update tb_user
set age = 20
where sex = '男' and name = '春花';

-- and 同时满足两个条件
-- or 只要满足其中一个条件即可
update tb_user
set age = 20
where sex = '男' or name = '春花';

-- 删除数据
-- 删除表中的所有数据
delete from tb_user;

-- 删除 Jss 数据
delete from tb_user where name = 'Jss';

-- 删除 小翠花 的年龄
delete tb_user set age = null where name = '小翠花';

-- 删除男的春花
delete from tb_user where name = '春花' and sex = '男';

-- 删除女的春花
delete from tb_user where name = '春花' and sex = '女';

-- 思考:如果我要删掉第二条女的春花,怎么办?

-- 先给表添加一个 id 列
alter table tb_user add(id int);

-- alter 是用来修改表的结构,如果要修改表中的数据,需要使用 update 
 

猜你喜欢

转载自blog.csdn.net/qq_41991836/article/details/82226286
今日推荐