简体   繁体   English

我有一个子表,它依赖于两个不相关的父表。 我想在子表中插入行

[英]I have a child table that depends on two parent tables that are not related. i want to insert rows into child table

I am creating a database for selling houses.我正在创建一个用于销售房屋的数据库。

I have a parent table that I am calling location that has three columns :我有一个父表,我称之为location ,它包含三列:

  • locationLat : the latitude of location of property locationLat : 财产位置的纬度
  • locationLong : the longitude of the property locationLong : 属性的经度
  • locationName : an alternative name that i can use to identify the location of the property by name locationName : 一个替代名称,我可以用它来按名称标识属性的位置

Both locationLat and locationlong make up a composite primary key of this table. locationLatlocationlong都构成了该表的复合主键。

Then I have a child table called houseToLet , this table only stores the houses that are for letting.然后我有一个叫做houseToLet的子表,这个表只存储出租的房子。 I have similar different tables houseToBuy , fullyFurnished , landtoSell etc. The table for houseTolet has a composite primary key like the location table above ie locationLat , locationLong and a foreign composite key location_locationLat and location_locationLong that references the location table.我有类似的不同的表houseToBuyfullyFurnishedlandtoSell等。用于表houseTolet具有类似位置表格上方即复合主键locationLatlocationLong和外国复合键location_locationLatlocation_locationLong引用位置表。

Then I have another table called generalFeatures .然后我有另一个名为generalFeatures表。 This the table where I store general features of all the properties I am selling.这是我存储我销售的所有房产的一般特征的表格。 So generalFeatures table is a child table of houseTolet and Housetobuy as well as Fullyfurnished tables,所以generalFeatures tablehouseToletHousetobuy的子表以及Fullyfurnished表,

When I insert rows into the location table, I find no problem as it is the parent table.当我将行插入location表时,我发现没有问题,因为它是父表。 When i insert rows into houseTolet table I find no problem since it is a child table and I will have inserted the parent location table already.当我将行插入houseTolet表时,我发现没有问题,因为它是一个子表,我已经插入了父位置表。 However when I insert into the generalFeatures table I get into problem since it is a child table of housetoBuy as well as other tables including Fullyfurnished .但是,当我插入generalFeatures表时,我遇到了问题,因为它是housetoBuy的子表以及其他表,包括Fullyfurnished

Please assist me to know how I can do this.请帮助我知道如何做到这一点。

  CREATE TABLE IF NOT EXISTS `foreign`.`location` (
  `locationLat` DECIMAL(10,8) NOT NULL,
  `locationLong` DECIMAL(11,8) NOT NULL,
  `locationName` VARCHAR(35) NOT NULL,
  PRIMARY KEY (`locationLat`, `locationLong`))
ENGINE = InnoDB

CREATE TABLE IF NOT EXISTS `location`.`housetolet` (
  `locationLat` DECIMAL(10,8) NOT NULL,
  `locationLong` DECIMAL(11,8) NOT NULL,
  `type` ENUM('gatedCommunity', 'standalone', 'apartment') NOT NULL,
  `location_locationLat` DECIMAL(10,8) NOT NULL,
  `location_locationLong` DECIMAL(11,8) NOT NULL,
  PRIMARY KEY (`locationLat`, `locationLong`),
  INDEX `fk_housetolet_location_idx` (`location_locationLat` ASC, `location_locationLong` ASC))


