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. I am creating this table with laravel Schema builder. Here is code
Schema::create('educations', function (Blueprint $table) {
$table->increments('id');
$table->increments('user_id')->unsigned();
$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:
CREATE TABLE `educations`
(
`id` INT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
`user_id` INT UNSIGNED NOT NULL,
`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. 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..
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire