【mysql】一对一关系的理解,以及Navicat Premium怎么设置字段的唯一性(UNIQUE)?
2024-08-29 19:44:18
背景:一对一关系设计方法:
设计2张表:customer表,student表
学生表中通过字段customer_id与customer表产生关系。
student表中的customer_id与customer表中的id值(主键)应当是一对一的关系。
所以在student表中要保证customer_id值是唯一的。
2张表的表结构如下:
左表的一条记录唯一对应右表的一条记录,反之也一样
create table customer(
id int primary key auto_increment,
name char(20) not null,
qq char(10) not null,
phone char(16) not null
); create table student(
id int primary key auto_increment,
class_name char(20) not null,
customer_id int unique, #该字段一定要是唯一的
foreign key(customer_id) references customer(id) #此时外键的字段一定要保证unique
on delete cascade
on update cascade );
通过navicat设置表的某1个字段值为一,就是为该字段建立索引,索引类型选择UNIQUE
具体的操作步骤,参照:
Navicat Premium怎么设置字段的唯一性(UNIQUE)?
检查配置的结果:
当某1个字段的值被设置为唯一之后,如表中已经存在custome_id=1,当再次插入custome_id=1的记录时,就会报错
最新文章
- sqlyog导出json数据格式支持mysql数据转存mongodb
- PHP中多线程处理
- NodeJs+http+fs+request+cheerio 采集,保存数据,并在网页上展示(构建web服务器)
- 将博客搬至CSDN
- linux时间同步ntp服务的安装与配置
- C# GZip对字符串压缩和解压
- (zhuan) Deep Reinforcement Learning Papers
- python中的popen和subprocess
- 【BZOJ】【1015】 【JSOI2008】星球大战starwar
- error: format not a string literal and no format arguments [-Werror=format-security]
- Java基础知识强化之IO流笔记17:FileOutputStream构造方法使用
- hadoop2.5.2安装部署
- 从一个例子入门Mysql储存过程
- Salesforce的公式和验证规则
- JSESSIONID、SESSION、cookie .
- 算法 -- 四种方法获取的最长“回文串”,并对时间复杂进行分析对比&;PHP
- 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。System.Threading.ThreadAbortException
- jms和activemq简介
- Ext BoxComponent
- javascript原型继承---constructor篇