oracle数据库学习笔记(九)

第九章 DML语句(表数据的增删改)

DML语句具有事务性,需要提交,可以回滚。
1)insert
insert into 表名[(字段名,字段名...)]
values (值1,值2,值3);

表名后面声明了字段名的话,代表要指定向哪几个字段插入数据

insert into 表名
values (值1,值2,值3);
未声明字段名,则默认向表中的全部字段插入数据。

create table s_student(
id number primary key,
name varchar2(20),
age number,
gender char(10)
);

将1号男生小明插入到数据库中:
insert into s_student(id,name,age,gender)
values(1,'小明',10,'男');

insert into s_student(id,name,gender)
values(1,'小明','男');

给小明添加年龄属性为10
update s_student
set age = 10
where name = '小明';


2)delete
删除表中的数据
delete [from] 表名
[where 条件...];
如果不写where条件,删除表中全部内容。

删除s_student表中的数据
delete s_student
delete from s_student

删除s_student表中id为1的学生?
delete from s_student
where id = 1;

= != > < in between
删除s_student表中学号为1、3、5、6、7、9、10的学生?
delete from s_student
where id = 1 or id = 3 or id = 5......

delete from s_student
where id in (1,3,5,6,7,9,10);

delete、drop和truncate都是删除,有什么区别?

3)update
修改表中某一个/几个字段的值
update 表名
set 字段名 = 字段值,
字段名 = 字段值,
字段名 = 字段值.......
[where 修改条件]

将s_student表中id为5的学生姓名修改为tom?
update s_student
set name = 'tom'
where id = 5;

 

序列的用法
(MySQL 数据库中Auto increment)序列自动增长
关键字:sequence
作用:生产唯一值 ,常用来生成主键的值
id主键设置自动增长,就可以序列

创建序列的语法:
create sequence 序列名 [start with 数字] [increment by 数字];
start with:用来指定开始值
increment by:指定增长值

如果从1开始,并且每次增长1
create sequence 序列名 ;
等价于 create sequence 序列名 start with 1 increment by 1;

删除序列:
drop sequence 序列名;

序列名命名规则:
表名_字段名_seq

如何使用?
查找序列的当前值?
select 序列名.currval from dual;
查找当前值不会引起序列自增长
查找序列的下一个值
select 序列名.nextval from dual;
查找下一个值会引起序列的自增长

create sequence test_seq start with 1 increment by 1;

create table s_student(
id number primary key,
name varchar2(20),
age number,
gender char(10)
);

tom 22岁 男
jack 23岁 男
lucy 24岁 女
lili 22岁 女
要求 : 使用序列产生id值
创建序列:create sequence s_stu_id start with 1 increment by 1;

插入数据:
insert into s_student values(s_stu_id.nextval,'tom',22,'男');
insert into s_student values(s_stu_id.nextval,'jack',23,'男');
insert into s_student values(s_stu_id.nextval,'lucy',24,'女');
insert into s_student values(s_stu_id.nextval,'lili',22,'女');

猜你喜欢

转载自www.cnblogs.com/DennySmith/p/12189251.html
今日推荐