剪不断,理不乱数据库中主外键的关系
在做机房收费系统时,考虑到了主外键,所以学习了一下,下面我们一起来 探究一下它们的关系。 主键Primary key,唯一标示一个实体。是保证数据库的实体完整性,保证数据中数据的正确性和合理性,取非空唯一。 外键Foreign,是用来使表与表之间联系。用来保证
在做机房收费系统时,考虑到了主外键,所以学习了一下,下面我们一起来探究一下它们的关系。
主键Primary key,唯一标示一个实体。是保证数据库的实体完整性,保证数据中数据的正确性和合理性,取值非空唯一。
外键Foreign,是用来使表与表之间联系。用来保证数据库的参照完整性,外键的取值必须来自参照表参照列的值,可以为空也可不为空。
外键的作用:
保持数据一致性,完整性,主要目的是控制存储在外键表中的数据。 使两张表形成关联,外键只能引用外表中的列的值!
主外键的关系结构:
1,一对一,不用引用主外键,把它们放一个表中即可
例如:一个学生只能有一个卡号,那么学生跟卡号放在一个表中即可
2,一对多,引用主外键,‘一’相当于主键,‘多’即是引用主键的外键。
例如:一个班级可以有多个学生,并且一个学生只能属于一个班级,这就是一对多的关系;
3,多对多关系,需要创建一个表,表中需要两个字段,分别是引用,两个多对多字段主键的外键。
例如:
学生表(学号,姓名,…………)主键:学号
课程表(课程号,课程名,…………)主键:课程号
一个学生能选多门课,一个课可以由多个学生选,即是多对多关系,
那么成绩表就是它们之间关系的体,
即引用成绩表(学号,课程号,成绩)。这里学号和课程号,分别是学生表和课程表中学号和课程号的外键
在我们现在这个阶段中,主要还是使用,主外键间的一对多的关系
下面一起来简单看一下大概建成的表结构
<span>--建班级表 create table class( classid int primary key,--定义班级ID为主键 classname varchar(15) ) </span>
<span>--建学生表 create table students( studentid int primary key,--定义学生ID为主键 classid int ,--外键值,跟班级表classid 属性类型相同 stuname varchar(20),--学生姓名 </span>
<span>---定义外键 foreign key(classid) references class(classid) --本表classid是基于class表classid的外键 ) </span>
如上定义了主外键后,两个表间的关系就是一对多的关系了,并且学生表内的classid必须依托班级表的classid存在,也就是说外键必须要主键存在的时候才能创建,
例如:
--在班级表为空的情况往学生表插入一条记录是不允许的:
<span>insert intostudents(studentid,classid,stuname)values(1,1,'小明')</span>
系统会抛出异常提示主键表班级表内班级ID不存在这样是不允许插入的;
必须要先往班级表内插入一条记录:
<span>insert into class(classid,classname)values(1,'一班')</span>
后才能执行插入前面一条往学生表插入信息的语句..
解释:
对于机房收费系统来说,如图:
只有当学生注册了信息,有了卡号,在学生信息表中存在了,才可以进行充值,退卡,上下机等,主外键的作用也就是当没有学生信息时,我们不可以进行充值等操作,而且想要删除学生信息表中的数据,必须先将充值表,退卡表,上下机表中此卡号的人删除之后才可以删除学生信息表中的数据,这样就避免了当学生未注册时,就可以充值的情况
也就是说,如果主键表中数据的更改使之与外键表中数据的链接失效,则这种更改是不能实现的,从而确保了引用完整性。如果试图删除主键表中的行或更改主键值,而该主键值与另一个表的 FOREIGN KEY 约束值相关,则该操作不可实现。若要成功更改或删除 FOREIGN KEY 约束的行,可以先在外键表中删除外键数据或更改外键数据,然后将外键链接到不同的主键数据上去。
外键是用来控制数据库中数据的数据完整性的 。就是当你对一个表的数据进行操作和他有关联的一个或更多表的数据能够同时发生改变,这就是外键的作用
连接查询与内外键的关系:
连接的 连接条件:A表的主键 = B表的外键如果做连接的两表没有主外键关系,那么连接结果没有意义。
例:学生表和选课表做连接,select *from 学生表,选课表where 学生表.学号(主键)=选课表.学号(外键)
主外键可以建级联级触发器,用来做级联删除、更新。作用原理还是因为主外键将两表联系。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
