English 中文(简体)
ISQL:组成一个部分,分裂的等级
原标题:mySQL: Forming a Partial, Disjointed Hierarchy
  • 时间:2024-04-14 00:07:59
  •  标签:
  • mysql

I m试图在Im数据库内建立一个等级结构。 在我的特别例子中,我正在跟踪一个联盟的成员是否是教练员或参与者。 我理解等级背后的想法,以及它如何运作,但却没有规划(我们首先提出欧洲复兴开发银行,没有编码)。 现在,如果我试图克服问题,或者仅仅不理解我需要如何把我在复兴共和与民主运动中所做的事情改成我的法典,那我就感到不舒服。

为了提供进一步的背景,客户的规格如下:

某个角色在某个季节在一个小组中扮演角色。 然而,参与者可能将工作队从一个季度改为另一个季度。 对于参与者来说,我们保留了姓名(我们希望根据最后名称进行查询)、出生日期、妇女人数(当参与者改变团队或从一个季节到另一个季节时可以改变)以及父母的移动电话。

每个季度各一个或一个以上科室,但最多3个。 一个小组由一名辅导员组成。 对于一名教师,我们保留姓名(我们根据头和最后名称进行查询)、出生日期、开始辅导的年份和电子邮件。

For the new entities, as applicable, the first, last and middle name components are at most 17 characters long. Phones are stored in the format: (999) 999-9999. Emails are at most 22 characters long. Organize the persons associated with the league in a hierarchy.

我的复兴共和与民主军处理这一问题的部分是:

“ERD

现行法典是:

CREATE TABLE MEMBER_BK(
    ID_BK INT PRIMARY KEY,
    FIRST_NAME_BK VARCHAR(17) NOT NULL,
    MIDDLE_NAME_BK VARCHAR(17),
    LAST_NAME_BK VARCHAR(17) NOT NULL,
    POSITION_BK CHAR(1) NOT NULL, # <P, C>, P = Player, C = Coach
    CHECK(POSITION_BK =  P  OR  C )
    );

CREATE TABLE PLAYER_BK(
    MEMBER_ID_BK INT,
    MEMBER_POSITION_BK CHAR(1),
    JERSEY_NUMBER_BK INT NOT NULL,
    PHONE_BK CHAR(14) NOT NULL,
    PRIMARY KEY(MEMBER_ID_BK, MEMBER_POSITION_BK),
    FOREIGN KEY(MEMBER_ID_BK) REFERENCES MEMBER_BK(ID_BK),
    #FOREIGN KEY(MEMBER_POSITION_BK) REFERENCES MEMBER_BK(POSITION_BK),
    CHECK(MEMBER_POSITION_BK =  P )
    );

CREATE TABLE COACH_BK(
    MEMBER_ID_BK INT,
    MEMBER_POSITION_BK CHAR(1),
    START_YEAR_BK YEAR NOT NULL,
    EMAIL_BK VARCHAR(22) NOT NULL,
    PRIMARY KEY(MEMBER_ID_BK, MEMBER_POSITION_BK),
    FOREIGN KEY(MEMBER_ID_BK) REFERENCES MEMBER_BK(ID_BK),
    #FOREIGN KEY(MEMBER_POSITION_BK) REFERENCES MEMBER_BK(POSITION_BK),
    CHECK(MEMBER_POSITION_BK =  C )
    );

目前,当我没有发表评论时,我收到一份1822年错误的法典,使我敢于克服这一问题。 将“ID_BK”的派任移至“FK”无错误。 在我寻求答案时,我确实看到,我可能需要编制索引,但这不是我所认为的。

问题回答

您可以使用一种与ID_BK和POSITION_BK并使用thaT ASREFERENCE相结合的关键:

添加一个独特之处 id的钥匙不会允许成员拥有C和P,但我想到的是真正的生活,一名成员可以有两种立场。

CREATE TABLE MEMBER_BK(
    ID_BK INT ,
    FIRST_NAME_BK VARCHAR(17) NOT NULL,
    MIDDLE_NAME_BK VARCHAR(17),
    LAST_NAME_BK VARCHAR(17) NOT NULL,
    POSITION_BK CHAR(1) NOT NULL, # <P, C>, P = Player, C = Coach
    CHECK(POSITION_BK =  P  OR  C )
  ,UNIQUE (ID_BK)
,  PRIMARY KEY (ID_BK, POSITION_BK)
    );


CREATE TABLE PLAYER_BK(
    MEMBER_ID_BK INT,
    MEMBER_POSITION_BK CHAR(1),
    JERSEY_NUMBER_BK INT NOT NULL,
    PHONE_BK CHAR(14) NOT NULL,
    PRIMARY KEY(MEMBER_ID_BK, MEMBER_POSITION_BK),
    FOREIGN KEY(MEMBER_ID_BK,MEMBER_POSITION_BK) REFERENCES MEMBER_BK(ID_BK,POSITION_BK ),
    CHECK(MEMBER_POSITION_BK =  P )
    );


CREATE TABLE COACH_BK(
    MEMBER_ID_BK INT,
    MEMBER_POSITION_BK CHAR(1),
    START_YEAR_BK YEAR NOT NULL,
    EMAIL_BK VARCHAR(22) NOT NULL,
    PRIMARY KEY(MEMBER_ID_BK, MEMBER_POSITION_BK),
     FOREIGN KEY(MEMBER_ID_BK,MEMBER_POSITION_BK) REFERENCES MEMBER_BK(ID_BK,POSITION_BK ),
    CHECK(MEMBER_POSITION_BK =  C )
    );

https://dbfiddle.uk/m5qQHO6Z”rel=“nofollow noreferer”>fiddle





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签