当前位置:网站首页>Simple creation of database views, indexes, stored procedures and triggers

Simple creation of database views, indexes, stored procedures and triggers

2022-06-10 09:07:00 spring℡

Catalog

1、 View

1.1 Create view

1.2 Modify the view

1.3 Create complex views

1.4 Delete view

#​ Example view

2、 Indexes

2.1 Create index directly

2.2 Create index with table

# Index example

3、 stored procedure

# Stored procedure example

4、 trigger

# Trigger example


1、 View

1.1 Create view

create view  View name  as  Query statement 

eg:
mysql> create view v1 as select cs_id,cs_name,cs_credit from course where cs_type=' Professional basis ';
Query OK, 0 rows affected (0.01 sec)

mysql> select * from v1;
+----------+--------------+-----------+
| cs_id    | cs_name      | cs_credit |
+----------+--------------+-----------+
|  5223013 | College Physics     |         4 |
|  5237514 | c Language        |         4 |
| 20201833 | probability theory       |         3 |
| 20202336 | Advanced mathematics     |         6 |
+----------+--------------+---ming--------+
4 rows in set (0.00 sec)

1.2 Modify the view

create or replace view  View name ( attribute 1, attribute 2, attribute 3...) as  Query statement 
 perhaps 
alter view  View name ( attribute 1, attribute 2, attribute 3...) as  Query statement 
eg1:
mysql> create or replace view  v1(id,name,credit) as select cs_id,cs_name,cs_credit from course where cs_type=' Professional basis ';
Query OK, 0 rows affected (0.01 sec)
​
mysql> select * from v1;
+----------+--------------+--------+
| id       | name         | credit |
+----------+--------------+--------+
|  5223013 |  College Physics      |      4 |
|  5237514 | c Language         |      4 |
| 20201833 |  probability theory        |      3 |
| 20202336 |  Advanced mathematics      |      6 |
+----------+--------------+--------+
4 rows in set (0.00 sec)
​
eg2:
mysql> alter  view  v1(id1,name1,credit1) as select cs_id,cs_name,cs_credit from course where cs_type=' Professional basis ';
Query OK, 0 rows affected (0.01 sec)
​
mysql> select * from v1;
+----------+--------------+---------+
| id1      | name1        | credit1 |
+----------+--------------+---------+
|  5223013 |  College Physics      |       4 |
|  5237514 | c Language         |       4 |
| 20201833 |  probability theory        |       3 |
| 20202336 |  Advanced mathematics      |       6 |
+----------+--------------+---------+
4 rows in set (0.00 sec)

1.3 Create complex views

mysql> create view v2 as (select e.name,d.depName from (Employee e,department d) where e.depno=d.depno and e.sex=' male ') ;
Query OK, 0 rows affected (0.01 sec)
​
mysql> select * from v2;
+---------+-----------------+
| name    | depName         |
+---------+-----------------+
|  Wang Lin     |  Human Resources Department       |
|  Wang Fang     |  Finance Department           |
|  Zhang Xiao     |  Finance Department           |
|  Li Hua     |  The Marketing Department           |
|  Li Ming     |  The Marketing Department           |
|  Wu Tian     |  The Marketing Department           |
|  Liu bei     |  Manager's Office       |
|  zhaoyun     |  R & D department           |
+---------+-----------------+
8 rows in set (0.00 sec)

1.4 Delete view

mysql> drop view  View name ;
​
mysql> select * from VIEWS where TABLE_NAME=' View name ';

#​ Example view

(1) In the database example Create college surface .College The contents of the table are as follows

CREATE TABLE college(
number INT(10) NOT NULL UNIQUE PRIMARY KEY COMMENT ' Student number ',
name VARCHAR(20) NOT NULL COMMENT ' full name ',
major VARCHAR(20) NOT NULL COMMENT ' major ',
age INT(5) COMMENT ' Age '
);

(2) stay student Create a view on a table college_view. The fields of the view include student_num、student_name、 student_age and department.ALGORITHM Set to MERGE type , And add... To the view WITH LOCAL CHECK OPTION Conditions

mysql> create ALGORITHM=MERGE view college_view(student_num,student_name,student_age,department) as select number,name,age,major from college with local check option;
Query OK, 0 rows affected (0.01 sec)

(3) View view college_view Detailed structure of

show create view college_view \G;

(4) Update the view . Insert into view 3 Bar record . The contents of the record are shown in the following table

numer name major age
0901  Zhang San   Foreign Languages   20
0902  Li Si Computer   22
0903  Wang Wu Computer   19

mysql> insert into college_view values('0901',' Zhang San ',20,' Foreign Languages ');
Query OK, 1 row affected (0.01 sec)
mysql> insert into college_view values('0902',' Li Si ',22,' Computer ');
Query OK, 1 row affected (0.00 sec)
insert into college_view value ('0903',' Wang Wu ',19,' Computer ');
Query OK, 1 row affected (0.00 sec)

(5) Modify the view , Make it display information specialized in computer , Other conditions remain the same

mysql> alter  ALGORITHM=MERGE view college_view(student_num,student_name,student_age,department) as select number,name,age,major from college where major=' Computer ' with local check option;
Query OK, 0 rows affected (0.00 sec)

# Can also be alter Switch to create or replace

mysql> select * from college_view;
+-------------+--------------+-------------+------------+
| student_num | student_name | student_age | department |
+-------------+--------------+-------------+------------+
|         902 | Li Si         |          22 | Computer     |
|         903 | Wang Wu         |          19 | Computer     |
+-------------+--------------+-------------+------------+
2 rows in set (0.00 sec)

(6) Delete view college_view

mysql> drop view college_view;
Query OK, 0 rows affected (0.00 sec)

2、 Indexes

advantage : Can improve the speed of data retrieval , For union queries between dependent child tables and parent tables , Can improve the query speed ; When using grouping and sorting clauses for data queries , It can also significantly save the time of grouping and sorting in the query .

shortcoming : Creating and maintaining indexes takes time , The amount of time spent increases with the amount of data ; Indexes need to take up physical space , Each index takes up a certain amount of physical space ; increase 、 When deleting and modifying data , To maintain the index dynamically , The speed of data maintenance is reduced .

2.1 Create index directly

(1) General index

create table  Index name  ( attribute 1, attribute 2, attribute 3......,Index( attribute ));

mysql> create table index1 (id char(5), name char(10), sex boolean, Index(id));
Query OK, 0 rows affected (0.02 sec)

(2) Uniqueness index

mysql> create table index2 (id char(5) unique, name char(10), sex boolean, unique Index index2_id(id));
Query OK, 0 rows affected, 1 warning (0.03 sec)

(3) Full-text index

(4) Single index

(5) Multi column index

(6) Spatial index

(7) Delete index

drop table  Index name ;

2.2 Create index with table

(1) There are already tables to create indexes

create [unique/fulltext/spatial/] index  Index name  on  Table name  ( attribute 1[l type ] [asc/desc]);

Example :

mysql> create index index_Emp on Employee (num asc);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

(2) Delete index

mysql> drop index index_Emp on Employee;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

(3) alter How to create an index

mysql> alter table college add index index_college (number desc);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

# Index example

1、 In the database job Create workInfo surface . Create a table while id The field is created with the name index_id Unique index of , Moreover, with In descending order .workInfo The contents of the table are as follows

CREATE TABLE workInfo(
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
type VARCHAR(10),
address VARCHAR(50),
tel VARCHAR(20),
wage INT,
content TINYTEXT,
extra TEXT,
UNIQUE INDEX index_id(id DESC)
);

2 、 Use create index Statement for name The field creation length is 10 The index of

mysql> create index index_name on workInfo(name(10)) ;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

3 、 Use alter table Statements in type and address Created on named index_t The index of

mysql> alter table workInfo add index index_t (type,address);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

4 、 take workInfo The storage engine of the table is changed to MyISAM type

mysql> alter table workInfo engine=MyISAM;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

5 、 Use alter table Statements in extra The field is created with the name index_ext Full text index of

mysql> alter table workInfo add fulltext index index_ext (extra);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

6 、 Delete workInfo Unique index of the table index_id

mysql> drop index index_id on workInfo;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

3、 stored procedure

 Format 
​
delimiter $$
create procedure  The process of ( parameter list )
begin
    SQL sentence 
end $$

Three parameter types

IN, OUT, INOUT:

DELIMITER $$
​
USE `db`$$
​
DROP PROCEDURE IF EXISTS `p1`$$
​
CREATE DEFINER=`admin`@`%` PROCEDURE `p1`()
BEGIN
   SELECT COUNT(1) FROM mysql.user;
   END$$
​
DELIMITER ;

Through the above preparation, we can get a number of users


​mysql> call p1();
+----------+
| count(1) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

# Stored procedure example

eg: The number of boys or girls in the query table

  Writing stored procedures

DELIMITER $$
​
CREATE
   PROCEDURE `db`.`p4`(sex  ENUM(' male ',' Woman ') )
   BEGIN
    SELECT COUNT(1) FROM student WHERE sex=stu_sex;
   END$$
​
DELIMITER ;

Stored procedure calls
mysql> call p4(' male ');
+----------+
| count(1) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call p4(' Woman ');
+----------+
| count(1) |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

4、 trigger

See what triggers are show triggers

Delete trigger drop trigger

{ BEFORE | AFTER } Trigger timing

{ INSERT | UPDATE | DELETE } Triggered events

# Trigger example

Create two databases , When a database is inserted, another table is automatically inserted .


mysql> CREATE TABLE tab1(
   -> id int primary key auto_increment,
   -> name varchar(50),
   -> sex enum('m','f'),
   -> age int
   -> );
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE tab2(
   -> id int primary key auto_increment,
   -> name varchar(50),
   -> salary double(10,2)
   -> );
Query OK, 0 rows affected (0.02 sec)

Create trigger

DELIMITER $$
​
USE `db`$$
​
DROP TRIGGER /*!50032 IF EXISTS */ `tab1_after_insert_trigger`$$
​
CREATE
   /*!50017 DEFINER = 'admin'@'%' */
   TRIGGER `tab1_after_insert_trigger` AFTER INSERT ON `tab1`
   FOR EACH ROW BEGIN
    INSERT INTO tab2(NAME,salary) VALUES(new.name,6000);
   END;
$$
​
DELIMITER ;

To watch tab1 insert data
mysql> insert into tab1 value(1,'john','m',12);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tab1;
+----+------+------+------+
| id | name | sex  | age  |
+----+------+------+------+
|  1 | john | m    |   12 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql> select * from tab2;
+----+------+---------+
| id | name | salary  |
+----+------+---------+
|  1 | john | 6000.00 |
+----+------+---------+
1 row in set (0.01 sec)

原网站

版权声明
本文为[spring℡]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100833501315.html