Experiment 2 Data update and SQL simple query

Experiment 2 Data update and SQL simple query

1. Insert data

① Insert a new product in the product table.

0077, porcelain, units (pieces), 8000, 3, 1002, 10000.00

insert into 产品 VALUES
('0077','瓷器,单位(件)',8000,3,'1002',10000.00);

② Insert a new order in the order table.

666、2003-06-06、300

insert into 订单 VALUES
('666','2003-06-06','300');

③ Insert the new agent in the agent table.

05, Hisense, No. 9, Zhongshan Road, 541000, 200000.00, 60

insert into 代理商 VALUES
('05','海信','中山路9号','541000',200000.00,60);

Alternate sentence:

Find all foreign key constraints

select *
from sys.foreign_keys
where referenced_object_id=object_id('订单')
order by 1

Add foreign key constraints

alter table 从表              
add constraint 约束名 <br>   foreign key(关联字段) references 主表(关联字段)   

alter table 订货项目
add foreign key(订单编号)
references 订单(订单编号)

Remove foreign key constraints

ALTER TABLE 订货项目
DROP CONSTRAINT fk_PerOrders

2. change the data

① The percentage of updating all agents is 20%.

update 代理商 set 提成比例=20;

② Add a new field to the product table ---- out of stock.

alter table 产品 add 缺货量 smallint;

③ Update the unit price of product 0011 to 1000.

update 产品 set 价格=1000 where 产品编号='0011';

3. delete data

① Delete the customer with the number 700.

delete from 客户 where 客户编号='700'

② Delete the agent with the number 05.

delete from 代理商 where 代理商编号='05'

③ Delete all order items with order number 444.

delete from 订货项目 where 订单编号='444'

4. Create and delete [index]

In each table, indexes are established by agent number, customer number, product number and order number.

create index 代理商索引 on 代理商(代理商编号)
create index 客户索引 on 客户(客户编号)
create index 产品索引 on 产品(产品编号)
create index 订单索引 on 订单(订单编号)
CREATE INDEX 代理商编号索引
ON 代理商 (代理商编号)

drop index 代理商编号索引 on 代理商
CREATE INDEX 客户编号索引
ON 客户 (客户编号)

drop index 客户编号索引 on 客户
CREATE INDEX 产品编号索引
ON 产品 (产品编号)

drop index 产品编号索引 on 产品
CREATE INDEX 订单编号索引
ON 订单 (订单编号)

drop index 订单编号索引 on 订单

5. Single table query

① Query the product number and inventory of existing products from the product table.

select 产品编号,库存量 from 产品;

② Inquire the agent number and address of "Wang Wu" from the customer table.

select 代理商编号,地址 from 客户 where 姓名='王五';

③ Check the commission amount of the name "HP" from the agent table.

select 提成金额 from 代理商 where 姓名='惠普';

④ Check the order number and order quantity of the product ordered by the order number "444" from the order item table.

select 产品编号,订购数量 from 订货项目 where 订单编号='444';

Guess you like

Origin www.cnblogs.com/lightice/p/12692489.html
Recommended