[英]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
,它包含三列:
Both locationLat
and locationlong
make up a composite primary key of this table. locationLat
和locationlong
都构成了该表的复合主键。
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.我有类似的不同的表
houseToBuy
, fullyFurnished
, landtoSell
等。用于表houseTolet
具有类似位置表格上方即复合主键locationLat
, locationLong
和外国复合键location_locationLat
和location_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 table
是houseTolet
和Housetobuy
的子表以及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
表中以供出租,而无需获得外键约束,我需要先更新其他表,例如houseTobuy
或fullyFurnished
,这是因为在特定情况下,我将更新特定类型的房屋只有但没有很多选择。 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*
表( houseToLet
, houseToBuy
,...)只是为了存储位置的当前状态(出租,购买,...):这可以表示为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.