简体   繁体   English

需要使用两个主键创建表(SQL)

[英]Need to create table with two primary keys (SQL)

i need to make a table with two primary keys in SQL, im new to coding and really need help im so confused! 我需要用SQL创建两个主键的表,这是编码的新手,真的需要帮助,我很困惑!

anyway here are my tables that i have. 无论如何,这是我的桌子。

mysql> create table participant(
    -> participantid int not null primary key,
    -> participant_fname varchar(20) not null,
    -> participant_lname varchar(20) not null);


    mysql> create table event(
    -> event_id int not null primary key,
    -> event_name varchar(35) not null,
    -> event_date int not null);

so those are my two tables, i need to merge them somehow and make a table called eventparticipant with (event_id and participantid) as primary keys? 所以这是我的两个表,我需要以某种方式合并它们,并创建一个名为eventparticipant的表,并以(event_id和participantid)作为主键? any idea how please? 知道如何取悦吗?

thank you! 谢谢!

I assume that you are trying to create am : n relationship between the two tables and that the eventparticipant table will serve as junction table. 我假设您正在尝试在两个表之间创建am:n关系,并且eventparticipant表将用作联结表。

This new table will have only one primary key consisting of the two columns participant_id and event_id . 这个新表将只有一个由两列的主键participant_idevent_id Note that a table can have only one primary key, but this primary key can be made of several columns. 请注意,一个表只能有一个主键,但是此主键可以由几列组成。 Each combination of the values of these columns must be unique. 这些列的值的每个组合必须唯一。

CREATE TABLE eventparticipant(
    participant_id int not null,
    event_id int not null,
    PRIMARY KEY ( participant_id, event_id )
);

ALTER TABLE participant
ADD CONSTRAINT fk_participant_eventpart
FOREIGN KEY(participant_id)
REFERENCES eventparticipant(participant_id)
ON DELETE CASCADE;

ALTER TABLE event
ADD CONSTRAINT fk_event_eventpart
FOREIGN KEY(event_id)
REFERENCES eventparticipant(evet_id)
ON DELETE CASCADE;

The ON DELETE CASCADE clause is optional. ON DELETE CASCADE子句是可选的。 It means that if you delete either a participant or an event, then the junction between the two will automatically be deleted. 这意味着,如果您删除参与者或事件,那么两者之间的连接点将被自动删除。 On the other hand, if you don't add this clause, then you will not be able to delete participants or events, unless you first delete all related eventparticipant records. 另一方面,如果不添加此子句,则除非先删除所有相关的eventparticipant记录,否则将无法删除参与者或事件。

If you didn't create these foreign key constraints, it would be possible to add records in table eventparticipant with id's not existing in participant or event and you could also delete participants or events and leave ghosted records in eventparticipant behind. 如果未创建这些外键约束,则可以在表eventparticipant添加participantevent不存在id的记录,还可以删除参与者或事件,并在eventparticipant留下重影记录。


If you really want to merge these two tables, don't do this physically, instead create a merged view or just a select query on these three tables 如果您确实要合并这两个表,请不要实际执行此操作,而是创建合并视图或仅对这三个表进行选择查询

SELECT
    p.participant_fname,
    p.participant_lname,
    e.event_name,
    e.event_date
FROM
    participants p
    INNER JOIN eventparticipant ep
        ON p.participant_id = ep.participant_id
    INNER JOIN event e
        ON ep.event_id = e.event_id;

Note: creating a merged table would mean to have non-normalized data. 注意:创建合并表将意味着具有非规范化数据。 This is bad, because you must keep several copies of the same data. 这很不好,因为您必须保留同一数据的多个副本。 For each participant of an event, you would have to enter the event names and dates again. 对于事件的每个参与者,您将不得不再次输入事件名称和日期。 This makes it difficult to maintain the data and to keep it consistent. 这使得难以维护数据并使数据保持一致。 If the event date changes, for instance, then it can happen that you forget to update it for all participants or that you mistype it somewhere. 例如,如果事件日期更改,则可能会发生忘记为所有参与者更新日期或在某个地方输错日期的情况。

You can't have two primary keys in a table. 一个表中不能有两个主键。 Also, you don't really want to combine those tables do you? 另外,您真的不想合并这些表吗? Good design would have separate tables for participants and events. 好的设计应为参与者和活动设置单独的表格。 The participant table might have a column with a foreign key to the event, or if a participant can have multiple events, you would create another table to link events and participants. 参与者表可能有一列带有事件外键的列,或者如果一个参与者可以拥有多个事件,则您将创建另一个表来链接事件和参与者。 IOW, a many to many relationship. IOW,多对多关系。

So, from a design perspective, you need to determine if participants can have more than one event. 因此,从设计角度来看,您需要确定参与者是否可以举办多个活动。

What you are looking for is Composite key which is a key made from two or more columns. 您正在寻找的是复合键 ,它是由两列或更多列组成的键 So, your table is defined by: 因此,您的表定义为:

create table eventparticipant(
    event_id int not null,
    participant_id int not null,
    PRIMARY KEY (participant_id, event_id)
);

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

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