Mysql集群创建外键,分为四种约束:no action,restrict,cascade,set null。如果表A的主关键字是表B中的字段,则该字段称为B的外键,表A称为主表,表B称为从表。外键是用来实现参照完整性的,不同的外键不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常的维护工作更加轻松。

CASCADE: 从父表删除或更新且自动删除或更新子表中匹配的行。
·SET NULL: 从父表删除或更新行,并设置子表中的外键列为NULL。
·NO ACTION: 在ANSI SQL-92标准中,NO ACTION意味这不采取动作,就是如果有一个相关的外键值在被参考的表里,删除或更新主要键值的企图不被允许进行(Gruber, 掌握SQL, 2000:181)。 InnoDB拒绝对父表的删除或更新操作。
RESRICT:同no action。
环境模拟:
先来添加两张表:ta_resource和ta_resourcetime。
DROP TABLE IF EXISTS `ta_resource`; CREATE TABLE `ta_resource` ( `ResourceId` varchar(64) NOT NULL, `ResourcePId` varchar(64) DEFAULT NULL, `ResourceName` varchar(64) DEFAULT NULL, `Type` varchar(64) DEFAULT NULL, `Desc` varchar(512) DEFAULT NULL, `Priority` smallint(6) DEFAULT NULL, `IsFold` varchar(2) DEFAULT NULL, `IsUse` varchar(2) DEFAULT NULL, `URL` varchar(64) DEFAULT NULL, `Icon` varchar(512) DEFAULT NULL, `Css` varchar(64) DEFAULT NULL, `JavaScript` varchar(64) DEFAULT NULL, `TimeSpan` datetime DEFAULT NULL, `IsDefault` varchar(2) DEFAULT NULL, PRIMARY KEY (`ResourceId`) ) ENGINE=ndbcluster DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for `ta_resourcetime` -- ---------------------------- DROP TABLE IF EXISTS `ta_resourcetime`; CREATE TABLE `ta_resourcetime` ( `TimeId` varchar(64) NOT NULL, `ResourceId` varchar(64) NOT NULL, `ResourceName` varchar(64) DEFAULT NULL, `RoleId` varchar(64) NOT NULL, `RoleName` varchar(64) DEFAULT NULL, `URL` varchar(64) DEFAULT NULL, `StartTime` datetime NOT NULL, `EndTime` datetime NOT NULL, `IsDeleted` varchar(2) DEFAULT NULL, `Desc` varchar(512) DEFAULT NULL, PRIMARY KEY (`TimeId`), KEY `ResourceId` (`ResourceId`), CONSTRAINT `resourceid` FOREIGN KEY (`ResourceId`) REFERENCES `ta_resource` (`ResourceId`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=ndbcluster DEFAULT CHARSET=utf8;
1、no action时
这两个表中的外键使用的是ON DELETE NO ACTION ON UPDATE NO ACTION
如果更改数据时, ta_resource是主表,ta_resourcetime是从表。
更新数据时:可以修改主表,但是不可以修改从表的数据。
删除数据时:先删除从表,再删除主表。
2、cascade时
集群搭建完再插入数据库表时,在on delete 和on update 都使用cascade时遇到了这样的问题:不能添加外键约束。

之前以为是mysql集群不支持外键,在网上查了mysql集群该版本支持外键约束和师哥们讨论过这个问题,表示mysql集群支持外键约束。于是经过多次实践,找到出现这种问题的原因:mysql集群中支持使用cascade,但是on delete 和on update只能有一个使用cascade,另一个使用no action就可以。
3、内存不足
出现的问题:插入不进去数据,经过查询,是由于内存不足

解决方法:
在配置文件config.ini中对内存进行设置,主要对这几项进行设置:
MaxNoOfTables=10240
设置集群中最大表对象数量
MaxNoOfAttributes=500000
设置可在集群中定义的属性数量
MaxNoOfConcurrentTransactions=1000000
用于设定节点内可能的并发事务数
MaxNoOfConcurrentOperations=1000000
设置能同时出现在更新阶段或同时锁定的记录数
MaxNoOfOrderedIndexes=10240
设置有序索引的总数
如果不配置这些,mysql集群中会默认内存的大小,当内存满了以后就插不进去数据了。
以上就是Mysql外键约束_MySQL的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!
MySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AMThe main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.
MySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AMThe steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.
MySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AMMySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.
Is MySQL Beginner-Friendly? Assessing the Learning CurveApr 17, 2025 am 12:19 AMMySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.
Is SQL a Programming Language? Clarifying the TerminologyApr 17, 2025 am 12:17 AMYes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AMACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.
MySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AMMySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.
MySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AMMySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function






