定义数据表 假如某个电脑生产商,它的数据库中保存着整机和配件的产品信息。用来保存整机产品信息的表叫做pc;用来保存配件供货信息的表叫做parts。 在pc表中有一个字段,用来描述这款电脑所使用的CPU型号;在parts表中相应有一个字段,描述的正是CPU的型号
定义数据表
假如某个电脑生产商,它的数据库中保存着整机和配件的产品信息。用来保存整机产品信息的表叫做pc;用来保存配件供货信息的表叫做parts。
在pc表中有一个字段,用来描述这款电脑所使用的CPU型号;在parts表中相应有一个字段,描述的正是CPU的型号,我们可以把它想成是全部CPU的型号列表。
很显然,这个厂家生产的电脑,其使用的CPU一定是供货信息表(parts)中存在的型号。这时,两个表中就存在一种约束关系(constraint)——pc表中的CPU型号受到parts表中型号的约束。
首先我们来创建parts表:
<p>CREATE TABLE parts (<br>... 字段定义 ...,<br>model VARCHAR(20) NOT NULL,<br>... 字段定义 ...<br>);</p> |
接下来是PC表:
<p>CREATE TABLE pc (<br>... 字段定义 ...,<br>cpumodel VARCHAR(20) NOT NULL,<br>... 字段定义 ...<br>};</p> |
设置索引
若要设置外键,在参照表 (referencing table,即pc表) 和被参照表(referenced table,即parts表)中,相对应的两个字段必须都设置索引(index)。
对parts表:
<p>ALTER TABLE parts ADD INDEX idx_model (model);</p> |
这句话的意思是,为parts表增加一个索引,索引建立在model字段上,给这个索引起个名字叫idx_model。
对pc表也类似:
<p>ALTER TABLE pc ADD INDEX idx_cpumodel (cpumodel);</p> |
事实上这两个索引可以在创建表的时候就设置。这里只是为了突出其必要性。
定义外键
下面为两张表之间建立前面所述的那种“约束”。因为pc的CPU型号必须参照parts表中的相应型号,所以我们将pc表的cpumodel字段设置为“外键”(FOREIGN KEY),即这个键的参照值来自于其他表。
<p>ALTER TABLE pc ADD CONSTRAINT fk_cpu_model <br>FOREIGN KEY (cpumodel) <br>REFERENCES parts(model);</p> |
第一行是说要为pc表设置外键,给这个外键起一个名字叫做fk_cpu_model;第二行是说将本表的cpumodel字段设置为外键;第三行是说这个外键受到的约束来自于parts表的model字段。
这样,我们的外键就搞好了!如果我们试着CREATE一台pc,它所使用的CPU的型号是parts 表中不存在的,那么MySQL会禁止这台PC被CREATE出来。
级联操作
考虑以下这种情况:
技术人员发现,一个月之前输入到parts表中的某个系列的cpu(可能有很多款)的型号全都输错了一个字母,现在需要改正。我们希望的是,当parts表中那些 Referenced Column 有所变化时,相应表中的 Referencing Column 也能自动更正。
可以在定义外键的时候,在最后加入这样的关键字:
<p>ON UPDATE CASCADE;</p> |
即在主表更新时,子表(们)产生连锁更新动作,似乎有些人喜欢把这个叫“级联”操作。
如果把这语句完整的写出来,就是:
<p>ALTER TABLE pc ADD CONSTRAINT fk_cpu_model <br>FOREIGN KEY (cpumodel) <br>REFERENCES parts(model)<br>ON UPDATE CASCADE;</p> |
除了CASCADE外,还有RESTRICT(禁止主表变更)、SET NULL(子表相应字段设置为空)等操作。
【相关文章】
- MySQL数据库中用GRANT语句增添新用户
- 分析MySQL的数据类型以及建库策略
- SQL Server中的完整性和外键

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software