Oralce笔记:更新语句(Insert,Update,delete,alter)

数据库中数据的变化主要就是增(Insert)删(delete)改(update)。

Insert语句:

insert既可以插入单条数据,也可以通过子查询插入多条数据。

单条插入:

insert into 表名(列名1,列名2,列名3,列名4......) values (值1,值2,值3,值4....);
//插入字符串时值要加'';
insert into 表名 values(值1,值2,值3,值4);
//如果insert语句插入的值对应表中所有的列,则可将列名省略。

批量插入:

表A结构为student_Id,student_Name;

向表A中插入表B中student_Id<=10的语句:

insert into A(student_Id,student_Name)  select student_Id,student_Name from B where student_Id<=10; 
//此处没有Values关键字

Update语句:

update是更新语句。

更新单列:

update 表名 set 列名 = 某值 where 条件;

更新多列:

update 表名 set 列名1 = 值,列名2 = 值,列名3 = 值.....;
//更新时,还可对值进行操作。
//比如set student_Id = student_Id +1,student_Name = upper(student_Name);


Delete语句:

基本语句:

delete from 表名 where 条件语句;

delete和truncate table的异同:

两者都为删除语句,delete和update,insert都属于DML——数据操作语言,所作的操作可以回滚。但是truncate table属于DDL——数据定义语言,所作操作不会进行回滚,但是速度比delete快。而且truncate table没有限制条件,直接删除表中所有数据。

truncate table 表名;

Alter语句:

alter table 表名 add column 字段 类型;增加字段
alter table 表名 drop column 字段; 删除字段
alter table 表名 add check(name <> ''); 增加约束
alter table 表名 add constraint some_name UNIQUE (product_no);增加约束
alter table 表名 add foreign key (子表字段) peferences 主表(主表字段) on delete casecade;增加约束
alter table 表名 drop constraint some_name;删除约束
alter table 表名 alter column 字段 set DEFAULT 值; 改字段默认值
alter table 表名 alter column 字段 drop DEFAULT; 去掉默认值
alter table 表名 alter column 字段 类型;改字段类型
alter table 表名 reName column 旧字段 TO 新字段;改字段名
alter table 表名 reName to items; 改表名



猜你喜欢

转载自javawebxy.iteye.com/blog/2002956