MySQL数据库权限、表的增删改查、数据类型的相关操作
一、概述
1、什么是数据库 ?
答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库
2、什么是 MySQL、Oracle、SQLite、Access、MS SQL Server等 ?
其他类似软件:
关系型数据库:sqllite,db2,oracle,access,sql server MySQL 数据存在强类型关系
非关系型数据库:MongoDB,redis
答:他们均是一个软件,都有两个主要的功能:
- a. 将数据保存到文件或内存
- b. 接收特定的命令,然后对文件进行相应的操作
PS:如果有了以上软件,无须自己再去创建文件和文件夹,而是直接传递 命令 给上述软件,让其来进行文件操作,他们统称为数据库管理系统(DBMS,Database Management System)
3、什么是SQL ?
答:上述提到MySQL等软件可以接受命令,并做出相应的操作,由于命令中可以包含删除文件、获取文件内容等众多操作,对于编写的命令就是是SQL语句。SQL,是结构化语言(Structured Query Language)的缩写,SQL是一种专门用来与数据库通信的语言。
二、下载安装
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。
想要使用MySQL来存储并操作数据,则需要做几件事情:
a. 安装MySQL服务端
b. 安装MySQL客户端
b. 【客户端】连接【服务端】
c. 【客户端】发送命令给【服务端MySQL】服务的接受命令并执行相应操作(增删改查等)
下载 http://dev.mysql.com/downloads/mysql/ 安装 windows: 点点点 Linux: yum install mysql-server Mac: 点点点
Window版本
1、下载
MySQL Community Server 5.7.16 http://dev.mysql.com/downloads/mysql/
2、解压
如果想要让MySQL安装在指定目录,那么就将解压后的文件夹移动到指定目录,如:C:\mysql-5.7.16-winx64
3、初始化
MySQL解压后的 bin 目录下有一大堆的可执行文件,执行如下命令初始化数据:初始化得到一个data文件,创建一个root用户,没有密码
cd c:\mysql-5.7.16-winx64\bin mysqld --initialize-insecure
4、启动MySQL服务
执行命令从而启动MySQL服务
# 进入可执行文件目录 cd c:\mysql-5.7.16-winx64\bin # 启动MySQL服务 mysqld
5、启动MySQL客户端并连接MySQL服务
由于初始化时使用的【mysqld --initialize-insecure】命令,其默认未给root账户设置密码
# 进入可执行文件目录 cd c:\mysql-5.7.16-winx64\bin # 连接MySQL服务器 mysql -u root -p # 提示请输入密码,直接回车
输入回车,见下图表示安装成功:
到此为止,MySQL服务端已经安装成功并且客户端已经可以连接上,以后再操作MySQL时,只需要重复上述4、5步骤即可。但是,在4、5步骤中重复的进入可执行文件目录比较繁琐,如想日后操作简便,可以做如下操作。
a. 添加环境变量
将MySQL可执行文件添加到环境变量中,从而执行执行命令即可
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 -->
【将MySQL的bin目录路径追加到变值值中,用 ; 分割】
如此一来,以后再启动服务并连接时,仅需:
# 启动MySQL服务,在终端输入
mysqld
# 连接MySQL服务,在终端输入:
mysql -u root -p
b. 将MySQL服务制作成windows服务(将在后台运行)
上一步解决了一些问题,但不够彻底,因为在执行【mysqld】启动MySQL服务器时,当前终端会被hang住,那么做一下设置即可解决此问题:
# 制作MySQL的Windows服务,在终端执行此命令: "c:\mysql-5.7.16-winx64\bin\mysqld" --install 必须是绝对路径
# 移除MySQL的Windows服务,在终端执行此命令: "c:\mysql-5.7.16-winx64\bin\mysqld" --remove
注册成服务之后,以后再启动和关闭MySQL服务时,仅需执行如下命令:
# 启动MySQL服务
net start mysql
# 关闭MySQL服务
net stop mysql
Linux版本
安装:
yum install mysql-server
服务端启动:
mysql.server start
客户端连接:
连接: mysql -h host -u user -p 常见错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL
server daemon (Unix) or service (Windows) is not running. 退出: QUIT 或者 Control+D
三、数据库操作
1、显示数据库
show databases;
默认数据库:
mysql - 用户权限相关数据
test - 用于用户测试数据
information_schema - MySQL本身架构相关数据
2、创建删除数据库
# utf-8 create database 数据库名称 default charset utf8 collate utf8_general_ci; # gbk create database 数据库名称 default charset gbk collate gbk_chinese_ci;
drop database db2; 删除数据库
3、使用数据库
USE db_name;
显示当前使用的数据库中所有表:show tables;
4、用户管理
select user from user; 查看有哪些用户
创建用户 create user '用户名'@'IP地址' identified by '密码'; create user '用户名'@'%' identified by '密码';
%表示任意IP
删除用户 drop user '用户名'@'IP地址'; 修改用户 rename user '用户名'@'IP地址' to '新用户名'@'IP地址'; 修改密码 set password for '用户名'@'IP地址' = Password('新密码') PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)
5、授权管理
select查看 insert插入 update更新
show grants for '用户'@'IP地址' -- 查看权限 grant 权限 on 数据库.表 to '用户'@'IP地址' -- 授权 grant all privileges on 数据库.表 to '用户'@'IP地址' --授予所有的权限,除了授权这个权限
grant all privileges on *.* to '用户'@'IP地址'
revoke 权限 on 数据库.表 from '用户'@'IP地址' -- 取消权限

all privileges 除grant外的所有权限 select 仅查权限 select,insert 查和插入权限 ... usage 无访问权限 alter 使用alter table alter routine 使用alter procedure和drop procedure create 使用create table create routine 使用create procedure create temporary tables 使用create temporary tables create user 使用create user、drop user、rename user和revoke all privileges create view 使用create view delete 使用delete drop 使用drop table execute 使用call和存储过程 file 使用select into outfile 和 load data infile grant option 使用grant 和 revoke index 使用index insert 使用insert lock tables 使用lock table process 使用show full processlist select 使用select show databases 使用show databases show view 使用show view update 使用update reload 使用flush shutdown 使用mysqladmin shutdown(关闭MySQL) super 使用change master、kill、logs、purge、master和set global。还允许mysqladmin调试登陆 replication client 服务器位置的访问 replication slave 由复制从属使用 对于权限

对于目标数据库以及内部其他: 数据库名.* 数据库中的所有 数据库名.表 指定数据库中的某张表 数据库名.存储过程 指定数据库中的存储过程 *.* 所有数据库

用户名@IP地址 用户只能在改IP下才能访问 用户名@192.168.1.% 用户只能在改IP段下才能访问(通配符%表示任意) 用户名@% 用户可以再任意IP下访问(默认IP地址为%)

grant all privileges on db1.tb1 TO '用户名'@'IP' grant select on db1.* TO '用户名'@'IP' grant select,insert on *.* TO '用户名'@'IP' revoke select on db1.tb1 from '用户名'@'IP' 示例
特殊的:
flush privileges,将数据读取到内存中,从而立即生效。

# 启动免授权服务端 mysqld --skip-grant-tables # 客户端 mysql -u root -p # 修改用户名密码 update mysql.user set authentication_string=password('666') where user='root'; flush privileges; 忘记密码
四、数据表基本
1、创建表
查看表
show tables; select * from 表名; 查看表内所有内容
select name,age,id from 表名; 查看表内name、age、id内容
insert into t1(id,name) values(1,'alex'); 插入数据
show create table 表名 查看创建表的sql语句
主键、唯一索引区别
它们的一些比较: (1)对于主健/unique constraint , oracle/sql server/mysql等都会自动建立唯一索引; (2)主键不一定只包含一个字段,所以如果你在主键的其中一个字段建唯一索引还是必要的; (3)主健可作外健,唯一索引不可; (4)主健不可为空,唯一索引可; (5)主健也可是多个字段的组合; (6)主键与唯一索引不同的是: a.有not null属性; b.每个表只能有一个。 主键约束比唯一索引约束严格,当没有设定主键时,非空唯一索引自动称为主键。对于主键和唯一索引的一些区别主要如下: 1.主键不允许空值,唯一索引允许空值 2.主键只允许一个,唯一索引允许多个 3.主键产生唯一的聚集索引,唯一索引产生唯一的非聚集索引 注:聚集索引确定表中数据的物理顺序,所以是主键是唯一的(聚集就是整理数据的意思)
唯一索引:
唯一索引,一种索引,不允许具有索引值相同的行,从而禁止重复的索引或键值。系统在创建该索引时检查是否有重复的键值,并在每次使用 INSERT 或 UPDATE 语句添加数据
时进行检查。
create table 表名( 列名 类型 是否可以为空(null 、not null) default 1(默认数据) auto_increment primary key;
列名 类型 是否可以为空 )engine=innodb default charset=utf8;(默认字符编码utf8)
# innodb 支持事务,原子性操作(数据交互失败,数据回滚)
# myisam 存储事务快一些,相对于innodb
# auto_increment表示自增 配合primary key使用(一个表里面只能有一个自增列和约束,这两个是绑定的)
# primary key: 主键表示约束(不能重复且不能为空); 加速查找(这一列会生成另外一个文件,以后查找速度更快)

