周六去考二级,应用第一题就是添加外键约束

草,写了半天老说语法不对,然后急中生智,觉得默认的库里应该有文档说明表

以下是SQL查询过程:

-- 猜测是在mysql库里面
mysql> USE mysql;
Database changed -- 查看这个库下的表
mysql> SHOW TABLES;
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| replication_asynchronous_connection_failover |
| replication_asynchronous_connection_failover_managed |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+------------------------------------------------------+
35 rows in set (0.46 sec)

  

然后发现这四张Help表,考试的时候我没关注过分类表

展开查看发现,里面已经说明了MySQL的内容:

mysql> SELECT * FROM help_category;
+------------------+---------------------------------------+--------------------+-----+
| help_category_id | name | parent_category_id | url |
+------------------+---------------------------------------+--------------------+-----+
| 0 | Contents | 0 | |
| 1 | Help Metadata | 0 | |
| 2 | Data Types | 0 | |
| 3 | Administration | 0 | |
| 4 | Functions | 0 | |
| 5 | Enterprise Encryption Functions | 4 | |
| 6 | Language Structure | 0 | |
| 7 | Geographic Features | 0 | |
| 8 | MBR | 7 | |
| 9 | WKT | 7 | |
| 10 | Comparison Operators | 4 | |
| 11 | Logical Operators | 4 | |
| 12 | Flow Control Functions | 4 | |
| 13 | Numeric Functions | 4 | |
| 14 | Date and Time Functions | 4 | |
| 15 | String Functions | 4 | |
| 16 | Cast Functions and Operators | 4 | |
| 17 | XML | 4 | |
| 18 | Bit Functions | 4 | |
| 19 | Encryption Functions | 4 | |
| 20 | Locking Functions | 4 | |
| 21 | Information Functions | 4 | |
| 22 | Spatial Functions | 4 | |
| 23 | WKT Functions | 22 | |
| 24 | WKB Functions | 22 | |
| 25 | Geometry Constructors | 22 | |
| 26 | Geometry Property Functions | 22 | |
| 27 | Point Property Functions | 22 | |
| 28 | LineString Property Functions | 22 | |
| 29 | Polygon Property Functions | 22 | |
| 30 | GeometryCollection Property Functions | 22 | |
| 31 | Geometry Relation Functions | 22 | |
| 32 | MBR Functions | 22 | |
| 33 | GTID | 4 | |
| 34 | Aggregate Functions and Modifiers | 4 | |
| 35 | GROUP BY Functions and Modifiers | 4 | |
| 36 | Window Functions | 4 | |
| 37 | Performance Schema Functions | 4 | |
| 38 | Internal Functions | 4 | |
| 39 | Miscellaneous Functions | 4 | |
| 40 | Data Definition | 0 | |
| 41 | Data Manipulation | 0 | |
| 42 | Transactions | 0 | |
| 43 | Compound Statements | 0 | |
| 44 | Account Management | 0 | |
| 45 | Table Maintenance | 0 | |
| 46 | User-Defined Functions | 0 | |
| 47 | Components | 0 | |
| 48 | Plugins | 0 | |
| 49 | Utility | 0 | |
| 50 | Storage Engines | 0 | |
+------------------+---------------------------------------+--------------------+-----+
51 rows in set (0.05 sec)

  

因为是关键字或者其他问题,尝试翻查这个关键字帮助表

当时写外键语句是这样:

ALTER TABLE xxx
ADD CONSTRAINT FOREIGN KEY xxx(xxx) REFERENCE xx(xx);

  

报错提醒发生在 reference这里

所以我当时就搜素两个关键字,外键和引用这两个

mysql> SELECT * FROM help_keyword WHERE `name` LIKE '%reference%';
+-----------------+------------+
| help_keyword_id | name |
+-----------------+------------+
| 653 | REFERENCE |
| 673 | REFERENCES |
+-----------------+------------+
2 rows in set (0.03 sec) mysql> SELECT * FROM help_keyword WHERE `name` LIKE '%FOREIGN%';
+-----------------+---------+
| help_keyword_id | name |
+-----------------+---------+
| 599 | FOREIGN |
+-----------------+---------+
1 row in set (0.02 sec)

  

然后官方给这个文档帮助表设定的说明关系是,关键字和主题是一个多对多的关系

用一个关系表维护,这里要先看【帮助主题】绑定的【关键字ID】

mysql> SELECT * FROM help_relation WHERE help_keyword_id = 599;
+---------------+-----------------+
| help_topic_id | help_keyword_id |
+---------------+-----------------+
| 508 | 599 |
| 518 | 599 |
| 520 | 599 |
| 521 | 599 |
+---------------+-----------------+
4 rows in set (0.12 sec)

我们还可以再看看之前reference关键字关联的

mysql> SELECT * FROM help_relation WHERE help_keyword_id = 653;
+---------------+-----------------+
| help_topic_id | help_keyword_id |
+---------------+-----------------+
| 519 | 653 |
| 532 | 653 |
+---------------+-----------------+
2 rows in set (0.02 sec) mysql> SELECT * FROM help_relation WHERE help_keyword_id = 673;
+---------------+-----------------+
| help_topic_id | help_keyword_id |
+---------------+-----------------+
| 520 | 673 |
| 521 | 673 |
| 610 | 673 |
+---------------+-----------------+
3 rows in set (0.02 sec)

  

可以发现599ID 和673ID都有共同两个帮助主题

所以确定是References关键字,然后看看520ID的主题是什么:

由于主题表输出的内容太多,格式完全混乱了,当时考试那个命令终端就是一直刷,还没办法停下

然后先查看主题表的字段:

mysql> DESC help_topic;
+------------------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------------+------+-----+---------+-------+
| help_topic_id | int unsigned | NO | PRI | NULL | |
| name | char(64) | NO | UNI | NULL | |
| help_category_id | smallint unsigned | NO | | NULL | |
| description | text | NO | | NULL | |
| example | text | NO | | NULL | |
| url | text | NO | | NULL | |
+------------------+-------------------+------+-----+---------+-------+
6 rows in set (0.08 sec)

  

最主要的是example样例和decription描述,但是example查询为空,只能看描述了

mysql> SELECT example FROM help_topic WHERE help_topic_id = 520;
+---------+
| example |
+---------+
| |
+---------+

  

520ID没找到,就找521,结果一看正好就是这个:

就是少写了S导致的

