DDL FOR SUPERTYPE AND SUBTYPE ORACLE - oracle

I have an entity with supertype: tb_transaksi with attributes: id_transaksi (pk), tgl_transaksi
and 2 subtypes, namely:
debit_card with attribute: no_kartu
and credit_card with attribute: persen_bunga.
But when I generate into DDL form an error occurs like this:
ERROR: No Discriminator Column found in Arc FKArc_1 - constraint trigger for Arc cannot be generated
ERROR: No Discriminator Column found in Arc FKArc_1 - constraint trigger for Arc cannot be generated
what do I have to do?
DDL STATEMENT :
CREATE TABLE cash (
id_transaksi NUMBER(10, 2) NOT NULL,
nominal NUMBER(10, 2) NOT NULL );
ALTER TABLE cash ADD CONSTRAINT cash_pk PRIMARY KEY ( id_transaksi );
CREATE TABLE credit (
id_transaksi NUMBER(10, 2) NOT NULL,
nomor_kartu NUMBER(10, 2) NOT NULL );
ALTER TABLE credit ADD CONSTRAINT credit_pk PRIMARY KEY ( id_transaksi
);
CREATE TABLE tb_transaksi (
id_transaksi NUMBER(10, 2) NOT NULL );
ALTER TABLE tb_transaksi ADD CONSTRAINT tb_transaksi_pk PRIMARY KEY (
id_transaksi );
ALTER TABLE cash
ADD CONSTRAINT cash_tb_transaksi_fk FOREIGN KEY ( id_transaksi )
REFERENCES tb_transaksi ( id_transaksi );
ALTER TABLE credit
ADD CONSTRAINT credit_tb_transaksi_fk FOREIGN KEY ( id_transaksi )
REFERENCES tb_transaksi ( id_transaksi );
-- ERROR: No Discriminator Column found in Arc FKArc_1 - constraint trigger for Arc cannot be generated
-- ERROR: No Discriminator Column found in Arc FKArc_1 - constraint trigger for Arc cannot be generated

Related

ORA-02256: number of referencing columns must match referenced [duplicate]

