Verified Commit 26dc270c authored by Kiryuu Sakuya's avatar Kiryuu Sakuya 🎵
Browse files

Add No.7

parent da699adc
# 实验
# 实验
## 名词解释
......@@ -6,20 +6,82 @@
### 示例代码
#### 1. 创建一个试图,该视图只包含上海客户信息(客户号、客户姓名、住址)
#### 1. 创建一个视图,该视图只包含上海客户信息(客户号、客户姓名、住址)
```mysql
CREATE VIEW emp_view AS SELECT cust_id, cust_name, addr FROM customer WHERE addr LIKE "上海%";
SHOW FULL COLUMNS FROM emp_view;
```
#### 2. 对视图添加一条记录数据
> 注意:分别查看 `customer` 表和该视图的结果。
#### 3. 删除视图中所有姓「王」的客户数据
在往视图中插入值的时候,其实是先向表中插入值。那么除了你插入的值外,其余字段(如未插入的字段)将自动变成 NULL,但其必须是 NOT NULL,造成了这一矛盾。
```mysql
SELECT * FROM emp_view;
SELECT * FROM customer;
INSERT INTO emp_view (
cust_id, cust_name, addr
) VALUE (
"100015", "陈水扁", "上海市黄浦区半淞园路377号"
);
SELECT * FROM emp_view;
SELECT * FROM customer;
```
#### 3. 删除视图中所有姓「袁」的客户数据
```mysql
SELECT * FROM emp_view;
SELECT * FROM customer;
# 先用 SELECT * FROM emp_view WHERE cust_name LIKE "袁%"; 查查有没有
DELETE FROM emp_view WHERE cust_name LIKE "袁%";
SELECT * FROM emp_view;
SELECT * FROM customer;
```
> 但这是行不通的,需要先删除这名用户的其它数据,再删除。
#### 4. 通过视图修改表内某一客户姓名
```mysql
SELECT * FROM emp_view;
SELECT * FROM customer;
UPDATE emp_view SET cust_name="周易梦" WHERE cust_id="100002";
SELECT * FROM emp_view;
SELECT * FROM customer;
```
#### 5. 有两个基本表 `employee` 和 `sales`,创建一个视图,该视图包含相同业务员的编号、姓名、订单号和销售总金额
#### 6. 将上述视图中订单号为 10001 的记录的销售总金额改为 60000
```mysql
CREATE VIEW another_view AS
SELECT
e.emp_no AS '编码',
e.emp_name AS '姓名',
s.order_no AS '订单号',
s.tot_amt AS '销售金额'
FROM
employee e
LEFT JOIN sales s ON s.sale_id = e.emp_no;
```
#### 6. 将上述视图中订单号为 10001 的记录的销售总金额改为 6000
```mysql
```
#### 7. 给上述视图添加一条记录数据
#### 8. 删除上述视图
\ No newline at end of file
```mysql
```
#### 8. 删除上述视图
```mysql
DROP VIEW another_view;
```
\ No newline at end of file
# 实验
# 实验
## 名词解释
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment