ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 6,修改数据(笔记)

6,修改数据(笔记)

原创 Linux操作系统 作者:tonykorn97 时间:2007-05-04 00:00:00 0 删除 编辑

下列语句用力修改数据:
delete
insert
update


删除行
删除表的所有行
delete from customer;
delete customer;

使用truncate table删除所有行。
使用truncate table语句快速地从表中除去所有行,同时除去所有相应的索引数据。提交事务之后,不能恢复删除的行。

使用truncate table语句除去行比使用delete语句除去行速度要快,原因如下:
1,截断表不会触发表的delete触发器。
2,不会对正在截断的表的每一行进行逻辑日志记录。

使用truncate table从朝表中删除数据。
在将truncate table语句与层次结构的超表配合使用时,only关键字运行只截断超表或截断超表及其所有子表。在缺省情况下
(不使用only),截断超表及其所有子表,假设你创建了超表person,并在它下面定义了两个子表employee和sales_rep,以下
truncate table语句从person,employee和sales_rep表中删除行;
truncate table person;

要将删除仅限制于超表中的行,必须在truncate table语句中使用only关键字,例如:只删除person表中的行。
truncate table only person;

删除指定行
delete语句中的where子句的格式与select 语句中的where子句相同。可以使用它来精确指定应删除的一行或多行。
delete from customer where customer_num = 175;

删除选择的行
delete from customer where company = 'Druid cyclery'

删除包含行类型的行(IDS)
delete from employee where address.city = 'san jose';

删除包含指定类型的行(IDS)
DELETE FROM manager where 'Baker' in diect_reports;

从超级表删除行(IDS)
当删除超表的各行时,删除操作的作用域是超表及其子表。假设您创建了超表person,并在它下面定义了两个子表employee和
sales_rep,对person表执行的以下delete语句从所有表person,employee和sales_rep中删除行:
delete from person where name = 'Walker';

要将删除仅限制于超表中的行,必须在delete语句中使用only关键字。
delete from only(person) where name = 'Walker';

复杂的删除条件
delete 语句中的where子句可能会象select语句中的where子句那样复杂。它可能包含用and和or连接的多个条件,也可能包含子查询
delete from stock where 0 = (select count(*) from manufact where manufact.manu_code = stock.manu_code)

使用删除连接(xps)
delete from stock using stock,manufact where stock.manu_code != manufact.manu_code;

插入行
insert语句向表添加新行。该语句具有两个基本功能。它可以使用您提供的列值创建一个新行。或者它可以使用从其它表选择的数据
创建一组新行。

单行
insert语句最简单的形式根据列值的列表创建一个新行,并将该放放置在表中,

insert into stock values (115,'rpc','tire pump',108,'box','6/box');

将行插入到类型表中(IDS)
create row type zip_t
(
z_code char(5),
z_suffix char(4)
);

create row type address_t
(
street varchar(20),
city varchar(20),
state char(2),
zip zip_t
);

create row type employee_t
(
name varchar(30),
address address_t,
salary integer
);

create table employee of type employee_t;

插入到行类型列中(IDS)
insert into employee
values ('poole,john',
row('42 high st','willits','ca',
row(69055,1450))::address_t,35000);

为行类型指定空值
insert into student values('Brauer,howie',null,3.75)

为行类型的特定字段插入null值时,必须包含row构造函数,一下insert语句显示可以如何将空值插入到employee表的address列的特定字段中。

insert into employee
values(
'singer,john',
row(null,'Davis','ca',
row(97000,2000))::address_t,67000
);


将行插入到超级表中(IDS)
INSERT INTO person
values(
'Poole,John'
row('402 saphire st.','Elmondo','ca','69055'),
345605900
)

更新行
使用update语句来更改表的一个或多个现有行中的一个或多个列的内容。此语句有两种完全不同的形式。
一种允许您按名称将特定值指定给列;另一种允许您将一系列值(这些值可由select语句返回)指定给一系列。

选择要更新的行
任一形式的update语句都可以where子句结束。如果省略where子句,则修改所有行。在
where子句中选择要修改的精确行集可能很复杂。对where 子句的唯一限制时不能在子查询的from子句中命名更新的表。

update customer set fname = 'barnaby',lname = 'dorfler' where customer_num = 13

还可在where子句中使用子查询,假定anza公司因为安全原因需要回收网球。结果时,必须将来自制造商ANZ并包含库存号6的
所有为交货订单设定为延期交货:
update orders
set backlog = 'y'
where ship_date is null
and order_num in
(select distinct items.order_num from items
where itmes.stock_num = 6
and items.manu_code = 'ANZ');

用统一值进行更新
update stock set unit_price = unit_price * 1.5 where manu_code = 'HRO';

update items set total_price = quantity * (select max(unit_price) from stock where stock.stock_num = items.stock_num)
where items.order_num in (select order_num from orders where ship_date is null);

用选择的值进行更新
update customer set (fname,lname) = ('Barnaby','Dorfler') where customer_num = 103;

用这种方法编写语句没有任何优点。实际上,它更难于阅读,原因时将那些值指定给那些列不明显。
但是,当要指定的值来自单个select语句时,此形式就很合理。
update customer
set (address1,address2,city,state,zipcode) =
((select address1,address2,city,state,zipcode from newaddr
where newaddr.customer_num=customer.customer_num))
where customer_num in (select customer_num from newaddr);

更新行类型(IDS)
更新包含已命名行类型的行
update employee set address = Row('103 california st','San Francisco', address.state,address.zip)::address_t
where name = 'zawinul,joe'
更新包含未命名行类型的行
update student set s_address = row(13 sunset','Fresno',s_address.state,s_address.zip) where s_name = 'henry,john'
为行类型的字段指定空值
update employee set address = row(null::varchar(20),‘Davis','CA',Row(Null::Char(5),null::Char(4)))::address_t)
where name = 'henry,john'

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/312079/viewspace-245654/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-05

  • 博文量
    1854
  • 访问量
    2088363