简体   繁体   中英

How to implement SQL Foreign Keys in Parent Tables

First Question here:

Currently I am working on a project that requires the use of 3 different Entities. Those Entities are: Users, Venues and Facilities.

Each Entity has its table:

Facility Table:

CREATE TABLE `facility` (
`facility_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`venue_id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
`surface` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
`max_timeblock` INT(10) UNSIGNED NOT NULL,
`min_timeblock` INT(10) UNSIGNED NOT NULL,
`availability` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`facility_id`),
UNIQUE INDEX `facility_id` (`facility_id`),
INDEX `facility-venue_id_key` (`venue_id`),
CONSTRAINT `facility-venue_id_key` FOREIGN KEY (`venue_id`) REFERENCES `venue` (`venue_id`) ON UPDATE CASCADE ON DELETE CASCADE)

Venue Table:

CREATE TABLE `venue` (
    `venue_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `description` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `email` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `phone` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `opening_time` TIME NOT NULL,
    `closing_time` TIME NOT NULL,
    `booking_conditions` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `booking_method` INT(10) UNSIGNED NOT NULL,
    `status` INT(10) UNSIGNED NULL DEFAULT NULL,
    PRIMARY KEY (`venue_id`),
    UNIQUE INDEX `venue_id` (`venue_id`)
)

User Table:

CREATE TABLE `user` (
    `user_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'This field will hold the Unique ID for each user.',
    `username` VARCHAR(50) NOT NULL COMMENT 'This field will be classed as a display name for the users so when searching they arent looking for the ID.' COLLATE 'utf8_unicode_ci',
    `email` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `password` VARCHAR(128) NOT NULL COLLATE 'utf8_unicode_ci',
    `first_name` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `last_name` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `facebook_id` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `user_type_id` INT(10) NULL DEFAULT NULL,
    `postcode` INT(10) NULL DEFAULT NULL,
    `status` INT(10) NULL DEFAULT NULL,
    `date_created` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    `date_modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`user_id`),
    UNIQUE INDEX `username` (`username`),
    UNIQUE INDEX `email` (`email`),
    UNIQUE INDEX `user_id` (`user_id`)
)

As such, Im trying to implement A Media Table.

An example of this table is below:

CREATE TABLE `media` (
    `media_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `album_id` INT(10) UNSIGNED NULL DEFAULT '0',
    `alt_name` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
    `mine_type` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
    `url_link` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
    PRIMARY KEY (`media_id`),
    INDEX `media-album_id_key` (`album_id`),
    CONSTRAINT `media-album_id_key` FOREIGN KEY (`album_id`) REFERENCES `album` (`album_id`) ON UPDATE CASCADE ON DELETE CASCADE
)

What I want is to Allow a User, Facility to have 1 image associated with it and a Venue to have a Album associated with it.

As such Im not sure how to implement the foreign keys in this regards because if I put a column of media into the Users table and set a foreign key to the Media_ID if I delete the Media it deletes the User as the cascade affect does this.

What I want is that if I delete the User/Facility/Venue it will delete all images associated with it.

Any assistance would be appreciated.

Regards,

Robert

You cannot do what you want to do directly without using triggers, so I've listed that here as your first option, along with a second option that you really should avoid but will give you a clue as to how to investigate this pattern problem further.

  • Keep the foreign key from each facility/venue/user to the media table, but use a ON DELETE SET NULL rather than ON DELETE CASCADE referential action, and then implement the deletion of related media in your application code (or in triggers)
  • Look up "polymorphic associations" and why they're a bad idea in relational databases, and if you're still convinced you want to do that, use one of the available techniques to construct the inverted relationship you're thinking of doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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