MySQL efficient programming study notes (eight) -- view

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.

  1. Create view
    CREAT VIEW view name (column name, ...) AS SELECT statement [WITH CHECK POINT];
    (1) Building a table
    Note: The table is built first with a primary key and no foreign key, otherwise it will be wrong.
    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.

  1. View column contains statistical functions
  2. Use subqueries when defining views
  3. View definition using GROUP BY/HAVING/DISTINCT/UNION
  4. Performing operations across multiple tables
    (8) Create a view using the with check option command
    This command can restrict the insertion or update of operations that do not conform to view retrieval.
    For example, select data with a unit price greater than 300 as a view. If you insert or update data smaller than 300, if you do not add with check option, the insert update succeeds (when the data is updated for later), added The statement will report an error.

    sql is not added with check option,

    After adding the check option, it will check if the sql statement satisfies the condition of creating the view price>300

Intelligent Recommendation

Oracle sql advanced programming study notes (eight)

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...

Object-oriented programming (C ++) study notes eight

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...

Programming Paradigm (Stanford University) Study Notes "Eight"

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 programming entry study notes (eight)

Python study notes (eight)...

TCP/IP network programming 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...

More Recommendation

Java programming thought study notes eight: polymorphism

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...

Linux shell programming study notes (eight) file

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...

Javascript study notes (eight)-HTML5 script programming

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...

java study notes-network programming (eight)

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...

Python study notes eight - network programming

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/...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top