问题
I tried to create several tables, but only the server table was created. Please fix my work!
CREATE TABLE server
(
SERVER_ID varchar(25) NOT NULL,
SERVER_IP varchar(15) NOT NULL,
SERVER_LOCATION varchar(25) NOT NULL,
SERVER_BRAND varchar(15) NOT NULL,
CONSTRAINT server_pk
PRIMARY KEY (SERVER_ID)
);
CREATE TABLE application
(
APP_ID varchar(25) NOT NULL,
ACCOUNT_NUM varchar(25) NOT NULL,
RECORD_ID varchar(10) NOT NULL,
VERSION_ID varchar(10) NOT NULL,
LAST_UPDATED date NOT NULL,
CONSTRAINT application_pk
PRIMARY KEY (APP_ID),
CONSTRAINT application _fk_account
FOREIGN KEY (ACCOUNT_NUM)
REFERENCES account (ACCOUNT_NUM),
CONSTRAINT application _fk_record
FOREIGN KEY (RECORD_ID)
REFERENCES record (RECORD_ID)
);
CREATE TABLE record
(
RECORD_ID varchar(25) NOT NULL,
VIN_NUM varchar(25) NOT NULL,
SERVER_ID varchar(25) NOT NULL,
CONSTRAINT record_pk
PRIMARY KEY (RECORD_ID, VIN_NUM, SERVER_ID),
CONSTRAINT record_fk_vehicle
FOREIGN KEY (VIN_NUM)
REFERENCES vehicle (VIN_NUM),
CONSTRAINT record_fk_server
FOREIGN KEY (SERVER_ID)
REFERENCES server (SERVER_ID)
);
CREATE TABLE vehicle
(
VIN_NUM varchar(25) NOT NULL,
V_MILEAGE int NOT NULL,
V_GASUSED varchar(25) NOT NULL,
V_ELECTRICALMILES int NOT NULL,
DRIVER_ID varchar(25) NOT NULL,
CONSTRAINT vehicle_pk
PRIMARY KEY (VIN_NUM),
CONSTRAINT vehicle_fk_driver
FOREIGN KEY (DRIVER_ID)
REFERENCES driver (DRIVER_ID)
);
CREATE TABLE driver
(
DRIVER_ID varchar(25) NOT NULL,
LICENSE_NUM varchar(25) NOT NULL,
FIRST_NAME varchar(25) NOT NULL,
LAST_NAME varchar(25) NOT NULL,
INSURANCE_POLICY varchar(25) NOT NULL,
ACCOUNT_NUM int NOT NULL,
CONSTRAINT driver_pk
PRIMARY KEY (DRIVER_ID),
CONSTRAINT driver_fk_account
FOREIGN KEY (ACCOUNT_NUM)
REFERENCES account (ACCOUNT_NUM)
);
CREATE TABLE account
(
ACCOUNT_NUM int NOT NULL,
DRIVER_ID varchar(25) NOT NULL,
DEVICE_ID varchar(25) NOT NULL,
DATE_CREATED date NOT NULL,
ACCOUNT_STATUS varchar(10) NOT NULL,
CONSTRAINT account_pk
PRIMARY KEY (ACCOUNT_NUM, DRIVER_ID),
CONSTRAINT account_fk_driver
FOREIGN KEY (DRIVER_ID)
REFERENCES driver (DRIVER_ID)
);
回答1:
There are several issues here.
You should create tables in order that matches foreign key constraint dependencies, i.e. you can't create detail table first, and then its master table because there's no table (or its primary key) which is to be referenced.
A good workaround for that is to remove foreign key constraints from create table
and create them separately (using alter table ... add constraint
) after all tables are created.
There's no problem if two tables reference each other (such as driver
and account
do), if there's a good reason to do it. However, as I previously said - you can't do it within create table
statements; at least one foreign key has to be moved out.
If primary key consists of several columns (so it is a composite key), foreign key also must contain the same number & datatypes of columns that reference it. That's what you have for driver
and account
tables.
account
's primary key is (account_num, driver_id
)driver
's foreign key can't be just (account_num
) - it has to containdriver_id
as well
Finally, application
table: its foreign keys can't be created because it references
account
table whose primary key is (account_num, driver_id
), but -application
doesn't containdriver_id
column at all which might mean that you wrongly setaccount
's primary key (orapplication
table)record
table which has a composite primary key on (record_id, vin_num, server_id
), whileapplication
has onlyrecord_id
. Remark I wrote foraccount
is valid here as well
Now, if we fix what I mentioned, tables are created, but application
misses all its foreign key constraints.
SQL> CREATE TABLE server
2 (
3 SERVER_ID varchar2(25) NOT NULL,
4 SERVER_IP varchar2(15) NOT NULL,
5 SERVER_LOCATION varchar2(25) NOT NULL,
6 SERVER_BRAND varchar2(15) NOT NULL,
7 CONSTRAINT server_pk
8 PRIMARY KEY (SERVER_ID)
9 );
Table created.
SQL>
SQL> CREATE TABLE driver
2 (
3 DRIVER_ID varchar2(25) NOT NULL,
4 LICENSE_NUM varchar2(25) NOT NULL,
5 FIRST_NAME varchar2(25) NOT NULL,
6 LAST_NAME varchar2(25) NOT NULL,
7 INSURANCE_POLICY varchar2(25) NOT NULL,
8 ACCOUNT_NUM int NOT NULL,
9 CONSTRAINT driver_pk
10 PRIMARY KEY (DRIVER_ID)
11 --CONSTRAINT driver_fk_account
12 --FOREIGN KEY (ACCOUNT_NUM)
13 --REFERENCES account (ACCOUNT_NUM)
14 );
Table created.
SQL>
SQL> CREATE TABLE account
2 (
3 ACCOUNT_NUM int NOT NULL,
4 DRIVER_ID varchar2(25) NOT NULL,
5 DEVICE_ID varchar2(25) NOT NULL,
6 DATE_CREATED date NOT NULL,
7 ACCOUNT_STATUS varchar2(10) NOT NULL,
8 CONSTRAINT account_pk
9 PRIMARY KEY (ACCOUNT_NUM, DRIVER_ID),
10 CONSTRAINT account_fk_driver
11 FOREIGN KEY (DRIVER_ID)
12 REFERENCES driver (DRIVER_ID)
13 );
Table created.
SQL>
SQL> alter table driver add constraint driver_fk_account
2 foreign key (account_num, driver_id)
3 references account (account_num, driver_id);
Table altered.
SQL>
SQL> CREATE TABLE vehicle
2 (
3 VIN_NUM varchar2(25) NOT NULL,
4 V_MILEAGE int NOT NULL,
5 V_GASUSED varchar2(25) NOT NULL,
6 V_ELECTRICALMILES int NOT NULL,
7 DRIVER_ID varchar2(25) NOT NULL,
8 CONSTRAINT vehicle_pk
9 PRIMARY KEY (VIN_NUM),
10 CONSTRAINT vehicle_fk_driver
11 FOREIGN KEY (DRIVER_ID)
12 REFERENCES driver (DRIVER_ID)
13 );
Table created.
SQL>
SQL> CREATE TABLE record
2 (
3 RECORD_ID varchar2(25) NOT NULL,
4 VIN_NUM varchar2(25) NOT NULL,
5 SERVER_ID varchar2(25) NOT NULL,
6 CONSTRAINT record_pk
7 PRIMARY KEY (RECORD_ID, VIN_NUM, SERVER_ID),
8 CONSTRAINT record_fk_vehicle
9 FOREIGN KEY (VIN_NUM)
10 REFERENCES vehicle (VIN_NUM),
11 CONSTRAINT record_fk_server
12 FOREIGN KEY (SERVER_ID)
13 REFERENCES server (SERVER_ID)
14 );
Table created.
SQL>
SQL> CREATE TABLE application
2 (
3 APP_ID varchar2(25) NOT NULL,
4 ACCOUNT_NUM varchar2(25) NOT NULL,
5 RECORD_ID varchar2(10) NOT NULL,
6 VERSION_ID varchar2(10) NOT NULL,
7 LAST_UPDATED date NOT NULL,
8 CONSTRAINT application_pk
9 PRIMARY KEY (APP_ID)
10 --CONSTRAINT application_fk_account
11 --FOREIGN KEY (ACCOUNT_NUM)
12 --REFERENCES account (ACCOUNT_NUM),
13 --CONSTRAINT application_fk_record
14 --FOREIGN KEY (RECORD_ID)
15 --REFERENCES record (RECORD_ID)
16 );
Table created.
SQL>
回答2:
You have to add a /
after every table definition end -
CREATE TABLE server
(
SERVER_ID varchar(25) NOT NULL,
SERVER_IP varchar(15) NOT NULL,
SERVER_LOCATION varchar(25) NOT NULL,
SERVER_BRAND varchar(15) NOT NULL,
CONSTRAINT server_pk
PRIMARY KEY (SERVER_ID)
);
/ ----> Here
CREATE TABLE application
(
APP_ID varchar(25) NOT NULL,
.....
来源:https://stackoverflow.com/questions/60996247/which-statement-is-needed-to-revise