是否可空,null表示空,非字符串 not null - 不可空 null - 可空

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值 create table tb1( nid int not null defalut 2, num int not null )

CREATE TABLE `apk_download_info` ( `id` int(32) NOT NULL AUTO_INCREMENT, `downloadlink` varchar(255) DEFAULT NULL, `downloadStatusCode` int(8) NOT NULL COMMENT 'HTTP response code when downloading from link', `createTime` datetime DEFAULT NULL, `modifyTime` datetime DEFAULT NULL, `malwareInfo` varchar(2000) DEFAULT NULL, `fileSize` int(32) DEFAULT NULL, `fileHash` varchar(100) DEFAULT NULL, `retrieveScanReport` int(1) DEFAULT NULL, `permaLink` varchar(200) DEFAULT NULL, `resource` varchar(100) DEFAULT NULL COMMENT 'this is the resource returned from Virus Total upon a successful upload of the downdloaded file', `apkName` varchar(200) DEFAULT NULL, `retryCount` int(8) DEFAULT '0' COMMENT 'number of retries for downloading/uploading/retrieving reports', `maliciousLinkType` enum('恶意扣费','隐私窃取','远程控制','恶意传播','资费消耗','系统破坏','诱骗欺诈','流氓行为') CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, `malCount` int(8) DEFAULT NULL COMMENT 'the number of indentified malware info', `hasChineseProviders` int(1) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `link_index` (`downloadlink`) ) ENGINE=InnoDB AUTO_INCREMENT=13106337 DEFAULT CHARSET=utf8

