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

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

以下是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. Qt QMainWindow的使用

    参考视频:黑马科技:https://www.bilibili.com/video/BV1XW411x7NU?p=19 QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu b ...

  2. .Net Core 部署IIS

    我相信很多人看了其他的贴子,都没有成功部署,因为里面有很多暗坑.接下来博主就一步一步给大家讲明白,带领大家部署 先基本的发布 操作:右击web项目的<发布>按钮.选文件 配置发布属性    ...

  3. vue带有参数的路由跳转 动态路由

    先定义好路由在router文件下面创建一个新的文件夹里面写上自己定义的路由 export default {   path: '/detail/:id',   component: () =>  ...

  4. [ROI 2018] Innophone 题解

    [ROI 2018] Innophone 看了半天网上仅有的一篇题解--才堪堪写出来 不过在LOJ上看提交,全是 KTT,看得我瑟瑟发抖(不会 题意翻译 在平面上有一些点,你需要在这个平面上任意确定一 ...

  5. Spring Boot 使用 拦截器 实现 token 验证

    Spring Boot 使用 拦截器 实现 token 验证 整体思路:1.写一个工具类封装生成.校验和解析 token 的方法:2.在注册和登录时生成 token ,生成的 token 存入 red ...

  6. 【踩坑】.NET 8.0 自定义IExceptionHandler不生效

    中间件实现异常处理 在ASP.NET Core里,我们可以使用中间件(Middleware)实现全局的异常处理. 如内置的异常处理中间件 UseExceptionHandler app.UseExce ...

  7. centos8使用nmcli实现bond

    #添加bonding接口 nmcli con add type bond con-name bond0 ifname bond0 mode active-backup ipv4.method manu ...

  8. Windows无法调节亮度

    原因1:驱动问题 解决方式: 安装360驱动大师,一键安装. 也可以使用其他软件:如驱动精灵. 推荐使用电脑品牌本身的驱动软件:如联想:联想驱动管理 原因2:设备管理问题 解决方式: 计算机 -> ...

  9. Jetpack Compose(7)——触摸反馈

    目录 一.点按手势 1.1 Modifier.clickable 1.2 Modifier.combinedClickable 二.滚动手势 2.1 滚动修饰符 Modifier.verticalSc ...

  10. Linux中的IDR机制

    # Linux中的IDR机制 背景 最近在学习 Linux的i2c子系统,看到代码中有关于IDR的调用.了解了一下有关的文档,发现是用来管理指针(对象实例). //based on linux V3. ...