CREATE TABLE IF NOT EXISTS `foreign`.`generalfeatures` (
  `locationLat` DECIMAL(10,8) NOT NULL,
  `locationLong` DECIMAL(11,8) NOT NULL,
  `livingAreaAndSize` INT NOT NULL,
  `bedrooms` TINYINT(4) NOT NULL,
  `bathrooms` TINYINT(4) NOT NULL,
  `masterEnsuite` TINYINT(1) NOT NULL,
  `bedroomsWithBathrooms` TINYINT(4) NOT NULL,
  `kitchenAndSize` TINYINT(4) NOT NULL,
  `parkingAndSlots` TINYINT(4) NOT NULL,
  `swimmingPool` TINYINT(1) NOT NULL,
  `liftsAndNumber` TINYINT(4) NOT NULL,
  `CCTV` TINYINT(1) NOT NULL,
  `sizeOfLand` INT(11) NOT NULL,
  `borehole` TINYINT(1) NOT NULL,
  `housetobuy_locationLat` DECIMAL(10,8) NOT NULL,
  `housetobuy_locationLong` DECIMAL(11,8) NOT NULL,
  `housetolet_locationLat` DECIMAL(10,8) NOT NULL,
  `housetolet_locationLong` DECIMAL(11,8) NOT NULL,
  `fullyfurnished_locationLat` DECIMAL(10,8) NOT NULL,
  `fullyfurnished_locationLong` DECIMAL(11,8) NOT NULL,
  PRIMARY KEY (`locationLat`, `locationLong`),
  INDEX `fk_generalfeatures_housetobuy1_idx` (`housetobuy_locationLat` ASC, `housetobuy_locationLong` ASC),
  INDEX `fk_generalfeatures_housetolet1_idx` (`housetolet_locationLat` ASC, `housetolet_locationLong` ASC),
  INDEX `fk_generalfeatures_fullyfurnished1_idx` (`fullyfurnished_locationLat` ASC, `fullyfurnished_locationLong` ASC),
  CONSTRAINT `fk_generalfeatures_housetobuy1`
    FOREIGN KEY (`housetobuy_locationLat` , `housetobuy_locationLong`)
    REFERENCES `foreign`.`housetobuy` (`locationLat` , `locationLong`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_generalfeatures_housetolet1`
    FOREIGN KEY (`housetolet_locationLat` , `housetolet_locationLong`)
    REFERENCES `foreign`.`housetolet` (`locationLat` , `locationLong`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_generalfeatures_fullyfurnished1`
    FOREIGN KEY (`fullyfurnished_locationLat` , `fullyfurnished_locationLong`)
    REFERENCES `foreign`.`fullyfurnished` (`locationLat` , `locationLong`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

I want to be able to insert rows into generalFeatures table for houses to Let without getting foreign key constraints that I need to update other tables like houseTobuy or fullyFurnished first, this is because in a particular instance I will be updating a particular type of a house only but not many options.我希望能够将行插入到generalFeatures表中以供出租,而无需获得外键约束,我需要先更新其他表,例如houseTobuyfullyFurnished ,这是因为在特定情况下,我将更新特定类型的房屋只有但没有很多选择。 A house to let cannot be at the same time be a house to buy in my case.在我的情况下,出租的房子不能同时是购买的房子。

Your database design has flaws.您的数据库设计存在缺陷。 Namely, you are storing in multiple tables information that could be represented as a single record.也就是说,您将可以表示为单个记录的信息存储在多个表中。 As it is, it looks like all tables could be merged into main table locations :实际上,看起来所有表都可以合并到主表locations

  • the house* tables ( houseToLet , houseToBuy , ...) are just there to store the current status of the location (to let, to buy, ...) : this could be represented as an ENUM field in the location table (or as several boolean columns if more than one status can be enabled at a time) house*表( houseToLethouseToBuy ,...)只是为了存储位置的当前状态(出租,购买,...):这可以表示为location表中的ENUM字段(或如果一次可以启用多个状态,则作为多个布尔列)

  • the generalFeatures table also seems to contain a single record for each location, hence all of its fields could be moved to the location table. generalFeatures表似乎也包含每个位置的单个记录,因此其所有字段都可以移动到location表中。

Using a single table appears like the right design for your use case (and it sure avoids issues with foreign constraints...).使用单个表似乎是您用例的正确设计(它确实避免了外部约束的问题......)。

Other considerations : use an autoincremented integer as primary key instead of a composite key (for this, you can create a UNIQUE constraint).其他注意事项:使用自动递增的整数作为主键而不是复合键(为此,您可以创建一个UNIQUE约束)。

Here is a sample DDL for your table :这是您的表的示例 DDL:

CREATE TABLE IF NOT EXISTS `foreign`.`location` (

    -- primary key
    `id` INT AUTO_INCREMENT,
    PRIMARY KEY (`id`),

    -- original columns
    `locationLat` DECIMAL(10,8) NOT NULL,
    `locationLong` DECIMAL(11,8) NOT NULL,
    `locationName` VARCHAR(35) NOT NULL,

    -- house status
    `status` ENUM('toLet', 'houseToBuy', 'fullyFurnished', 'landtoSell') NOT NULL,

    -- columns from `generalfeatures`
    `livingAreaAndSize` INT NOT NULL,
    `bedrooms` TINYINT(4) NOT NULL,
    `bathrooms` TINYINT(4) NOT NULL,
    `masterEnsuite` TINYINT(1) NOT NULL,
    `bedroomsWithBathrooms` TINYINT(4) NOT NULL
    -- other columns from `generalfeatures`...
)ENGINE = INNODB;

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

相关问题 在MySQL 5.7中,我如何选择所有少于2个相关子表行的父表行 - In MySQL 5.7 how would I select all parent table rows with less than 2 related child table rows 可以将子表作为数组包含在父表中吗? - Parent MySQL Table with Child Table with list of images can I have the child table as an array in parent table? 从两个表中选择数据。 一张桌子是父母/孩子,我需要让父母没有关系 - Select data from two tables. one table is parent/child and I need to get the parents without a relation 如何将JSON数据插入父子表 - How do I insert JSON data into parent child table 计数子表中的相关行 - Counting related rows in a child table MYSQL:我有两个表。 我想在表 1 中使用 FK 显示表 1 和表 2 中的数据 - MYSQL: I have two tables. I want to show data from table 1 and table 2 using FK in table 1 我有两个表A和B。我想使用内部联接将数据从表B插入到A - I have two tables both A and B. I want to insert data from table B to A using an inner join 如何从父表的两个子表中获取值 - how to get values from two child tables for parent table JDBC JOINing 多个子表到父表,我怎么知道结果行来自哪个表? - JDBC JOINing multiple child tables to a parent table, how can i know from which table is the resulting row? 在同一表中获取与父项相关的子项元素 - Getting child elements that are related to a parent in same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM