What Mysql The views are? what's the effect?

Mysql in view, in fact, is a virtual table, dynamic query to retrieve data use, how to understand this concept? We first create the underlying table, the statement is as follows:

DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `arts_name` varchar(20) DEFAULT NULL,
  `user_name` varchar(20) DEFAULT NULL,
  `user_pwd` varchar(20) DEFAULT NULL,
  `rank_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK1gy0qoa8oxhd6lv5j41ubn0if` (`rank_id`),
  CONSTRAINT `FK1gy0qoa8oxhd6lv5j41ubn0if` FOREIGN KEY (`rank_id`) REFERENCES `user_rank` (`rank_id`)
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user_info
-- ----------------------------
INSERT INTO `user_info` VALUES ('1', '扫地僧', 'admin', '000000', '6');
INSERT INTO `user_info` VALUES ('2', '降龙十八掌', '萧峰', '111111', '5');
INSERT INTO `user_info` VALUES ('3', '六脉神剑', '段誉', '111111', '5');
INSERT INTO `user_info` VALUES ('4', '小无相功', '虚竹', '222222', '5');
INSERT INTO `user_info` VALUES ('5', '易筋经', '游坦之', '222222', '3');
INSERT INTO `user_info` VALUES ('6', '斗转星移', '慕容复', '1111', '3');
INSERT INTO `user_info` VALUES ('7', '化功大法', '丁春秋', '11111', '3');
INSERT INTO `user_info` VALUES ('8', '天长地久不老长春功', '天山童姥', '222', '4');
INSERT INTO `user_info` VALUES ('9', '天山六阳掌', '无崖子', '333', '4');
INSERT INTO `user_info` VALUES ('11', '小无相功', '李秋水', '555', '3');
INSERT INTO `user_info` VALUES ('12', '颜值', '王语嫣', '2222222', '1');
INSERT INTO `user_info` VALUES ('13', '毒', '阿紫', '111111', '1');
INSERT INTO `user_info` VALUES ('14', '易容术', '阿朱', '111111', '2');
INSERT INTO `user_info` VALUES ('15', '一阳指', '段正淳', '111111', '2');
INSERT INTO `user_info` VALUES ('16', '鳄嘴剪', '南海鳄神', '111111', '2');
INSERT INTO `user_info` VALUES ('17', '轻功', '云中鹤', '111111', '1');
INSERT INTO `user_info` VALUES ('20', '嘴炮', '钟万仇', '666666', '1');
DROP TABLE IF EXISTS `user_rank`;
CREATE TABLE `user_rank` (
  `rank_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_rank_name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`rank_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user_rank
-- ----------------------------
INSERT INTO `user_rank` VALUES ('1', '青铜');
INSERT INTO `user_rank` VALUES ('2', '白银');
INSERT INTO `user_rank` VALUES ('3', '黄金');
INSERT INTO `user_rank` VALUES ('4', '铂金');
INSERT INTO `user_rank` VALUES ('5', '钻石');
INSERT INTO `user_rank` VALUES ('6', '王者');

Built form is completed, we can build a view to establish Mysql in view of the statement is as follows (plus create pre-select statement view view name as) to:

CREATE VIEW view_name
AS
SELECT * FROM table_name ;

>>>>>示例<<<<<
CREATE VIEW userView
AS
SELECT * FROM user_info ; //创建视图

SELECT * FROM userView; //查询视图

UPDATE userView SET user_pwd='admin' WHERE user_name='admin'; //使用视图更新底层数据

If you look at the above view, that view seems to be useless, then, we see such a Liezi:

CREATE VIEW userView2
AS
SELECT * FROM user_info WHERE id<10

SELECT * FROM userView2;

INSERT INTO `user_info` VALUES ('22', '梅剑', '333', 'new', '1');

Create a view of the data show only 1-10, insert a row into the underlying table user_info, the userView2 is not visible, the actual application may be used WITH CHECK OPTION when creating or editing the view (to ensure a consistent view of the sexual), using WITH CHECK OPTION, the user can display the view seen by or updated data, such as:

Id password to view updates through 11 is 888, but in fact update is unsuccessful because userView3 restrictions id <10. So, you view what use is it?

1) simplification, data WYSIWYG (association table, such as 5, but only needs to query every one field in Table 1-2, to establish the most suitable view)

2) implement access control, more security (users can query or modify data Where restrictions, it can covert the data structure of the real tables)

Speaking of advantages, disadvantages inevitably naturally, a disadvantage of view as follows:

1) relatively poor performance (if the view is to create a different view, based on the query will be very slow)

2) Modify the inconvenience

To create updatable view, SELECT statement defining the view does not contain any of the following elements:
➢ aggregate functions;
➢distinct clause;
➢group by clause;
➢having clause;
➢union and union all clauses;
➢ external connection
NOTE: not recommended update operations to create a multi-table view based.

>>>>>查看视图<<<<<
SHOW TABLE STATUS [LIKE 'view_name%'] ; (查看基本信息)

DESC view_name ; 或者 DESCRIBE view_name; (查看基本信息)

SHOW CREATE VIEW view_name ; (查看详细信息)

SELECT * FROM information_schema.views; (查看数据库中所有视图详细信息)

>>>>>删除视图<<<<<
DROP VIEW IF EXISTS view_name ;

 

Published 224 original articles · won praise 34 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_39309402/article/details/105233917