简体   繁体   English

使用许多外键在MySql中创建大表

[英]Create big table in MySql with many foreign keys

I am creating an App where I have to save Information About User Educations then I am going to show it to them. 我正在创建一个应用程序,必须在其中保存有关用户教育的信息,然后将其显示给他们。 Each user may have 1, 2 or more Educations. 每个用户可以接受1、2或更多的教育。 I am creating this table with laravel Schema builder. 我正在使用laravel模式构建器创建此表。 Here is code 这是代码

    Schema::create('educations', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->date('from')->nullable();
        $table->date('to')->nullable();
        $table->integer('country_id')->unsigned();
        $table->integer('city_id')->unsigned();
        $table->integer('university_id')->unsigned();
        $table->integer('faculty_id')->unsigned()->nullable();
        $table->integer('speciality_id')->unsigned()->nullable();
        $table->integer('degree_id')->unsigned()->nullable();
        $table->string('image', 100)->nullable();
        $table->text('additional')->nullable();


        $table->index('university_id');
        $table->index('faculty_id');
        $table->index('country_id');
        $table->index('city_id');
        $table->index('degree_id');
        $table->index('speciality_id');


        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade');

        $table->foreign('faculty_id')
            ->references('id')->on('faculties')
            ->onDelete('cascade');

        $table->foreign('degree_id')
            ->references('id')->on('degrees')
            ->onDelete('cascade');

        $table->foreign('speciality_id')
            ->references('id')->on('specialities')
            ->onDelete('cascade');

        $table->foreign('university_id')
            ->references('id')->on('universities')
            ->onDelete('cascade');

        $table->foreign('country_id')
            ->references('id')->on('countries')
            ->onDelete('cascade');

        $table->foreign('city_id')
            ->references('id')->on('cities')
            ->onDelete('cascade');
    });

MySql Code: MySql代码:

CREATE TABLE `educations` 
( 
    `id`            INT UNSIGNED NOT NULL auto_increment PRIMARY KEY, 
    `user_id`       INT UNSIGNED NOT NULL, 
    `from`          DATE NULL, 
    `to`            DATE NULL, 
    `country_id`    INT UNSIGNED NOT NULL, 
    `city_id`       INT UNSIGNED NOT NULL, 
    `university_id` INT UNSIGNED NOT NULL, 
    `faculty_id`    INT UNSIGNED NULL, 
    `speciality_id` INT UNSIGNED NULL, 
    `degree_id`     INT UNSIGNED NULL, 
    `image`         VARCHAR(100) NULL, 
    `additional`    TEXT NULL 
) 

DEFAULT CHARACTER SET utf8mb4 COLLATE 'utf8mb4_unicode_ci') 

So i have a question: when I want to get all Education of One user I hae to run join on almost 7 table or use laravel query builder and run many query when page loads. 所以我有一个问题:当我想获得一个用户的所有教育时,我要在几乎7个表上运行join或使用laravel查询生成器并在页面加载时运行许多查询。 Is my database design Correct? 我的数据库设计正确吗?

the reason why I am using foreign keys on each column is users must choose their faculties, universities, specialities and etc from my list in database which is variable. 我在每列上使用外键的原因是用户必须从数据库中的变量列表中选择其学院,大学,专业等。

any answer will help. 任何答案都会有所帮助。 thanks.. 谢谢..

I believe I can see your idea behind these relational structures but in my humble opinion, they are not quite correct because I feel like in some cases the column entries are not directly related with each other. 我相信我可以在这些关系结构的背后看到您的想法,但以我的拙见,它们并不完全正确,因为在某些情况下,我觉得列条目之间并不是直接相关的。 I will give you some examples: 我会给你一些例子:

  1. Your table name is 'educations'. 您的表格名称是“教育”。 1 user has got several educations but 1 education is not only reserved for 1 user. 1位用户接受了几次教育,但不仅为1位用户保留1位教育。 So this is an n:m-relation, which is why you need 1 table called 'educations', 1 for 'users' and 1 table in between those called 'user_has_got_education', for example. 因此,这是一个n:m关系,这就是为什么您需要1个名为“教育”的表,1个“用户”的表和1个介于“ user_has_got_education”之间的表的原因。 Your foreign keys land in the latter table with user_id and education_id. 您的外键与user_id和education_id一起位于后面的表中。
  2. country_id, city_id, university_id, faculty_id have got no direct relation to educations. country_id,city_id,University_id,faculty_id与教育没有直接关系。 Create a table called 'countries'(country_id, country_name, country_population, country_code,.....), then create a table called 'cities' in the same way. 创建一个名为“ countries”的表(country_id,country_name,country_population,country_code,.....),然后以相同的方式创建一个名为“ citys”的表。 Here it gets a bit more difficult because one city_id is not necessarily distinct as there could be TWO city_ids with the city_name='London', one referring to London(UK), one to London(USA, Ohio) and one to London(USA, Kentucky). 在这里变得更加困难,因为一个city_id不一定是唯一的,因为可能有两个city_id具有city_name =“ London”,一个是指伦敦(英国),一个是指伦敦(美国,俄亥俄州),另一个是指伦敦(美国) ,肯塔基州)。 So what you need here is at least a table relation to 'countries' AND a state/county/shire - and even that might not be enough (if there were 2 cities called 'London' in the USA in Ohio state, for example). 因此,您这里至少需要一个与“国家”和州/县/郡之间的表格关系,甚至可能还不够(例如,如果在俄亥俄州的美国有两个城市叫做“伦敦”,则为“伦敦”) 。 I call this 'direct relations'. 我称之为“直接关系”。 In short, your columns (foreign keys) are in the wrong place, they belong into a 'directly connected brother table', not > into a 'nephew table'. 简而言之,您的列(外键)放在错误的位置,它们属于“直接连接的兄弟表”,而不属于“侄子表”。

Try to get from one logical direct relation to another and 'connect' your tables accordingly. 尝试从一个逻辑直接关系到另一个逻辑直接关系,并相应地“连接”表。 I hope I could help a bit, can't really give you a full table structure here, though. 我希望我能有所帮助,但是在这里不能真正为您提供完整的表格结构。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM