• <bdo id='lO8XM'></bdo><ul id='lO8XM'></ul>

    1. <tfoot id='lO8XM'></tfoot>
    2. <i id='lO8XM'><tr id='lO8XM'><dt id='lO8XM'><q id='lO8XM'><span id='lO8XM'><b id='lO8XM'><form id='lO8XM'><ins id='lO8XM'></ins><ul id='lO8XM'></ul><sub id='lO8XM'></sub></form><legend id='lO8XM'></legend><bdo id='lO8XM'><pre id='lO8XM'><center id='lO8XM'></center></pre></bdo></b><th id='lO8XM'></th></span></q></dt></tr></i><div id='lO8XM'><tfoot id='lO8XM'></tfoot><dl id='lO8XM'><fieldset id='lO8XM'></fieldset></dl></div>
    3. <small id='lO8XM'></small><noframes id='lO8XM'>

      <legend id='lO8XM'><style id='lO8XM'><dir id='lO8XM'><q id='lO8XM'></q></dir></style></legend>

        如何将空值传递给外键字段?

        时间:2024-04-16

          <small id='7TPMS'></small><noframes id='7TPMS'>

            <tbody id='7TPMS'></tbody>

            <legend id='7TPMS'><style id='7TPMS'><dir id='7TPMS'><q id='7TPMS'></q></dir></style></legend>
            <tfoot id='7TPMS'></tfoot>

                • <bdo id='7TPMS'></bdo><ul id='7TPMS'></ul>
                  <i id='7TPMS'><tr id='7TPMS'><dt id='7TPMS'><q id='7TPMS'><span id='7TPMS'><b id='7TPMS'><form id='7TPMS'><ins id='7TPMS'></ins><ul id='7TPMS'></ul><sub id='7TPMS'></sub></form><legend id='7TPMS'></legend><bdo id='7TPMS'><pre id='7TPMS'><center id='7TPMS'></center></pre></bdo></b><th id='7TPMS'></th></span></q></dt></tr></i><div id='7TPMS'><tfoot id='7TPMS'></tfoot><dl id='7TPMS'><fieldset id='7TPMS'></fieldset></dl></div>

                  本文介绍了如何将空值传递给外键字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两张桌子:

                  大学:

                  university_id(p.k) | university_name
                  

                  用户:

                  uid | name | university_id(f.k)
                  

                  如何在用户表中保持university_id NULL?

                  How to keep university_id NULL in user table?

                  我只写了 1 个查询,我的查询是:

                  I am writting only 1 query, my query is:

                  INSERT INTO user (name, university_id) VALUES ($name, $university_id);
                  

                  这里 $university_id 可以从前端为 null.

                  Here $university_id can be null from front end.

                  university 表将由我默认设置.

                  在前端,学生将选择大学名称,根据university_id将传递给user表,但如果学生没有选择任何大学名称,则应该将空值传递给 university_id 字段的 user 表.

                  In the front end, student will select the university name, according to that the university_id will pass to user table, but if student is not selecting any university name then is should pass null value to the user table for university_id field.

                  推荐答案

                  只允许表useruniversity_id列允许NULL值,所以你可以保存空值.

                  Just allow column university_id of table user to allow NULL value so you can save nulls.

                  CREATE TABLE user
                  (
                     uid INT NOT NULL,
                     Name VARCHAR(30) NOT NULL, 
                     university_ID INT NULL,    -- <<== this will allow field to accept NULL
                     CONSTRAINT user_fk FOREIGN KEY (university_ID)
                         REFERENCES university(university_ID)
                  )
                  

                  更新 1

                  根据您的评论,您应该插入 NULL 而不是 ''.

                  based on your comment, you should be inserting NULL and not ''.

                  insert into user (name,university_id) values ('harjeet', NULL)
                  

                  更新 2

                  $university_id = !empty($university_id) ? "'$university_id'" : "NULL";
                  insert into user (name,university_id) values ('harjeet', $university_id);
                  

                  作为旁注,该查询容易受到 SQL 注入(s) 的变量来自外部.请查看下面的文章,了解如何预防.通过使用 PreparedStatements,您可以摆脱在值周围使用单引号.

                  As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

                  • 如何在 PHP 中防止 SQL 注入?

                  这篇关于如何将空值传递给外键字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 MySQL 中存储多对多关系的最佳方式? 下一篇:在 MySQL 中存储时区偏移的数据类型/结构

                  相关文章

                  <small id='8duRX'></small><noframes id='8duRX'>

                  <tfoot id='8duRX'></tfoot>
                    • <bdo id='8duRX'></bdo><ul id='8duRX'></ul>
                    <legend id='8duRX'><style id='8duRX'><dir id='8duRX'><q id='8duRX'></q></dir></style></legend>

                    1. <i id='8duRX'><tr id='8duRX'><dt id='8duRX'><q id='8duRX'><span id='8duRX'><b id='8duRX'><form id='8duRX'><ins id='8duRX'></ins><ul id='8duRX'></ul><sub id='8duRX'></sub></form><legend id='8duRX'></legend><bdo id='8duRX'><pre id='8duRX'><center id='8duRX'></center></pre></bdo></b><th id='8duRX'></th></span></q></dt></tr></i><div id='8duRX'><tfoot id='8duRX'></tfoot><dl id='8duRX'><fieldset id='8duRX'></fieldset></dl></div>