MySQL用户、库、表(单/多)操作
用户及权限操作:
管理员登录:mysql -uroot -p 用户设置密码:set password=password(密码); 查看数据库所有用户:select * from mysql.user; 查看用户权限:show grants for 用户名@网络地址 [identified by 密码] 普通用户登录:mysql -u用户名 -h网络地址 -p 查看当前用户:select user(); 查看当前数据库:select database(); 当前时间:now() 查看当前数据库是否是严格模式:select @@sql_mode; 创建用户:create user 用户名@网络地址 identified by 密码; 用户授权:grant all[SELECT INSERT UPDATE DELETE] on 数据库名.表名 to 用户名@网络地址(%表示所有) identified by 用户密码; 取消授权:revoke all[SELECT INSERT UPDATE DELETE ] on 数据库名.表名 from 用户名@网络地址 [identified by 密码]; 修改普通用户密码:set password for 用户名=password(密码); 查看当前用户权限:show grants;
库操作:
查看数据库: show databases; 查看当前数据库: select database(); 创建数据库: create database 数据库名 [charset utf-8]; 选择数据库: use 数据库名; 删除数据库: drop database 数据库名; 修改数据库: alter database 数据库名 charset utf8;
表操作:
MySQL支持的数据类型
参考博客:https://www.cnblogs.com/Eva-J/articles/9683316.html
参考博客:https://www.cnblogs.com/clschao/articles/9959559.html
1.数值类型(有约束条件无符号unsigned): int: 整型4字节 -2^31-2147483647 无符号:2^32-1 float: 单精度浮点数4字节 double: 双精度浮点数8字节 decimal: 小数值 2.字符类型: char: 定长字符,能表示的字符个数有限(0-255),读写快 varchar: 变长字符,能表示的字符个数多(0-65535),读写慢 3. 时间和日期类型:(系统内置函数now()获取当前时间) year: 年 date: 年月日 time: 时分秒 datetime: 年月日时分秒 timestamp: 年月日时分秒(1970-2038年之间,如果不设置这默认显示当前时间) 4.enum 和set 类型: enum(): 枚举,单选,自动屏蔽不存在的项 set(): 集合,多选,自动屏蔽不存在且去重 mysql支持的数据类型
MySQL中的约束条件
参考博客:https://www.cnblogs.com/Eva-J/articles/9687915.html
参考博客:https://www.cnblogs.com/clschao/articles/9968396.html
MySQL中的约束条件(可配合使用): 1.整型无符号: unsiged 2.唯一: unique 只能约束数据类型内不能重复,但不能约束null 3.非空: not null 4.默认值 default 值 5.自增: auto_increment 必须为数值类型,且设置唯一unique 6.主键: primary key 7.外键: foreign key 在没有设置主键的情况下,遇到约束条件为非空唯一时系统默认为主键!
MySQL中表的操作
创建表:
#语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); #注意: 1. 在同一张表中,字段名是不能相同 2. 宽度和约束条件可选 3. 字段名和类型是必须的 表创建语句
create table t1(id int,name char...); 实例: # create table class( # cid int primary key auto_increment, # caption char(4) unique not null #不设置非空且唯一的话按主键排序 # ); 表创建实例
查看表及表结构:
查看数据库中的表:show tables; 查看表结构: describe t1; #查看表结构,可简写为:desc 表名 show create table t1\G; #查看表详细结构,可加\G desc 表名;
删除表:
删: drop table 表名;
修改表结构(字段、约束等):
表的操作(alter table 表名......): (1)改名 alter table 表名 rename 新表名; (2)添加表字段 alter table 表名 add 字段 类型(长度) [约束] [first/after 字段]; (3)删除表字段 alter table 表名 drop 字段 ; (4)修改表字段 alter table 表名 change 字段 新字段 类型(长度); (5)修改表字段的类型(长度)、约束 alter table 表名 modify 字段 类型(长度)[约束] ; (6)修改表中字段的顺序 alter table 表名 change 字段 新字段 类型(长度)[约束] [first/after 字段]; alter table 表名 modify 字段 类型(长度)[约束] [first/after 字段] ; (7)添加/删除外键 alter table 表名 add constraint 名称描述 foreign key (字段) references 关系表名 (关系表内字段) on update cascade on delete cascade; alter table 表名 drop foreign key 名称描述;
(1)创建一个无约束表t1 create table t1( id int, name char(4), sex enum('male','female') ); mysql> insert into t1 values(2147483647,'yang',null); #有符号正常在规定范围内正常写入 mysql> select * from t1; +------------+------+------+ | id | name | sex | +------------+------+------+ | 2147483647 | yang | NULL | +------------+------+------+ 1 row in set (0.00 sec) mysql> insert into t1 values(2147483648,'peng','nihao'); #超出范围之后会自动按最大值写入,enum超出单选范围自动屏蔽 Query OK, 1 row affected, 2 warnings (0.01 sec) mysql> select * from t1; +------------+------+------+ | id | name | sex | +------------+------+------+ | 2147483647 | yang | NULL | | 2147483647 | peng | | +------------+------+------+ 2 rows in set (0.00 sec) (2)约束条件的表格t2 create table t2( id int unsigned, name char(4), sex enum('male','female') not null ); mysql> insert into t2 values(2147483648,'peng','nihao'); #约束无符号之后,超出有符号的范围正常写入;enum超出单选范围自动屏蔽 Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select * from t2; +------------+------+-----+ | id | name | sex | +------------+------+-----+ | 2147483648 | peng | | +------------+------+-----+ 1 row in set (0.00 sec) mysql>insert into t2 (id,name) values((21474836488,'yang'); #sex二选一没给值报错 mysql> insert into t2 values(2147483648,'peng','male'); #给正确的值直接写入 Query OK, 1 row affected (0.01 sec) mysql> select * from t2; +------------+------+------+ | id | name | sex | +------------+------+------+ | 2147483648 | peng | | | 2147483648 | peng | male | +------------+------+------+ 2 rows in set (0.00 sec)
# create table t3( # id int unique, # name char(4), # sex enum('male','female')default 'male' # ); mysql> desc t3; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | UNI | NULL | | | name | char(4) | YES | | NULL | | | sex | enum('male','female') | YES | | male | | +-------+-----------------------+------+-----+---------+-------+ 3 rows in set (0.02 sec) mysql> insert into t3(id,name) values(1,'yang'); #不加sex的值会自动使用default默认的 Query OK, 1 row affected (0.00 sec) mysql> select * from t3; +------+------+------+ | id | name | sex | +------+------+------+ | 1 | yang | male | +------+------+------+ 1 row in set (0.00 sec) mysql> insert into t3 values(1,'peng','female'); #id设置唯一,给了一个重复值直接报错 ERROR 1062 (23000): Duplicate entry '1' for key 'id'
# ip + port # 192.168.16.13 mysql 3306 # 192.168.16.13 kugou 8080 # 192.168.16.13 flask 5000 # 192.168.16.15 mysql 3306 # 192.168.16.16 mysql 3306 # create table service( # id int, # ip char(15), # name char(15), # port int(5), # unique(ip,port) #ip+port联合唯一 # ); mysql> desc service; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | ip | char(15) | YES | MUL | NULL | | | name | char(15) | YES | | NULL | | | port | int(5) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> insert into service values(1,'192.168.16.13', 'mysql', 3306),(2,'192.168.16.13', 'kugou', 8080),(3,'192.168.16.13', 'flask', 5000) -> ,(4,'192.168.16.15' ,'mysql',3306),(5,'192.168.16.16','mysql', 3306); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from service; +------+---------------+-------+------+ | id | ip | name | port | +------+---------------+-------+------+ | 1 | 192.168.16.13 | mysql | 3306 | | 2 | 192.168.16.13 | kugou | 8080 | | 3 | 192.168.16.13 | flask | 5000 | | 4 | 192.168.16.15 | mysql | 3306 | | 5 | 192.168.16.16 | mysql | 3306 | +------+---------------+-------+------+ 5 rows in set (0.00 sec) mysql> insert into service values(6,'192.168.16.13', 'hha', 3306); #重复的ip+port直接报错 ERROR 1062 (23000): Duplicate entry '192.168.16.13-3306' for key 'ip'
# create table t6( # id int unique auto_increment, # name char(4) , # sex enum('male','female') # ); mysql> desc t6; +-------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(4) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | +-------+-----------------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) mysql> insert into t6 (name,sex) values('yang','male'); #id 自动从1开始 Query OK, 1 row affected (0.01 sec) mysql> select * from t6; +----+------+------+ | id | name | sex | +----+------+------+ | 1 | yang | male | +----+------+------+ 1 row in set (0.00 sec) mysql> insert into t6 (name,sex) values('peng','haha'); #id自动变成2 Query OK, 1 row affected, 1 warning (0.01 sec) mysql> select * from t6; +----+------+------+ | id | name | sex | +----+------+------+ | 1 | yang | male | | 2 | peng | | +----+------+------+ 2 rows in set (0.00 sec)
(1) # create table t4( # id int unique , # name char(4) unique not null, #没有设置主键时,默认为主键PRI # sex enum('male','female') # ); mysql> desc t4; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | UNI | NULL | | | name | char(4) | NO | PRI | NULL | | | sex | enum('male','female') | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) (2) create table t5( id int primary key , #设置了id为主键 name char(4) unique not null, sex enum('male','female') ); mysql> desc t5; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | char(4) | NO | UNI | NULL | | | sex | enum('male','female') | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
# create table t7( # id1 int, # num int, # primary key(id1,num) # ); mysql> desc t7; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id1 | int(11) | NO | PRI | 0 | | | num | int(11) | NO | PRI | 0 | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
# 表2 班级表 cid class_name # create table clas( # cid int primary key, # class_name char(20) # ) # 表1 学生表 id name class_id # create table stu( # id int primary key , # name char(18), # class_id int, # foreign Key(class_id) references clas(cid) #设置外键连接到clas表的主键cid # ) mysql> insert into clas values(1,'一'); #在clas表中创建一个班级 Query OK, 1 row affected (0.01 sec) mysql> select * from clas; +-----+------------+ | cid | class_name | +-----+------------+ | 1 | 一 | +-----+------------+ 1 row in set (0.00 sec) mysql> insert into stu values(1,'yang',1); #stu表中添加外键班级信息(在clas中必须存在,否则报错) Query OK, 1 row affected (0.00 sec) mysql> select * from stu; +----+------+----------+ | id | name | class_id | +----+------+----------+ | 1 | yang | 1 | +----+------+----------+ 1 row in set (0.00 sec) mysql> insert into stu values(1,'yang',2); #stu表中添加外键不存在的班级信息直接报错 ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into clas values(2,'二'); #在clas表中创建一个班级2 Query OK, 1 row affected (0.00 sec) mysql> select * from clas; +-----+------------+ | cid | class_name | +-----+------------+ | 1 | 一 | | 2 | 二 | +-----+------------+ 2 rows in set (0.00 sec) #在设置了外键之后会对关联表的数据更新、删除产生约束 mysql> update clas set cid=3 where cid=1; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`day038`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `clas` (`cid`)) mysql> delete from clas where cid=1; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`day038`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `clas` (`cid`)) #未关联的可以自行修改 mysql> update clas set cid=3 where cid=2; Query OK, 1 row affected (0.00 sec) mysql> delete from clas where cid=3; Query OK, 1 row affected (0.00 sec)
# 表2 班级表 cid class_name # create table clas( # cid int primary key, # class_name char(20) # ) (1)外键关联: 表1 学生表 id name class_id # create table stu( # id int primary key , # name char(18), # class_id int, # foreign Key(class_id) references clas(cid) #设置外键连接到clas表的主键cid # ) # (2)级联更新 级联删除(# 表1 学生表 id name class_id) # create table stu( # id int primary key , # name char(18), # class_id int, # foreign key(class_id) references clas(cid) on update cascade on delete cascade # );
参考博客:https://www.cnblogs.com/Eva-J/articles/9677452.html#_label2
1、首先创建一个数据表table_test: create table table_test( `id` varchar(100) NOT NULL, `name` varchar(100) NOT NULL, PRIMARY KEY (`name`) ); 2、如果发现主键设置错了,应该是id是主键,但如今表里已经有好多数据了,不能删除表再重建了,仅仅能在这基础上改动表结构。 先删除主键 alter table table_test drop primary key; 然后再增加主键 alter table table_test add primary key(id); 注:在增加主键之前,必须先把反复的id删除掉。
创建press表 CREATE TABLE `press` ( `id` int(11) NOT NULL, `name` char(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ; 创建book表 CREATE TABLE `book` ( `id` int(11) DEFAULT NULL, `bk_name` char(12) DEFAULT NULL, `press_id` int(11) NOT NULL, KEY `press_id` (`press_id`) ) ; 为book表添加外键 alter table book add constraint fk_id foreign key(press_id) references press(id); 删除外键 alter table book drop foreign key fk_id;
create table t(id int unique,name char(10) not null); #去掉null约束 alter table t modify name char(10) null; # 添加null约束 alter table t modify name char(10) not null; # 去掉unique约束 alter table t drop index id; # 添加unique约束 alter table t modify id int unique; alter处理null和unique约束
语法: 1. 修改表名 ALTER TABLE 表名 RENAME 新表名; 2. 增加字段 ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…], ADD 字段名 数据类型 [完整性约束条件…]; 3. 删除字段 ALTER TABLE 表名 DROP 字段名; 4. 修改字段 ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…]; ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…]; ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…]; 5.修改字段排列顺序/在增加的时候指定字段位置 ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] FIRST; ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名; ALTER TABLE 表名 CHANGE 字段名 旧字段名 新字段名 新数据类型 [完整性约束条件…] FIRST; ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…] AFTER 字段名;
单表操作实例:
mysql> desc staff_info; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | age | int(3) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.00 sec) # 表重命名 mysql> alter table staff_info rename staff; Query OK, 0 rows affected (0.00 sec) mysql> desc staff; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | age | int(3) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.00 sec) # 删除sex列 mysql> alter table staff drop sex; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ rows in set (0.01 sec) # 添加列 mysql> alter table staff add sex enum('male','female'); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 # 修改id的宽度 mysql> alter table staff modify id int(4); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.01 sec) # 修改name列的字段名 mysql> alter table staff change name sname varchar(20); Query OK, 4 rows affected (0.03 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | sname | varchar(20) | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.00 sec) # 修改sex列的位置 mysql> alter table staff modify sex enum('male','female') after sname; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | sname | varchar(20) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.00 sec) # 创建自增id主键 mysql> alter table staff modify id int(4) primary key auto_increment; Query OK, 4 rows affected (0.02 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | sname | varchar(20) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+----------------+ rows in set (0.00 sec) # 删除主键,可以看到删除一个自增主键会报错 mysql> alter table staff drop primary key; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key # 需要先去掉主键的自增约束,然后再删除主键约束 mysql> alter table staff modify id int(11); Query OK, 4 rows affected (0.02 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | sname | varchar(20) | YES | | NULL | | | sex | enum('male','female') | YES | | NULL | | | age | int(3) | YES | | NULL | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.01 sec) mysql> alter table staff drop primary key; Query OK, 4 rows affected (0.06 sec) Records: 4 Duplicates: 0 Warnings: 0 # 添加联合主键 mysql> alter table staff add primary key (sname,age); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 # 删除主键 mysql> alter table staff drop primary key; Query OK, 4 rows affected (0.02 sec) Records: 4 Duplicates: 0 Warnings: 0 # 创建主键id mysql> alter table staff add primary key (id); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | sname | varchar(20) | NO | | | | | sex | enum('male','female') | YES | | NULL | | | age | int(3) | NO | | 0 | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ rows in set (0.00 sec) # 为主键添加自增属性 mysql> alter table staff modify id int(4) auto_increment; Query OK, 4 rows affected (0.02 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> desc staff; +-------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | sname | varchar(20) | NO | | | | | sex | enum('male','female') | YES | | NULL | | | age | int(3) | NO | | 0 | | | phone | bigint(11) | YES | | NULL | | | job | varchar(11) | YES | | NULL | | +-------+-----------------------+------+-----+---------+----------------+ rows in set (0.00 sec)
多表关系创建----foreign key约束:
多表关系分析:
分析步骤: #1、先站在左表的角度去找 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id) #2、再站在右表的角度去找 是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id) #3、总结: #多对一: 如果只有步骤1成立,则是左表多对一右表 如果只有步骤2成立,则是右表多对一左表 #多对多 如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系 #一对一: 如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
多对一或一对多:
#一对多或称为多对一
三张表:出版社,作者信息,书 一对多(或多对一):一个出版社可以出版多本书 关联方式:foreign key
=====================多对一===================== create table press( id int primary key auto_increment, name varchar(20) ); create table book( id int primary key auto_increment, name varchar(20), press_id int not null, foreign key(press_id) references press(id) on delete cascade on update cascade ); insert into press(name) values ('北京工业地雷出版社'), ('人民音乐不好听出版社'), ('知识产权没有用出版社') ; insert into book(name,press_id) values ('九阳神功',1), ('九阴真经',2), ('九阴白骨爪',2), ('独孤九剑',3), ('降龙十巴掌',2), ('葵花宝典',3) ;
多对一:其它关系实例
多对多:
#多对多 三张表:出版社,作者信息,书 多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多 关联方式:foreign key+一张新的表
=====================多对多===================== create table author( id int primary key auto_increment, name varchar(20) ); #这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了 create table author2book( id int not null unique auto_increment, author_id int not null, book_id int not null, constraint fk_author foreign key(author_id) references author(id) on delete cascade on update cascade, constraint fk_book foreign key(book_id) references book(id) on delete cascade on update cascade, primary key(author_id,book_id) ); #插入四个作者,id依次排开 insert into author(name) values('egon'),('alex'),('yuanhao'),('wpq'); #每个作者与自己的代表作如下 egon: 九阳神功 九阴真经 九阴白骨爪 独孤九剑 降龙十巴掌 葵花宝典 alex: 九阳神功 葵花宝典 yuanhao: 独孤九剑 降龙十巴掌 葵花宝典 wpq: 九阳神功 insert into author2book(author_id,book_id) values (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,1), (2,6), (3,4), (3,5), (3,6), (4,1) ;
服务和机器
一个服务可能被部署到多台机器上,一台机器上也可以部署多个服务
学生和课程
一个学生可以选择多门课程,一门课程也可以被多个学生选择
一对一:
#一对一 两张表:学生表和客户表 一对一:一个学生是一个客户 关联方式:foreign key+unique
create table customer( -> id int primary key auto_increment, -> name varchar(20) not null, -> qq varchar(10) not null, -> phone char(16) not null -> ); create table student( -> id int primary key auto_increment, -> class_name varchar(20) not null, -> customer_id int unique, #该字段一定要是唯一的 -> foreign key(customer_id) references customer(id) #外键的字段一定要保证unique -> on delete cascade -> on update cascade -> ); #增加客户 mysql> insert into customer(name,qq,phone) values -> ('韩蕾','31811231',13811341220), -> ('杨澜','123123123',15213146809), -> ('翁惠天','283818181',1867141331), -> ('杨宗河','283818181',1851143312), -> ('袁承明','888818181',1861243314), -> ('袁清','112312312',18811431230) mysql> #增加学生 mysql> insert into student(class_name,customer_id) values -> ('脱产1班',3), -> ('周末1期',4), -> ('周末1期',5) -> ;
例一:一个用户只有一个博客 用户表: id name egon alex wupeiqi 博客表 fk+unique id url name_id xxxx 1 yyyy 3 zzz 2 例二:一个管理员唯一对应一个用户 用户表: id user password egon xxxx alex yyyy 管理员表: fk+unique id user_id password 1 xxxxx 2 yyyyy