1. 程式人生 > >mysql常見操作記錄1

mysql常見操作記錄1

mysql常見操作記錄1

文章目錄

環境

Linux version 3.10.0-862.el7.x86_64

mysql Ver 14.14 Distrib 5.7.24, for linux-glibc2.12 (x86_64)

navicat mysql

1.配置

1.1mysql下載及安裝

下載參考:

https://dev.mysql.com/downloads/mysql/5.7.html#downloads

安裝參考:

https://www.cnblogs.com/fangts/p/8994851.html

表、欄位設計規範參考:

https://www.cnblogs.com/mjbrian/p/6841226.html

mysql常見配置:

https://www.cnblogs.com/zhshto/p/6653424.html

1.2mysql常見操作命令

1)建表語句:在文章末尾

2)簡單操作,增刪查改單表

#SELECT * FROM cas_user where user_name="admin"
#INSERT INTO cas_user (user_name,pass_word)values("test1","123456"),("test2","123456");
#DELETE FROM cas_user WHERE user_name="test1";
#UPDATE cas_user SET user_name="updateName" where id=5;

3)複雜操作:多表操作

關聯表的增、刪、改一般利用事物控制,關聯表的查詢根據查詢條件不同,查詢語句也不同。

#內連線
#SELECT * FROM sys_user u INNER JOIN sys_user_role ur ON u.user_id=ur.user_id where u.user_id=238
#上面的內連線相當於where關聯查詢
#SELECT u.*,ur.id,ur.role_id FROM sys_user u ,sys_user_role ur WHERE u.user_id=ur.user_id 
#左連線
#SELECT u.* ,ur.id,ur.role_id FROM sys_user u LEFT JOIN sys_user_role ur ON u.user_id=ur.user_id
#右連線
#SELECT u.user_id ,ur.* FROM sys_user u RIGHT JOIN sys_user_role ur ON u.user_id=ur.user_id

4)使用者-角色-許可權表查詢

####1、查詢使用者的角色及許可權資訊-- 
########連線三個資料表的用法:FROM (表1 INNER JOIN 表2 ON 表1.欄位號=表2.欄位號) INNER JOIN 表3 ON 表1.欄位號=表3.欄位號
-- 
--  SELECT u.*,r.*,p.* FROM sys_user u inner join sys_user_role ur on ur.user_id=u.user_id 
--  inner join sys_role r on r.role_id=ur.role_id INNER JOIN sys_role_permission rp on rp.role_id=r.role_id
--  INNER JOIN sys_permission p on p.perm_id=rp.perm_id WHERE user_name='admin';

####2、查詢某使用者的角色
-- SELECT u.*,r.* FROM sys_user u,sys_role r,sys_user_role ur WHERE u.user_name='admin' AND u.user_id=ur.user_id AND ur.role_id=r.role_id;

####3、查詢某使用者擁有的許可權
-- SELECT p.* FROM sys_permission p,sys_role_permission rp,sys_role r WHERE r.role_id=rp.role_id AND rp.perm_id=p.perm_id AND  r.role_id
-- IN (SELECT r.role_id FROM sys_user u,sys_role r,sys_user_role ur WHERE u.user_name ='admin' AND u.user_id=ur.user_id AND ur.role_id=r.role_id);
####4、查詢擁有某角色的使用者資訊
 #SELECT u.* FROM sys_user u,sys_role r,sys_user_role ur WHERE r.role_id=1 AND  r.role_id=ur.role_id AND ur.user_id=u.user_id;
####5、查詢擁有某許可權的角色
--  SELECT p.`perm_name`,r.`role_name` FROM sys_permission p,sys_role_permission rp,sys_role r WHERE p.`perm_name`='query' AND p.perm_id=rp.perm_id AND rp.role_id=r.role_id;
####6、查詢某角色擁有的許可權
-- SELECT p.* FROM sys_permission p,sys_role_permission rp,sys_role r WHERE r.role_id=rp.role_id AND rp.perm_id=p.perm_id AND  r.role_id
-- IN (SELECT r.role_id FROM sys_user u,sys_role r,sys_user_role ur WHERE u.user_name ='user1' AND u.user_id=ur.user_id AND ur.role_id=r.role_id);

在這裡插入圖片描述

1.3mysql常見配置

參考:https://www.cnblogs.com/zhshto/p/6653424.html

http://tool.oschina.net/apidocs/apidoc?api=mysql-5.1-zh

附錄:

文中測試用到的建表:

/*
Navicat MySQL Data Transfer

Source Server         : local
Source Server Version : 80000
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 80000
File Encoding         : 65001

Date: 2018-12-18 18:36:51
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for cas_user
-- ----------------------------
DROP TABLE IF EXISTS `cas_user`;
CREATE TABLE `cas_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(50) DEFAULT NULL,
  `pass_word` varchar(21000) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_name` (`user_name`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

-- ----------------------------
-- Records of cas_user
-- ----------------------------
INSERT INTO `cas_user` VALUES ('1', 'admin', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `cas_user` VALUES ('2', 'user1', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `cas_user` VALUES ('3', 'user2', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `cas_user` VALUES ('5', 'updateName', '123456');

-- ----------------------------
-- Table structure for sys_permission
-- ----------------------------
DROP TABLE IF EXISTS `sys_permission`;
CREATE TABLE `sys_permission` (
  `perm_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '許可權id',
  `perm_name` varchar(20) NOT NULL COMMENT '許可權名',
  PRIMARY KEY (`perm_id`),
  UNIQUE KEY `perm_name` (`perm_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='許可權表';

-- ----------------------------
-- Records of sys_permission
-- ----------------------------
INSERT INTO `sys_permission` VALUES ('1', 'add');
INSERT INTO `sys_permission` VALUES ('2', 'delete');
INSERT INTO `sys_permission` VALUES ('4', 'query');
INSERT INTO `sys_permission` VALUES ('3', 'update');

-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
  `role_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色id',
  `role_name` varchar(255) NOT NULL COMMENT '角色名',
  PRIMARY KEY (`role_id`),
  UNIQUE KEY `role_name` (`role_name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='角色表';

-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES ('1', 'admin');
INSERT INTO `sys_role` VALUES ('2', 'role1');
INSERT INTO `sys_role` VALUES ('3', 'role2');

-- ----------------------------
-- Table structure for sys_role_permission
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_permission`;
CREATE TABLE `sys_role_permission` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '角色-許可權關聯表的id',
  `role_id` int(11) NOT NULL COMMENT '角色id',
  `perm_id` int(11) NOT NULL COMMENT '許可權id',
  PRIMARY KEY (`id`),
  KEY `perm_id` (`perm_id`) USING BTREE,
  KEY `role_id` (`role_id`) USING BTREE,
  CONSTRAINT `sys_role_permission_perm_id` FOREIGN KEY (`perm_id`) REFERENCES `sys_permission` (`perm_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `sys_role_permission_role_id` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`role_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='角色-許可權表';

-- ----------------------------
-- Records of sys_role_permission
-- ----------------------------
INSERT INTO `sys_role_permission` VALUES ('1', '1', '1');
INSERT INTO `sys_role_permission` VALUES ('2', '1', '2');
INSERT INTO `sys_role_permission` VALUES ('3', '1', '3');
INSERT INTO `sys_role_permission` VALUES ('4', '1', '4');
INSERT INTO `sys_role_permission` VALUES ('5', '2', '4');
INSERT INTO `sys_role_permission` VALUES ('6', '3', '4');

-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
  `user_id` int(40) NOT NULL AUTO_INCREMENT COMMENT '使用者id',
  `user_name` varchar(255) NOT NULL COMMENT '使用者名稱',
  `real_name` varchar(255) DEFAULT NULL COMMENT '真實名',
  `pass_word` varchar(255) NOT NULL COMMENT '使用者密碼',
  `telephone` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `photo` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `status` int(10) DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `user_name` (`user_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=242 DEFAULT CHARSET=utf8 COMMENT='使用者表';

-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES ('238', 'admin', 'user104_RealName', '038bdaf98f2037b31f1e75b5b4c9b26e', '1111111', null, '111', '2018-10-09 18:50:03', '2018-09-28 19:28:06', null);
INSERT INTO `sys_user` VALUES ('239', 'user1', 'user1_RealName', 'd6e75e048d00e4d0378d904bfdf81870', null, null, null, '2018-10-23 11:35:55', '2018-10-23 11:35:55', null);
INSERT INTO `sys_user` VALUES ('240', 'user2', 'user2_RealName', '2fec513e98ba7033049be21d92f55fa3', null, null, null, '2018-10-23 14:29:51', '2018-10-23 14:29:51', null);
INSERT INTO `sys_user` VALUES ('241', 'yhh', 'yhh', 'e22fdab996d9aca5be5ba6ae4f361255', null, null, null, '2018-11-13 22:37:08', '2018-11-13 22:37:08', null);

-- ----------------------------
-- Table structure for sys_user_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '使用者-角色關聯表id',
  `user_id` int(40) NOT NULL COMMENT '使用者id',
  `role_id` int(11) NOT NULL COMMENT '角色id',
  PRIMARY KEY (`id`),
  KEY `role_id` (`role_id`) USING BTREE,
  KEY `user_id` (`user_id`) USING BTREE,
  CONSTRAINT `sys_user_role_role_id` FOREIGN KEY (`role_id`) REFERENCES `sys_role` (`role_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `sys_user_role_user_id` FOREIGN KEY (`user_id`) REFERENCES `sys_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='使用者-角色表';

-- ----------------------------
-- Records of sys_user_role
-- ----------------------------
INSERT INTO `sys_user_role` VALUES ('6', '238', '1');
INSERT INTO `sys_user_role` VALUES ('7', '238', '2');
INSERT INTO `sys_user_role` VALUES ('8', '238', '3');
INSERT INTO `sys_user_role` VALUES ('9', '239', '2');
INSERT INTO `sys_user_role` VALUES ('10', '240', '2');