MySQL supports foreign keys, which permit cross-referencing related
data across tables, and foreign key constraints, which help keep the
related data consistent. A foreign key relationship involves a parent table that holds the
initial column values, and a child table with column values that
reference the parent column values. A foreign key constraint is defined
on the child table. The essential syntax for a defining a foreign key constraint in a
CREATE TABLE or ALTER TABLE statement includes the following: [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (col_name, ...)
REFERENCES tbl_name (col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option] reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT URL: https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

  

【MySQL】二级MySQL考试 救场帮助表的更多相关文章

  1. mysql的锁--行锁,表锁,乐观锁,悲观锁

    一 引言--为什么mysql提供了锁 最近看到了mysql有行锁和表锁两个概念,越想越疑惑.为什么mysql要提供锁机制,而且这种机制不是一个摆设,还有很多人在用.在现代数据库里几乎有事务机制,aci ...

  2. mysql复习-来源考试

    mysql复习-   No1 .登录和权限 (一)常用命令1.登录mysqlmysql -h localhost -u root -p 2.重启mysqlservice mysql restart 延 ...

  3. MySQL的选则字段+联表+判断+排序(order by)

    MySQL的选则字段+联表+判断+排序(order by) 两个表:1.成绩单 2.查询名单 目标: 1.选中全部字段,用于输出. 2.成绩单中有很多人的成绩,第一步是希望通过联表,只查查询名单上的人 ...

  4. MySQL学习笔记02_数据库和表的基本操作

    02_1 操作数据库 (1)创建数据库 CREATE DATABASE [IF NOT EXISTS] db_name [create_specification[, create_specifica ...

  5. MySQL中select * for update锁表的范围

    MySQL中select * for update锁表的问题 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例 ...

  6. MySQL中select * for update锁表的问题

    MySQL中select * for update锁表的问题 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例 ...

  7. mysql系统库INFORMATION_SCHEMA,MySQL,TEST,mysql系统表的作用

    本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TEST三个数据库的用途. 第一个数据库INFORMATION_SCHEMA: 提供了访问数据库元数据的方 ...

  8. MYSQL中约束及修改数据表

    MYSQL中约束及修改数据表 28:约束约束保证数据的完整性和一致性约束分为表级约束和列级约束约束类型包括:    NOT NULL(非空约束)    PRIMARY KEY(主键约束)    UNI ...

  9. MySQL 如何只导出 指定的表 的表结构和数据 ( 转 )

    MySQL 如何只导出 指定的表 的表结构和数据 ( 转 ) 2011-01-04 15:03:33 分类: MySQL MySQL 如何只导出 指定的表 的表结构和数据 导出更个库的表结构如下:my ...

  10. mysql存储过程之游标遍历数据表

    原文:mysql存储过程之游标遍历数据表 今天写一个mysql存储过程,根据自己的需求要遍历一个数据表,因为对存储过程用的不多,语法不甚熟悉,加之存储过程没有调试环境,花了不少时间才慢慢弄好,故留个痕 ...

随机推荐

  1. itest work(爱测试) 开源接口测试&敏捷测试管理平台 9.5.0 发布,接口测试及脑图用例升级

    (一)itest work 简介 itest work (爱测试)  一站式工作站让测试变得简单.敏捷,"好用.好看,好敏捷" ,是itest wrok 追求的目标.itest w ...

  2. 在AngularJS中,控制器没有生命周期方法

    在AngularJS中,控制器没有生命周期方法,但是$scope对象有一些事件,可以模拟生命周期方法的行为.例如,$scope.$on('$destroy', function() {...})可以在 ...

  3. kettle从入门到精通 第五十九课 ETL之kettle 邮件发送多个附件,使用正则轻松解决

    问题场景: 一个朋友说他用kettle将生成好的多个文件(a.xls和b.xls,文件在data目录下)发送给客户,但是data目录下还有其他的文件,他如果指定data目录发送会把 data目录下面的 ...

  4. INFINI Labs 产品更新 | Console 数据迁移支持 Percentiles 均匀分区

    INFINI Labs 产品又更新啦~,包括 Console v1.14.0,Gateway 1.21.0.其中 Console 数据迁移支持 Percentiles 均匀分区,修复已知 Bug 等. ...

  5. EF CORE 遇到“无法打开登录所请求的数据库 "win7bc"。登录失败。”

    报错内容:ex:An exception has been raised that is likely due to a transient failure. Consider enabling tr ...

  6. C# .NET 云南农信国密签名(SM2)简要解析

    BouncyCastle库(BC库)与云南农信最大的区别是 : BC库 SM2Signer.Init()  方法比云南农信多了最后3行代码: digest.Reset(); z = GetZ(user ...

  7. CentOS 7- 配置阿里镜像源

    1.备份CentOS 7系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo命令: mv /etc/yum.repos.d/CentOS-Base.repo /e ...

  8. 面试官:谈谈对SpringAI的理解?

    Spring AI 已经发布了好长时间了,目前已经更新到 1.0 版本了,所以身为 Java 程序员的你,如果还对 Spring AI 一点都不了解的话,那就有点太落伍了. 言归正传,那什么是 Spr ...

  9. IDEA 报错:无效的源发行版 sourceCompatibility

    IDEA 报错:无效的源发行版 sourceCompatibility 检查配置文件中的jdk版本的配置,//错误:sourceCompatibility = '18'//修改成正确的如下:sourc ...

  10. ONNX Runtime入门示例:在C#中使用ResNet50v2进行图像识别

    ONNX Runtime简介 ONNX Runtime 是一个跨平台的推理和训练机器学习加速器.ONNX 运行时推理可以实现更快的客户体验和更低的成本,支持来自深度学习框架(如 PyTorch 和 T ...