CREATE TABLE `message_url_info` ( `id` int(32) NOT NULL AUTO_INCREMENT, `url` varchar(255) NOT NULL, `message_type` varchar(100) DEFAULT NULL, `found_time` datetime DEFAULT NULL, `is_phishing` int(8) DEFAULT '0' COMMENT '0 means that this url isnot phishing,1 means that this url is phishing', `phishing_url` varchar(255) DEFAULT NULL, `phishing_type` varchar(100) DEFAULT NULL, `modify_time` datetime DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `url_index` (`url`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。 create table tb1( nid int not null auto_increment primary key, num int null ) 或 create table tb1( nid int not null, num int not null, primary key(nid,num) )

create table userinfo( uid bigint auto_increment primary key, name varchar(32), department_id int, xx_id int, constraint fk_user_depar(外键的名字) foreign key (department_id) references department(id), constraint fk_xx_ff foreign key (xx_id) references XX(id)(多个外键的时都写在最后,最后不加逗号))engine=innodb default charset=utf8; create table department( id bigint auto_increment primary key, title char(15) )engine=innodb default charset=utf8;
自增
自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列) create table tb1( nid int not null auto_increment primary key, num int null ) 或 create table tb1( nid int not null auto_increment, num int null, index(nid) ) 注意:1、对于自增列,必须是索引(含主键)。 2、对于自增可以设置步长和起始值
desc t10; 查看表字段类型、默认值、是否为空
show create table t10; 查看这个表怎么创建的
show create table t10 \G; 纵向查看这个表怎么创建的
alter table t10 AUTO_INCREMENT=20;指定自增值
MySQL: 自增步长
基于会话级别:下次连接需要重新设置
show session variables like 'auto_inc%'; 查看全局变量
set session auto_increment_increment=2; 设置会话步长
# set session auto_increment_offset=10; 设置起始值
基于全局级别:
show global variables like 'auto_inc%'; 查看全局变量
set global auto_increment_increment=2; 设置会话步长
# set global auto_increment_offset=10; 设置起始值
SqlServer:自增步长:
基础表级别:
CREATE TABLE `t5` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) NOT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`nid`,`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET=utf8
2、删除表
drop table 表名
3、清空表
delete from 表名 清空,自增列表还是从原来的数字开始
truncate table 表名 内容清空,自增列表也清空(速度快)
4、修改表列、主键、外键
添加列:alter table 表名 add 列名 类型 alter table t1 add column addr varchar(20) not null;
alter table t1 add column addr varchar(20) not null after user1;
alter table t1 add column addr varchar(20) not null first;
删除列:alter table 表名 drop column 列名 修改列: alter table 表名 modify column 列名 类型; -- 类型 alter table 表名 change 原列名 新列名 类型; -- 列名,类型 添加主键: alter table 表名 add primary key(列名); 删除主键: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; 添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段); 删除外键:alter table 表名 drop foreign key 外键名称 修改默认值:alter table testalter_tbl alter i set default 1000;
删除默认值:alter table testalter_tbl alter i drop default;
5、修改表的索引
1.添加PRIMARY KEY(主键索引) ALTER TABLE table_name ADD PRIMARY KEY (column) ; 2.添加UNIQUE(唯一索引) ALTER TABLE table_name ADD UNIQUE INDEX index_name(column); 3.添加INDEX(普通索引) ALTER TABLE table_name ADD INDEX index_name (column); 4.添加FULLTEXT(全文索引) ALTER TABLE table_name ADD FULLTEXT (column); 5.添加多列索引 ALTER TABLE table_name ADD INDEX index_name (column1, column2, column3); 删除索引 drop index index_name on table_name ;
6、基本数据类型
MySQL的数据类型大致分为:数值、时间和字符串
bit[(m)] 二进制位(101001),m表示二进制位的长度(1-64),默认m=1 tinyint[(m)] [unsigned] [zerofill] 小整数,数据类型用于保存一些范围的整数数值范围: 有符号: -128 ~ 127. 无符号:0~ 255 特别的: MySQL中无布尔值,使用tinyint(1)构造。 int[(m)][unsigned][zerofill] 整数,数据类型用于保存一些范围的整数数值范围: 有符号: -2147483648 ~ 2147483647 无符号:0~ 4294967295 特别的:整数类型中的m仅用于显示,对存储范围无限制。例如: int(5),当插入数据2时,select 时数据显示为: 00002 bigint[(m)][unsigned][zerofill] 大整数,数据类型用于保存一些范围的整数数值范围: 有符号: -9223372036854775808 ~ 9223372036854775807 无符号: 0~ 18446744073709551615 decimal[(m[,d])] [unsigned] [zerofill] 准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。 特别的:对于精确数值计算时需要用此类型 decimal能够存储精确值的原因在于其内部按照字符串存储。 FLOAT[(m,d)] [UNSIGNED] [ZEROFILL] 单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。 无符号: -3.402823466E+38 to -1.175494351E-38, 1.175494351E-38 to 3.402823466E+38 有符号: 1.175494351E-38 to 3.402823466E+38 **** 数值越大,越不准确 **** DOUBLE[(m,d)] [UNSIGNED] [ZEROFILL] 双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。 无符号: -1.7976931348623157E+308 to -2.2250738585072014E-308 2.2250738585072014E-308 to 1.7976931348623157E+308 有符号: 2.2250738585072014E-308 to 1.7976931348623157E+308 **** 数值越大,越不准确 **** char (m) char数据类型用于表示固定长度的字符串,可以包含最多达255个字符。其中m代表字符串的长度。 PS: 即使数据小于m长度,也会占用m长度 varchar(m) varchars数据类型用于变长的字符串,可以包含最多达255个字符。其中m代表该数据类型所允许保存的字符串的最大长度,只要长度小于该最
大值的字符串都可以被保存在该数据类型中。 注:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度
的50%。因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡 text text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。 mediumtext A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. longtext A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters. enum(每次使用一个)
枚举类型, An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) 示例: CREATE TABLE shirts ( name varchar(40), size enum('x-small', 'small', 'medium', 'large', 'x-large') ); insert into shirts (name, size) values ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small'); set(集合内元素组合使用)
集合类型 A SET column can have a maximum of 64 distinct members. 示例: CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d'); DATE YYYY-MM-DD(1000-01-01/9999-12-31) TIME HH:MM:SS('-838:59:59'/'838:59:59') YEAR YYYY(1901/2155) DATETIME YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y) TIMESTAMP YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
二进制数据:TinyBlob、Blob、MediumBlob、LongBlob
更多参考:
- http://www.runoob.com/mysql/mysql-data-types.html
- http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html
五、表内容操作
1、增
insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表
insert into tb11(name,age) values('alex',12);
insert into tb11(name,age) values('alex',12),('root',18);
insert into tb12(name,age) select name,age from tb11;
2、删
truncate table 表;
delete from 表 delete from 表 where id=1 and name='alex' (红色为条件)
delete from tb12;
delete from tb12 where id !=2
delete from tb12 where id =2
delete from tb12 where id > 2
delete from tb12 where id >=2
delete from tb12 where id >=2 or name='alex'
3、改
update
表
set
name
=
'alex'
where
id>1
update tb12 set name='alex' where id>12 and name='xx'
update tb12 set name='alex',age=19 where id>12 and name='xx'
update tb12 set name=null #设置列为空
4、查
条件: case when id>8 then xx else xx end
三元运算: if(isnull(xx),0,1)
select * from 表 *效率低
select * from 表 where id > 1 select nid,name,gender as gg from 表 where id > 1 as相当于取别名,别名为gg
select count(id) from userinfo5; 查询表内有多少条数据
select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
select id,name as cname from tb12 where id > 10 or name ='xxx';
select name,age,11 from tb12; 多加了一列11
其他:
select * from tb12 where id != 1
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11)
select * from tb12 where id between 5 and 12; 闭区间
通配符:
select * from tb12 where name like "a%" 以a开头的所有(多个字符)
select * from tb12 where name like "a_" 以a开头的所有(一个字符)
分页:
select * from tb12 limit 10;
select * from tb12 limit 0,10; 0开始位置行,10查看的行数
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
select * from tb12 limit 10 offset 20; 20开始位置行,10查看的行数
排序:
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc; 按优先级安序排列
取后10条数据
select * from tb12 order by id desc limit 10;
分组:
select count(id),part_id from userinfo5 group by part_id; 统计数量
select max(id),part_id from userinfo5 group by part_id; part_id重合时,id取max
select nin(id),part_id from userinfo5 group by part_id;
聚合函数(去除重复)
count
max
min
sum
avg 平均值
**** 如果对于聚合函数结果进行二次筛选时?必须使用having ****
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;
连表操作:
select * from userinfo5,department5 where userinfo5.part_id = department5.id
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
# 左连接:userinfo5右边允许有空
select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
# 右连接:department5左边允许有空
select * from userinfo5 inner join department5 on userinfo5.part_id = department5.id
# 内连接:不会出现null
select * from userinfo5 full join department5 on userinfo5.part_id = department5.id
# 完全连接:左右都可能出现null
g、组合(上下连表)
组合,自动处理重合
select nickname from A union select name from B
组合,不处理重合
select nickname from A union all select name from B
外键创建表结构的几种情况

a. 用户表和部门表 用户: 1 alex 1 2 root 1 3 egon 2 4 laoyao 3 部门: 1 服务 2 保安 3 公关 ===》 一对多 b. 用户表和博客表 用户表: 1 alex 2 root 3 egon 4 laoyao 博客表: FK() + 唯一 1 /yuanchenqi/ 4 2 /alex3714/ 1 3 /asdfasdf/ 3 4 /ffffffff/ 2 ===> 一对一(唯一索引) c. 用户表(百合网) 相亲记录表 示例1: 用户表 相亲表 ===》多对多 不唯一索引 示例2: 用户表 主机表 用户主机关系表 ===》多对多 唯一索引
外键的变种:

0. 唯一索引 create table t1( id int ...., num int, xx int, unique 唯一索引名称 (列名,列名), constraint .... ) # 1 1 1 2 PS: 唯一: 约束不能重复(可以为空) PS: 主键不能重复(不能为空) 加速查找 1. 外键的变种 a. 用户表和部门表 用户: alex 1 root 1 egon 2 laoyao 3 部门: 服务 保安 公关 ===》 一对多 b. 用户表和博客表 用户表: alex root egon laoyao 博客表: FK() + 唯一 /yuanchenqi/ 4 /alex3714/ 1 /asdfasdf/ 3 /ffffffff/ 2 ===> 一对一(唯一索引) create table userinfo1( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table admin( id int not null auto_increment primary key, username varchar(64) not null, password VARCHAR(64) not null, user_id int not null, unique uq_u1 (user_id), CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id) )engine=innodb default charset=utf8; c. 用户表(百合网) 相亲记录表 示例1: 用户表 相亲表 ===》多对多 不唯一索引 示例2: 用户表 主机表 用户主机关系表 ===》多对多 唯一索引 create table userinfo2( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table host( id int auto_increment primary key, hostname char(64) )engine=innodb default charset=utf8; create table user2host( id int auto_increment primary key, userid int not null, hostid int not null, unique uq_user_host (userid,hostid), CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id), CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id) )engine=innodb default charset=utf8;
基于角色的权限管理(先给人物分配角色)

基于角色的权限管理(先给人物分配角色) 用户信息 id username pwd role_id 1 alex 123123 1 2 eric 123123 1 权限 1 订单管理 2 用户劵 3 Bug管理 .... 角色表: 1 IT部门员工 2 咨询员工 3 IT主管 角色权限管理 1 1 1 2 3 1 3 2 3 3