Oracle SQL 约束,输入的名称必须是成员
Oracle SQL constraint, name entered must be a member
我已经创建了一个网球俱乐部数据库。此数据库包含以下 tables:
玩家table
CREATE TABLE player
(
member_id INT PRIMARY KEY NOT NULL,
member_name VARCHAR2(70) NOT NULL,
date_of_birth DATE,
member_address VARCHAR2(300),
contact_number INT NOT NULL,
gender VARCHAR2(1) NOT NULL CHECK(gender IN('f','m')),
club_seeding INT NOT NULL,
county_seeding INT NOT NULL,
renewal_date DATE,
m_type VARCHAR(9) NOT NULL CHECK(m_type IN('junior','student', 'senior', 'family', 'associate'))
);
这包括网球俱乐部的成员。
然后是团队 table
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 VARCHAR2(70) NOT NULL,
player2 VARCHAR2(70) NOT NULL,
team_name VARCHAR2(145) NOT NULL
);
用于输入2名选手组成双打队伍。
我的问题是如何创建一个约束,只允许来自 玩家 table 的玩家(member_name)进入 团队 table?
您需要创建外键约束
请参阅文档 here
但在您的示例中,您需要将 player1
和 player2
列的类型从 varchar
更改为 int
:
sample of alter table statements
-- Create foreign key constraints
alter table TEAM
add constraint fk_team_player_1 foreign key (PLAYER1)
references player (MEMBER_ID);
alter table TEAM
add constraint fk_team_player_2 foreign key (PLAYER2)
references player (MEMBER_ID);
在 table
中创建 FK 约束的示例
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 INT NOT NULL,
player2 INT NOT NULL,
team_name VARCHAR2(145) NOT NULL
,constraint fk_team_player_1 foreign key (PLAYER1)
references player (MEMBER_ID)
,constraint fk_team_player_2 foreign key (PLAYER2)
references player (MEMBER_ID)
);
if you want to use member names as PK
CREATE TABLE player
(
member_name VARCHAR2(70) PRIMARY KEY NOT NULL,
date_of_birth DATE,
member_address VARCHAR2(300),
contact_number INT NOT NULL,
gender VARCHAR2(1) NOT NULL CHECK(gender IN('f','m')),
club_seeding INT NOT NULL,
county_seeding INT NOT NULL,
renewal_date DATE,
m_type VARCHAR(9) NOT NULL CHECK(m_type IN('junior','student', 'senior', 'family', 'associate'))
);
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 VARCHAR2(70) NOT NULL,
player2 VARCHAR2(70) NOT NULL,
team_name VARCHAR2(145) NOT NULL
,constraint fk_team_player_1 foreign key (PLAYER1)
references player (member_name)
,constraint fk_team_player_2 foreign key (PLAYER2)
references player (member_name)
);
我已经创建了一个网球俱乐部数据库。此数据库包含以下 tables:
玩家table
CREATE TABLE player
(
member_id INT PRIMARY KEY NOT NULL,
member_name VARCHAR2(70) NOT NULL,
date_of_birth DATE,
member_address VARCHAR2(300),
contact_number INT NOT NULL,
gender VARCHAR2(1) NOT NULL CHECK(gender IN('f','m')),
club_seeding INT NOT NULL,
county_seeding INT NOT NULL,
renewal_date DATE,
m_type VARCHAR(9) NOT NULL CHECK(m_type IN('junior','student', 'senior', 'family', 'associate'))
);
这包括网球俱乐部的成员。
然后是团队 table
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 VARCHAR2(70) NOT NULL,
player2 VARCHAR2(70) NOT NULL,
team_name VARCHAR2(145) NOT NULL
);
用于输入2名选手组成双打队伍。
我的问题是如何创建一个约束,只允许来自 玩家 table 的玩家(member_name)进入 团队 table?
您需要创建外键约束 请参阅文档 here
但在您的示例中,您需要将 player1
和 player2
列的类型从 varchar
更改为 int
:
sample of alter table statements
-- Create foreign key constraints
alter table TEAM
add constraint fk_team_player_1 foreign key (PLAYER1)
references player (MEMBER_ID);
alter table TEAM
add constraint fk_team_player_2 foreign key (PLAYER2)
references player (MEMBER_ID);
在 table
中创建 FK 约束的示例CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 INT NOT NULL,
player2 INT NOT NULL,
team_name VARCHAR2(145) NOT NULL
,constraint fk_team_player_1 foreign key (PLAYER1)
references player (MEMBER_ID)
,constraint fk_team_player_2 foreign key (PLAYER2)
references player (MEMBER_ID)
);
if you want to use member names as PK
CREATE TABLE player
(
member_name VARCHAR2(70) PRIMARY KEY NOT NULL,
date_of_birth DATE,
member_address VARCHAR2(300),
contact_number INT NOT NULL,
gender VARCHAR2(1) NOT NULL CHECK(gender IN('f','m')),
club_seeding INT NOT NULL,
county_seeding INT NOT NULL,
renewal_date DATE,
m_type VARCHAR(9) NOT NULL CHECK(m_type IN('junior','student', 'senior', 'family', 'associate'))
);
CREATE TABLE team
(
team_id INT PRIMARY KEY NOT NULL,
club_seeding INT,
county_seeding INT,
player1 VARCHAR2(70) NOT NULL,
player2 VARCHAR2(70) NOT NULL,
team_name VARCHAR2(145) NOT NULL
,constraint fk_team_player_1 foreign key (PLAYER1)
references player (member_name)
,constraint fk_team_player_2 foreign key (PLAYER2)
references player (member_name)
);