Multiple related complex information placed in a table will lead to verbosity of the data. It can be split into several tables and then connected with the primary key foreign key. However, when there are many tables to be connected, the SQL statement for each query order information will change. It is very complicated, which leads to inefficiency and error-proneness of the query, and the view can solve such contradiction. The essence of the view is to save the search result of the select statement in the form of a table. Note: The role of the view is to simplify the sql statement, but performance may be degraded, and you need to be cautious when using multiple views.
create table product(pid int primary key,pname varchar(50),price double);
create table user(uid int primary key,zip varchar(50),address varchar(50),name varchar(50));
create table order_detail(oid int primary key,pid int,quantity int,constraint fk_pid foreign key (pid) references product(pid));
create table order_basic(oid int primary key,odate date,uid int,constraint fk_uid foreign key (uid) references user(uid));

(2) Normal connection table query mode (no view, garbled here, can be solved by setting the character set to gbk)
select ob.oid as oid,ob.odate as odate,u.uid as uid,u.zip as zip,u.address as address,u.name as name,p.pid as pid ,p.pname as pname,p.price as price,od .quantity as quantity from(((order_basic as ob inner join order_detail as od on ob.oid=od.oid)inner join product as p on od.pid=p.pid)inner join user as u on ob.uid =u.uid)where ob.oid=1\G

(3) Create an order view
create view v_order (oid,odate,uid,zip,address,name,pid,pname,price,quantity)as select ob.oid ,ob.odate,u.uid,u.zip,u.address,u.name,p.pid,p.pname,p.price,od.quantity from (((order_basic as ob inner join order_detail as od on ob.oid=od.oid)inner join product as p on od.pid=p.pid)inner join user as u on ob.uid=u.uid);
If you are replacing an existing view, you can change create to replace

(4) Delete view drop view view name

(5) Confirm the contents of the order view
Show tables;//This command will display the created view

View details with show fields
show fields from v_order ;

(6) Use the view to retrieve the order (really convenient O (∩ _ ∩) O haha ~)
select * from v_order where oid=1;

solve Chinese garbled set names gbk on cmd;
(7) Use the view to add and delete order data (for some reasons, you can't save Chinese data in cmd)
insert into v_order(pid,pname,price) values(3,’’,34);

Note: The following conditions cannot add or delete view data.



Full scan and multiple block reads The full scan operation will perform multiple block reads. That is to say, a single IO call will request multiple blocks instead of just one. The number of data bloc...
Function template background: in a double array, find the maximum number; in a float array, find the maximum number; In an Object [] array to find the largest element. is the same algorithm, iterate t...
This lesson mainly explains how to implement memory management functions. 1. Heap (heap) The heap is located in the low address space, and the address of the heap is allocated through the malloc funct...
Python study notes (eight)...
usefdopen()Can convert the file descriptor to CFILEPointer, then you can pass standard C IO functions, such asfread()、fputs()Wait to achieve efficient IO operations, but pay attention to the correspon...
1. Revisiting the upward transformation In Chapter 7, we said that an object reference can be used as its own type or as its base type. This practice of using a type reference as its base type is call...
1, comm comparison command Among them, -1 means that columns that only appear in file 1 will not be displayed, -2 means that columns that only appear in file 2 will not be displayed, and -3 means that...
HTML5 script programming Cross-document messaging Referred to as XDM. The core is the postMessage() method, which receives two parameters: a message and a string indicating which domain the message re...
network programming Label: Learn the bridge of various network protocols What is a computer network The role of computer networks: resource sharing and information transmission. The composition of the...
network programming Network communication is a communication between two processes on both computers. Network classification: LAN、WAN、WLAN LAN (LAN) WAN Wide Area Network, Wan) WiReless LAN, WLAN TCP/...