简体   繁体   English

在不使用枚举的情况下,从 MySQL 的一组预选数据中输入数据的最佳方法是什么?

[英]Without using enums, what's the best way to enter data from a set of preselected data for MySQL?

For example's sake, I'm going to say I'm making a database for previous presidents in a fictional world, where they have the Windows party, the Mac party, the Debian party, the Ubuntu Party, and the Unix Party.例如,我要说我正在为虚构世界中的前任总统制作一个数据库,在那里他们有 Windows 派对、Mac 派对、Debian 派对、Ubuntu 派对和 Unix 派对。 There's no other parties, but that might change every now and then (There are no 'independents').没有其他政党,但这可能会时不时地改变(没有“独立人士”)。 I want to scrape the data from a website that has these listed using web scraping libraries for PHP, but I'm worried that it might screw up when they change the website at some point, and accidentally enter any other entries than the following if I use char for MySQL.我想使用 PHP 的网络抓取库从列出这些数据的网站上抓取数据,但我担心当他们在某个时候更改网站时可能会搞砸,并且如果我不小心输入了以下条目以外的任何其他条目对 MySQL 使用char So for instance, if the website makes a typo and enter Ubunta instead of Ubuntu , that would mess up my db.因此,例如,如果网站Ubunta并输入Ubunta而不是Ubuntu ,那会弄乱我的数据库。

So instead of sanitizing data from the PHP side, what steps can I take to make sure any other entry than the preselected list of parties would spit out an error?因此,不是从 PHP 端清理数据,我可以采取哪些步骤来确保除预先选择的各方列表之外的任何其他条目都会吐出错误? Here are the options I considered so far:以下是我目前考虑的选项:

  • I know I can use the enum datatype, but I heard it's not really recommended.我知道我可以使用enum数据类型,但我听说并不真正推荐它。 I'm using Laravel for data input, and it's supported on newer versions, but enum itself, I heard, was not really a recommended way of doing things, especially on mySQL.我正在使用 Laravel 进行数据输入,并且它在较新版本上得到支持,但我听说enum本身并不是真正推荐的做事方式,尤其是在 mySQL 上。

  • I also know that I can use PHP side validation.我也知道我可以使用 PHP 端验证。 But I'm looking for better ways when there are more "preselected options", such as countries;但是当有更多“预选选项”(例如国家/地区)时,我正在寻找更好的方法; For example, I wouldn't want to have code that has something like if(country === "Afghanistan" || "United States" || ...) for something like 240 entries.例如,对于 240 个条目,我不希望代码具有类似if(country === "Afghanistan" || "United States" || ...)类的内容。 I also would like to add additional parties if I could, but I would like it to be maintainable, so people don't have to search to where to add additional if conditions.如果可以,我还想添加其他parties ,但我希望它是可维护的,这样人们就不必搜索添加额外if条件的位置。

  • I also saw that I can use trigger, but I would like to keep it short, if I were to use something like countries.我还看到我可以使用触发器,但如果我要使用国家之类的东西,我想保持简短。 I also would have to update these triggers manually, since Laravel doesn't support building trigger functions and have to do DB::unprepared .我还必须手动更新这些触发器,因为 Laravel 不支持构建触发器函数并且必须执行DB::unprepared unprepared 。

  • Creating a custom datatable for these parties or countries .为这些partiescountries创建自定义数据表。 Probably the best option, and would work well for longer list of options, like country , but it seems wildly unnecessary for shorter list of options, like party , where there are only 5 parties.可能是最好的选择,并且适用于较长的选项列表,例如country ,但对于较短的选项列表(例如party ,其中只有 5 个政党)似乎完全没有必要。 Would having FOREIGN KEY for SELECT statements speed up the query instead of doing WHERE ?FOREIGN KEY用于SELECT语句会加速查询而不是执行WHERE吗?

MySQL 8.0.16 supports check constraints. MySQL 8.0.16 支持check约束。 That is one option.那是一种选择。

However, if the values change over time, your best option is a reference table (your last option).但是,如果值随时间发生变化,您最好的选择是参考表(您的最后一个选择)。 A properly declared foreign key constraint does exactly what you want.正确声明的外键约束正是您想要的。 And adding new values in the future is as simple as adding a new row into the reference table.将来添加新值就像在引用表中添加新行一样简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用 mysql 服务器资源的情况下加密数据的最佳方法是什么? 使用 CFB 模式的 mcrypt 是使用 php 的最佳方式吗? - What is the best way to encrypt data without using the resource of mysql server ? Is mcrypt with CFB mode the best way with php? 在MySQL中存储敏感数据的最佳方法是什么? - What's the best way to store sensitive data in MySQL? 在MySQL中的两个表中同步行数据的最佳方法是什么? - What's the best way to sync row data in two tables in MySQL? 在 MySQL 中构建和存储部分数据的最佳方法是什么? - What's the best way to structure and store part of data in MySQL? 将html数据存储在mysql数据库中的最佳方法是什么? - What's the Best way to store html data in a mysql database? MongoDB-在不使用全文的情况下查询此数据的最佳方法是什么 - MongoDB - What's the best way to go about querying this data without using full-text 考虑到速度和安全性,从MySQL调用数据的最佳方法是什么? - what is the best way to call data from MySQL with speed and security in mind? 从 mysql 数据库生成和打印数据的最佳方法是什么 - What is the best way to generate & print out data from mysql database 在CakePHP中输入隐藏字段数据的最佳方法是什么? - What is the best way to enter hidden field data in CakePHP? 映射表时,将数据输入小型数据库的最佳方法是什么? - What is the best way to enter data into a small database when mapping tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM