Database views and indexes

1. View
View (View), as a database object, provides users with a way to retrieve the data in the data table . The user browses some or all of the data of interest in the data table through the view, while the physical storage location of the data is still in the table.
A view is a virtual table that does not represent any physical data, just a window for viewing data . Views are not stored in the database in the form of a set of data. The database only stores the definition of the view, not the data corresponding to the view. These data are still stored in the basic table from which the view is derived. When the data in the base table changes, the data queried from the view also changes.
The data rows and columns in the view come from the base table and are dynamically generated when the view is referenced . Views can be used to centralize, simplify and customize the user's database display, and users can access data through the view without having to directly access the basic table of the view.
A view consists of two parts: the view name and the view definition. A view is a table derived from one or more tables. It is actually a query result. The name of the view and the query corresponding to the view are stored in the data dictionary .

Advantages of views
1. Data security.
Define different views for different users, so that users can only see the data related to themselves. The database authorization command can make each user's retrieval in the database restricted to specific database objects, but cannot be authorized to specific rows and columns of the database. With views, users can be restricted to different subsets of data.
2. Query simplification.
To build a view for a complex query, the user does not need to enter a complex query statement, but only needs to do a simple query against this view. Those frequently used queries can be defined as views, so that the user does not have to specify all the conditions each time for subsequent operations.
3. Logical data independence.
Views allow some degree of independence between application and database tables. If there is no view, the application must be built on the table. With the view, the program can be built on the view, so that the program and the database table are separated by the view.
For the operation of the view, for example, the query only depends on the definition of the view. When the basic table constituting the view needs to be modified, only the subquery part in the view definition needs to be modified, and the query based on the view does not need to be changed.

Disadvantages of views
1. Performance.
SQL Server must convert the query on the view into a query on the base table. If the view is defined by a complex multi-table query, then even a simple query on the view, SQL Server turns it into a complex union body, it will take some time.
2. Modify the restrictions.
When a user tries to modify some rows of the view, SQL Server must translate it into modifications to some rows of the base table. In fact, the same is true when inserting or deleting from a view. This is convenient for simple views, but may not be modifiable for more complex views. These views have the following characteristics:
  a. Views with set operators such as UNIQUE.
  b. A view with a GROUP BY clause.
  c. Views with aggregate functions such as AVG\SUM\MAX.
  d. Views using the DISTINCT keyword.
  e. Views that connect tables (there are some exceptions)
3. Restrictions on creating views:
When creating views, pay attention to the following constraints:
(1) Rules or DEFAULT definitions cannot be associated with views.
(2). The query that defines the view cannot contain the ORDER BY\COMPURER\COMPUTER BY clause and the INTO keyword
(3). If a column in the view is an arithmetic expression, constructor or constant, and two or more different columns in the view have the same name (this is usually because there is a connection, and these two or more columns from different tables have the same name), in this case, the user needs to specify the name of the column for each column of the view.

Create a view:
create view viewName (parameter list / can not be written, the default is the same as the following select) as select * from table name; for

example:
(1), create table viewdemo(id int primary key , name char(10), score float);
(2), create view view1 as select* from viewdemo;
(3), create view view2(name,score) as select name ,score from viewdemo;

modify the view:
alter view viewname (columns) as select columns from tableName ;

Example:
 alter view view2 (id ,name) as select id,name from viewdemo ;

Delete view:
 drop view if exists view name;

Column:
drop view view2;


2. Index
1. An index is a database object based on a table column . It saves the index column sorted in the table, and records the physical storage location of the index column in the data table , which realizes the logical ordering of the data in the table. The main purpose is to improve the performance of SQL Server system, speed up data query speed and reduce system response time. The index points to the records in the table through the key value in the record table, so that the database engine does not have to scan the entire table to locate the relevant records. Conversely, without an index, it would cause SQL Server to search all records in the table for matching results.
In addition to improving the speed of querying data in a table, indexes can also speed up the connection between tables. For example, when implementing data referential integrity, the foreign key of a table can be used as an index, which will speed up the connection between tables.

The index can be understood as the directory of a book, which records the location where the data is stored in the database, just like the directory of a book, which records the location of a certain page in the book. Likewise, an index is a single, on-disk database structure that holds reference pointers to all the records in the database table.


2. The benefits of indexes
  Appropriate use of indexes can improve database query speed!

3. Example:
Create an index when creating a table
CREATE TABLE table name [column name data type] [ UNIQUE | FULLTEXT ] [ INDEX | KEY ] [ index name ] ( column name [ length ] ) [ ASC | DESC ]
Description: UNIQUE 、 FULLTEXT are optional parameters, representing unique index and full-text index respectively; INDEX and KEY are synonyms, and they have the same function and are used to specify the index;

(1), ordinary index (index): ordinary index is the basic index type of MySQL , allowing the insertion of duplicate and null values ​​in the column defining the index
Example:
CREATE TABLE book
(
bookid INT NOT NULL,
bookname VARCHAR(100) NOT NULL,
authors VARCHAR(100) NOT NULL,
info VARCHAR(500) NULL,
year_publication YEAR NOT NULL,
INDEX(year_publication)
);

(2), Unique index (unique): The value of a unique index column must be unique, but null values ​​are allowed. A primary key index is a special kind of unique index that does not allow nulls.
Example:
CREATE TABLE book
(
  id INT NOT NULL,
  name CHAR(50) NOT NULL,
  UNIQUE INDEX UniqueIdx(id)
);

(3) Combined index: Combined index is to create an index on multiple columns. When querying, the index will only be used if the leftmost field of these fields (which columns are specified when creating the composite index) are used in the query conditions.
CREATE TABLE student
(
  id INT NOT NULL,
  name CHAR(50) NOT NULL,
  age INT NOT NULL,
  info VARCHAR(200),
  INDEX MultiIdx(id,name,age)

);


(4) Full-text index: MySQL only supports the FULLTEXT index in the MyISAM storage engine, and the class types are CHAR, TEXT, VARCHAR. And the storage engine of the specified table needs to be MyISAM.
Example:
CREATE TABLE t4
(
  id INT NOT NULL,
  name CHAR(50) NOT NULL,
  age INT NOT NULL,
  info VARCHAR(200),
  FULLTEXT INDEX FullindexName(info)

) ENGINE = MyISAM ;


Create an index on an existing table:
Syntax:
ALTER TABLE table_name ADD [ UNIQUE | FULLTEXT ] [ INDEX | KEY ] 
[ inex_name ] (col_name [ length ] ,...) [ASC | DESC ]


(1), ordinary index: ALTER TABLE book ADD INDEX indexName( bookname(30) );
(2), unique index: ALTER TABLE book ADD UNIQUE INDEX UniqueIdx( bookid );
(3), combined index: ALTER TABLE book ADD INDEX BkAndInfoIdx( authors(20), info(50) );
(4), full-text index: ALTER TABLE t6 ADD FULLTEXT INDEX infiIdx(info); (provided that the storage engine of this table is MyISAM)

Use create index to create an index:
Syntax:
CREATE [ UNIQUE | FULLTEXT ] [ INDEX | KEY ] INDEX index_name
ON table_name ( col_name[ length ] , ... ) [ASC | DESC ]

Example: Create an index named BkNameIdx on the bookname field of the table book.
CREATE INDEX BkNameIdx ON book (bookname);
Example: Create a unique index on the bookId field of the book table.
CREATE UNIQUE INDEX UniqueIdx ON book (bookId);

4. Delete index:
(1), ALTER TABLE table_name DROP INDEX index_name;
(2), DROP INDEX index_name ON table_name ;


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325471843&siteId=291194637