This question already has an answer here:
ORA-02256: referencing foreign key
(1 answer)
Closed last year.
CREATE TABLE Goft_ForeEver_cus(
customer_Id VARCHAR(10),
first_Name VARCHAR2(20) CONSTRAINT sys_cus_fName_nn NOT NULL,
last_Name VARCHAR2(20) CONSTRAINT sys_cus_lName_nn NOT NULL,
girt_Card_Amount NUMBER(5,2) CONSTRAINT sys_cus_gca_nn NOT NULL,
email VARCHAR2(30) CONSTRAINT sys_cus_email_ck CHECK (email ='%[^a-z,0-9,#,.,_,-]%' ) CONSTRAINT sys_cus_email_nn NOT NULL,
CONSTRAINT sys_cus_pk PRIMARY KEY(customer_Id)
);
ALTER TABLE Goft_ForeEver_cus
ADD CONSTRAINT sys_cus_email_UK UNIQUE (email);
CREATE TABLE Goft_ForeEver_Course_Info(
course_Name VARCHAR2(20),
city VARCHAR2(30) CONSTRAINT sys_courtInf_city_nn NOT NULL,
prov VARCHAR2(20) CONSTRAINT sys_courtInf_prov_nn NOT NULL,
postal_Code VARCHAR2(10) CONSTRAINT sys_courtInf_posC_nn NOT NULL,
star_rating NUMBER(1,1),
discript VARCHAR2(200) CONSTRAINT sys_courtInf_descript_uk UNIQUE,
year_Build DATE CONSTRAINT sys_courtInf_date_nn NOT NULL,
court_length Number(5,2)
CONSTRAINT sys_courtInf_courtL_ck CHECK( court_length = 'YARD')
CONSTRAINT sys_courtInf_courtL_nn NOT NULL,
CONSTRAINT sys_courtInf_pk PRIMARY KEY (course_Name, star_rating )
);
CREATE TABLE Goft_ForeEver_favorite_courses(
course_Name VARCHAR2(20),
customer_Id VARCHAR2(10),
CONSTRAINT sys_favCourt_PK PRIMARY KEY(course_Name,customer_Id),
CONSTRAINT sys_favCourt_FK1 FOREIGN KEY (customer_Id) REFERENCES Goft_ForeEver_cus(customer_Id),
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
);
);
Error
Error report - ORA-02256: number of referencing columns must match referenced columns 02256. 00000 - "number of referencing columns must match referenced columns" *Cause: The number of columns in the foreign-key referencing list is not equal to the number of columns in the referenced list. *Action: Make sure that the referencing columns match the referenced columns.
It is because Goft_ForeEver_Course_Info has primary key as
CONSTRAINT sys_courtInf_pk PRIMARY KEY (course_Name, star_rating )
while Goft_ForeEver_favorite_courses that references it has foreign key as
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
See? You're referencing COURSE_NAME, STAR_RATING composite key by COURSE_NAME only - that won't work.
Therefore, either remove STAR_RATING from the primary key in Goft_ForeEver_Course_Info, add STAR_RATING to Goft_ForeEver_favorite_courses, or redesign everything alltogether.
Illustration (SQL*Plus, which points to the error):
SQL> CREATE TABLE Goft_ForeEver_favorite_courses(
2 course_Name VARCHAR2(20),
3 customer_Id VARCHAR2(10),
4 CONSTRAINT sys_favCourt_PK PRIMARY KEY(course_Name,customer_Id),
5 CONSTRAINT sys_favCourt_FK1 FOREIGN KEY (customer_Id) REFERENCES Goft_ForeEver_cus(customer_Id),
6 CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
7 );
CONSTRAINT sys_favCourt_FK2 FOREIGN KEY (course_Name) REFERENCES Goft_ForeEver_Course_Info(course_Name)
*
ERROR at line 6:
ORA-02270: no matching unique or primary key for this column-list
SQL>
The problem here is you have declared the "course_Name" along with "star_rating" column as composite primary key in Goft_ForeEver_Course_Info table whereas you are referencing only one column "course_Name" in sys_favCourt_FK2 foreign key declaration in Goft_ForeEver_favorite_courses table.

"ORA-00904 invalid identifier" when adding foreign key constraint

I'm trying to create a "diagnosis" table(last table), but something is wrong with the 2 lines of code that creates Foreign Key. I'm sure that's where the problem lies because when I comment them out, I can create the table. The first 4 tables can be created with no errors, so no problem with them. Thank you for reading my question.
I have tried to spot some syntax errors but to no avail.
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
I expected that the table can be created with no error.
Your diagnosis table refers to out_pat_ID and in_pat_ID as columns which should serve as foreign keys, but these columns don't actually exist in the table definition. Try adding them as one possible fix here:
CREATE TABLE diagnosis (
dia_ref number(12) NOT NULL,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8),
out_pat_ID number(5),
in_pat_ID number(5)
CONSTRAINT dia_pk PRIMARY KEY (dia_ref),
CONSTRAINT dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINT dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
diagnosis table need add out_pat_ID and in_pat_ID column
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
✓
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
✓
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
✓
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
✓
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5) not null,
in_pat_ID number(5) not null,
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
✓
db<>fiddle here
If you are allowing only one patient per diagnosis, then you should have a check constraint as well. Your comment and data structure are out-of-sync:
CREATE TABLE diagnosis (
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
out_pat_ID number(5),
in_pat_ID number(5),
constraint chk_diagnosis_pat_id
check ( (out_pat_id is not null and in_pat_id is null) or
(out_pat_id is null and in_pat_id is not null)
),
constraint dia_pk primary key (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
constraint dia_out_fk foreign key (out_pat_ID) references out_patient (out_pat_ID),
constraints dia_in_fk foreign key (in_pat_ID) references in_patient (in_pat_ID)
);
That said, separate tables for in-patient and out-patient do not look correct. Or, at the very least, you need a persons table with information about a person over time. Then you can have separate "appointments" and "registrations" to determine if they make an out-patient appointment or register for in-patient care.
Personally I prefer to define single-column constraints inline as part of the column definition. Then you don't have to repeat the column name, and for FK constraints you can let the column inherit its parent's datatype so it is less error-prone.
create table in_patient
( in_pat_id number(5) constraint in_pat_pk primary key
, in_pat_name varchar2(20)
, in_pat_add varchar2(50)
, in_pat_dob date
, in_pat_history varchar2(100)
, in_mode_payment varchar2(20)
, in_start_date date
, in_end_date date
);
create table out_patient
( out_pat_id number(5) constraint out_pat_pk primary key
, out_pat_name varchar2(20)
, out_pat_add varchar2(50)
, out_pat_dob date
, out_pat_history varchar2(100)
, out_mode_payment varchar(20)
, out_date_of_visit date
);
create table diagnosis
( dia_ref number(12) constraint dia_pk primary key
, dia_type varchar2(20)
, dia_date date
, dia_result varchar2(8)
, dia_out_fk references out_patient (out_pat_id)
, dia_in_fk references in_patient (in_pat_id)
);
If dia_result can only be 'POSITIVE' or 'NEGATIVE' then I'd suggest adding a check constraint to enforce this.
Probably some more columns could be defined as not null. It's a good idea to define every possible constraint you can think of.

ORA-02256: referencing foreign key

I'm a SQL newbie and having some troubles with referencing a foreign KEY
My Event Table:
create table Event
(
Bookid number(5),
edate date,
FacID int,
GuestID int,
CONSTRAINT pk1edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk1Bookid FOREIGN KEY (Bookid) references BOOK (Bookid),
CONSTRAINT fk2FacID FOREIGN KEY (FacID) references CUSTOMER (SponsorID),
CONSTRAINT fk3GuestID FOREIGN KEY (GuestID) references CUSTOMER (SponsorID)
);
and my event_register table
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK,
CONSTRAINT fk3edate FOREIGN KEY (edate) references Event (edate,Bookid)
);
I get this error when I try to run the Event_Register:
ERROR at line 9:
ORA-02256: number of referencing columns must match referenced columns
You need to specify all the columns in both parent and child tables while referring.
create table EVENT_REGISTER
(
CID number(6),
Bookid number(5),
edate date,
CONSTRAINT pk1Edate PRIMARY KEY (edate,Bookid),
CONSTRAINT fk2Bookid FOREIGN KEY (Bookid) references BOOK(Bookid),
CONSTRAINT fk3edate FOREIGN KEY (edate,Bookid) references Event (edate,Bookid)
);

oracle change a column to unique

Table has been created in system this way
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The new crete table statement is as below:
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The differnce is INSTANCEID has UNIQUE in it. How do i Alter the table? I used the below statement and it did not work for me.
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID);
It gave an error:
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID)
Error report:
SQL Error: ORA-02261: such unique or primary key already exists in the table
02261. 00000 - "such unique or primary key already exists in the table"
*Cause: Self-evident.
*Action: Remove the extra key.
Please help me to Alter the table as required above. Thanks!
Here is the output of SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES';
"CONSTRAINT_NAME","COLUMN_NAME","CONSTRAINT_TYPE"
"SYS_C0016531","DM","C"
"SYS_C0016532","INSTANCEID","C"
"SYS_C0016533","INSTANCENAME","C"
"SYS_C0016534","HOSTNAME","C"
"PK_INSTANCES","HOSTNAME","P"
"PK_INSTANCES","INSTANCEID","P"
"SYS_C0016536","INSTANCENAME","U"
You have already stated that INSTANCEID is supposed to be UNIQUE, so a constraint has been created.
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE, -- UNIQUE constraint
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
Edit: Ok, after reading your comment, try this:
SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES'
AND con.constraint_type = 'U'
;
It will list UNIQUE constraints and associated columns for INSTANCE table. Please check if there is a unique constraint on the INSTANCEID column (and if that constraint has no other associated columns).
Example at SQLFiddle: http://sqlfiddle.com/#!4/43b43/6
Edit #2: creating named constraints, all options:
-- CREATE TABLE - "In Line" Constraints
CREATE TABLE ports (
ID NUMBER CONSTRAINT PORT_ID_PK PRIMARY KEY,
NAME VARCHAR2(20)
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_NN NOT NULL
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_UQ UNIQUE
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER CONSTRAINT SHIP_PORT_FK
REFERENCES PORTS (ID)
);
-- CREATE TABLE - "Out of Line" Constraints
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT PORT_ID_PK PRIMARY KEY (ID)
);
-- NOT NULL constraints can not be created "Out of Line"!
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT NAME_UQ UNIQUE (NAME)
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER,
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER,
CONSTRAINT SHIP_PORT_FK FOREIGN KEY
(HOME_PORT_ID) REFERENCES PORTS (ID)
);
-- ALTER TABLE - "In Line" Constraints
ALTER TABLE PORTS MODIFY ID
CONSTRAINT PORT_ID_PK PRIMARY KEY;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_NN NOT NULL;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_UQ UNIQUE;
ALTER TABLE SHIPS MODIFY HOME_PORT_ID
CONSTRAINT SHIP_PORT_FK REFERENCES PORTS (ID);
-- ALTER TABLE - "Out of Line" Constraints
ALTER TABLE PORTS ADD CONSTRAINT
PORT_ID_PK PRIMARY KEY (ID);
-- NOT NULL constraints can not be created "Out of Line"!
ALTER TABLE PORTS ADD CONSTRAINT
NAME_UQ UNIQUE (NAME);
ALTER TABLE PORTS ADD
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5));
ALTER TABLE SHIPS ADD CONSTRAINT SHIP_PORT_FK
FOREIGN KEY (HOME_PORT_ID)
REFERENCES PORTS (ID);
NOT NULL constraints cannot be create of out line.

EF4 generates invalid script

When I right-click in a .EDMX file and click Generate Database From Model, the resulting script is obviously wrong because of the table names. What it generates is the following script. Note the table names in the DROP TABLE part versus the CREATE TABLE part.
Why is this inconsistent?
This is obviously not a reusable script. What I created was an Entity named "Address" and an Entity named "Company", etc (all singular). The EntitySet names are pluralized. The "Pluralize New Objects" boolean does not change this either. So what's the deal?
For what it's worth, I originally generated the EDMX by pointing it to a database that had tables with non-pluralized names and now that I've made some changes, I want to go back the other way. I'd like to have the option to go back and forth as neither the db-first nor the model-first model is ideal in all scenarios, and I have the control to ensure that there will be no merging issues from multiple people going both ways at the same time.
-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- NOTE: if the constraint does not exist, an ignorable error will be reported.
-- --------------------------------------------------
ALTER TABLE [Address] DROP CONSTRAINT [FK_Address_StateID-State_ID];
GO
ALTER TABLE [Company] DROP CONSTRAINT [FK_Company_AddressID-Address_ID];
GO
ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_BossEmployeeID-Employee_ID];
GO
ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_CompanyID-Company_ID];
GO
ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_PersonID-Person_ID];
GO
ALTER TABLE [Person] DROP CONSTRAINT [FK_Person_AddressID-Address_ID];
GO
-- --------------------------------------------------
-- Dropping existing tables
-- NOTE: if the table does not exist, an ignorable error will be reported.
-- --------------------------------------------------
DROP TABLE [Address];
GO
DROP TABLE [Company];
GO
DROP TABLE [Employee];
GO
DROP TABLE [Person];
GO
DROP TABLE [State];
GO
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'Addresses'
CREATE TABLE [Addresses] (
[ID] int IDENTITY(1,1) NOT NULL,
[StreetAddress] nvarchar(100) NOT NULL,
[City] nvarchar(100) NOT NULL,
[StateID] int NOT NULL,
[Zip] nvarchar(10) NOT NULL
);
GO
-- Creating table 'Companies'
CREATE TABLE [Companies] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(100) NOT NULL,
[AddressID] int NOT NULL
);
GO
-- Creating table 'People'
CREATE TABLE [People] (
[ID] int IDENTITY(1,1) NOT NULL,
[FirstName] nvarchar(100) NOT NULL,
[LastName] nvarchar(100) NOT NULL,
[AddressID] int NOT NULL
);
GO
-- Creating table 'States'
CREATE TABLE [States] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(100) NOT NULL,
[Abbreviation] nvarchar(2) NOT NULL
);
GO
-- Creating table 'Employees'
CREATE TABLE [Employees] (
[ID] int IDENTITY(1,1) NOT NULL,
[PersonID] int NOT NULL,
[CompanyID] int NOT NULL,
[Position] nvarchar(100) NOT NULL,
[BossEmployeeID] int NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [ID] in table 'Addresses'
ALTER TABLE [Addresses]
ADD CONSTRAINT [PK_Addresses]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [PK_Companies]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'People'
ALTER TABLE [People]
ADD CONSTRAINT [PK_People]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'States'
ALTER TABLE [States]
ADD CONSTRAINT [PK_States]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [PK_Employees]
PRIMARY KEY ([ID] );
GO
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [StateID] in table 'Addresses'
ALTER TABLE [Addresses]
ADD CONSTRAINT [FK_Address_StateID_State_ID]
FOREIGN KEY ([StateID])
REFERENCES [States]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Address_StateID_State_ID'
CREATE INDEX [IX_FK_Address_StateID_State_ID]
ON [Addresses]
([StateID]);
GO
-- Creating foreign key on [AddressID] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [FK_Company_AddressID_Address_ID]
FOREIGN KEY ([AddressID])
REFERENCES [Addresses]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Company_AddressID_Address_ID'
CREATE INDEX [IX_FK_Company_AddressID_Address_ID]
ON [Companies]
([AddressID]);
GO
-- Creating foreign key on [AddressID] in table 'People'
ALTER TABLE [People]
ADD CONSTRAINT [FK_Person_AddressID_Address_ID]
FOREIGN KEY ([AddressID])
REFERENCES [Addresses]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Person_AddressID_Address_ID'
CREATE INDEX [IX_FK_Person_AddressID_Address_ID]
ON [People]
([AddressID]);
GO
-- Creating foreign key on [CompanyID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_CompanyID_Company_ID]
FOREIGN KEY ([CompanyID])
REFERENCES [Companies]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_CompanyID_Company_ID'
CREATE INDEX [IX_FK_Employee_CompanyID_Company_ID]
ON [Employees]
([CompanyID]);
GO
-- Creating foreign key on [BossEmployeeID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_BossEmployeeID_Employee_ID]
FOREIGN KEY ([BossEmployeeID])
REFERENCES [Employees]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_BossEmployeeID_Employee_ID'
CREATE INDEX [IX_FK_Employee_BossEmployeeID_Employee_ID]
ON [Employees]
([BossEmployeeID]);
GO
-- Creating foreign key on [PersonID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_PersonID_Person_ID]
FOREIGN KEY ([PersonID])
REFERENCES [People]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_PersonID_Person_ID'
CREATE INDEX [IX_FK_Employee_PersonID_Person_ID]
ON [Employees]
([PersonID]);
GO
-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------
I have been unable to find an answer to this problem. In addition, I have had issues recreating this problem where I'm not going back and forth from DB-first to model-first scenarios. As such, I'm just assuming that back-and-forth has caused issues and is a bad situation to put EF through.

Resources